I am creating a program where the user can enter an even number between 4 and 5000 and the program will output a list of pairs of prime numbers that sum up the number provided in the input.
My program works fine to put all the prime numbers in a list and checks that the input is an even integer between 4 and 5000. For small inputs it works perfectly but for larger inputs it says that the "list index is out of range" and I have no idea why.
This is the code I used: (added comments to help)
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()
For a small number this works perfectly e.g.
>>>
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
>>>
But for a larger number like 800, (actually its any number above 668 for some reason if that helps) then it says "IndexError: list index out of range"
>>>
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
>>>
I have no idea why this isn't working after 668.
So, my foremost advices: don't use try unless you really need it. It will hide code bugs. Avoid global variables unless you really need it. It makes your brain dizzy after a while.
In your code the second sin is the source of the problem. You are running a for loop in range(q) which points to prime_numbers[i]. Tying those indexes mean that both lists need to have the same size, or primenumbers should be smaller than q. Write print(i)
and print(len(prime_numbers)), in the beginning of your for i in range(q)
loop. You will see that the script breaks when i > len(prime_numbers)
To avoid the try, write regex cases for your input. To avoid the global, pass the list as a function argument, or run the loop on the prime_number list.
Hope this helps.