Skip to main content
 首页 » 编程设计

python之哥德巴赫猜想算法显示 "list index out of range"高于特定数字

2024年10月01日14grandyang

我正在创建一个程序,用户可以在其中输入一个 4 到 5000 之间的偶数,该程序将输出一个素数对列表,这些素数对输入中提供的数字求和。

我的程序工作正常,可以将所有质数放入列表中,并检查输入是否为 4 到 5000 之间的偶数。对于小输入,它工作得很好,但对于更大的输入,它说“列表索引超出范围”,我不知道为什么。

这是我使用的代码:(添加评论帮助)


global prime_numbers 
prime_numbers=[] 
user_number=0 
def start(): 
    for i in range(5000): 
        prime_test(i) 
 
def prime_test(a): 
    global prime_numbers 
    x = True  
    for i in range(2, a): 
            while x: 
               if a%i == 0: 
                   x = False 
                   break 
               else: 
                   x = True 
                   break 
 
 
if x==True and a!=0 and a!=1: 
    prime_numbers.append(a) 
    #print(prime_numbers[int(len(prime_numbers)-1)])# 
    #prints last digit/\ /\ /\ (for testing purposes)# 
    if a==4999: 
        user_input() 
def user_input(): 
    user_number=input('please enter a whole even number between 4 and 5000:   ') 
    #checks if user input is valid# 
    try: 
        #checks for a number# 
        user_number=float(user_number) 
        #checks for an integar# 
        if user_number!=int(user_number): 
            print('that is not a whole number') 
            user_input() 
        #checks for an even number# 
        a=(user_number/2) 
        if a!=int(a): 
            print('that is not an even number') 
            user_input() 
        #checks it is inbetween 4 and 5000# 
        elif user_number<4: 
            print('that is not greater than or equal to 4') 
            user_input() 
        elif user_number>5000: 
             print('that is not less than or equal to 5000') 
             user_input() 
        #if it is a valid input it procedes to the next section# 
        else: 
            q=int(user_number) 
            goldbergs_conjecture(q) 
    #the bit that checks to see if it is a number# 
    except ValueError: 
        print('that is not a number') 
        user_input() 
def goldbergs_conjecture(q): 
    global prime_numbers 
    for i in range(q): 
        #serches for the first prime number in the list then the second ect...# 
        x=prime_numbers[i] 
        #y= user input minus a prime number form the list# 
        y=q-x 
        #if y is also in the list then it must be prime also so these are printed# 
        if y in prime_numbers and x<(q/2): 
            print(str(x) + ' + ' + str(y)) 
        #had to add this seperatly because if they were the same then this /\/\ didnt catch them# 
        if y==x: 
            print(str(x) + ' + ' + str(y)) 
#gives the user something to look at whilst the list is generating#     
print('loading...') 
start() 

对于少数情况,这非常有效,例如


>>>  
 RESTART: C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py  
loading... 
please enter a whole even number between 4 and 5000:   568 
5 + 563 
11 + 557 
47 + 521 
59 + 509 
89 + 479 
101 + 467 
107 + 461 
137 + 431 
149 + 419 
167 + 401 
179 + 389 
251 + 317 
257 + 311 
>>>  

但是对于更大的数字,如 800,(如果有帮助的话,实际上它是出于某种原因大于 668 的任何数字)然后它会说“IndexError:列表索引超出范围”


>>>  
 RESTART: C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py  
loading... 
please enter a whole even number between 4 and 5000:   800 
3 + 797 
13 + 787 
31 + 769 
43 + 757 
61 + 739 
67 + 733 
73 + 727 
109 + 691 
127 + 673 
139 + 661 
157 + 643 
181 + 619 
193 + 607 
199 + 601 
223 + 577 
229 + 571 
277 + 523 
313 + 487 
337 + 463 
367 + 433 
379 + 421 
Traceback (most recent call last): 
  File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 72, in <module> 
    start() 
  File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 6, in start 
    prime_test(i) 
  File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 26, in prime_test 
    user_input() 
  File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 52, in user_input 
    goldbergs_conjecture(q) 
  File "C:\!A-level cs preparation\9th problem, goldbach's conjecture\main code first attempt.py", line 61, in goldbergs_conjecture 
    x=prime_numbers[i] 
IndexError: list index out of range 
>>>  

我不知道为什么这在 668 之后不起作用。

请您参考如下方法:

您在 range(q) 上循环(在 goldbergs_conjecture() 中),范围可以是 4..5000 之间的任何值。

但您正在寻找这个值:x=prime_numbers[i],它是 prime_numbers 列表的索引。

prime_numbers 列表不保证与 range(q) 一样长,因为(根据定义)素数更少给定范围内的数字而不是整数。

在请求输入之前尝试打印出 prime_numbers 列表的长度。我敢打赌它接近 668。

相反,您可以尝试以下方法:

for prime in prime_numbers: 
    if prime >= q: 
        break 
 
    ... do something with prime 

遍历所有小于 q 的素数。