I am trying to solve this problem: Goldbach Conjecture
Show with a program "goldbach.py" that all even numbers up to 1000 can indeed be written as the sum of two primes. Specifically: for each even number, also show explicitly (on the screen) that it can be written as the sum of two primes, as in the example below
Even more important is of course if you find a number that does not meet Goldbach's suspicion. Make sure your program clearly displays such a discovery on the screen. Bingo!
16 = ...
18 = 5 + 13
20 = 3 + 17
22 = 5 + 17
24 = ...
Progress
So far, I have created a list where all the primes until 1000 are stored, and then I have created a list in which all the combination of primes of which the sum is an even number until 1000. I knew the format to have it print 3 + 17, but I am stuck in trying to have it say sum(pairs) = prime1 "+" prime2. Should be 3 + 17 = 20 for example. Also, I don't know how to have only 1 example of a pair of primes who's sum is of an even number until 1000. I need to break the loop some how.
Because the sum function was not working I found I could convert it to a "numpy array" and then use "accumulate". I just can't get it to work and know I'm getting the error message 'DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.'
Could someone help me with the code?
from itertools import accumulate, islice
from numpy import array
import numpy as np
primes = []
pairs = []
numpy_pairs = np.asarray(pairs)
for num in range (4, 1000):
for j in range (2, num):
if (num % j) == 0:
break
else:
primes.append(num)
#for x in range(2,1000):
# if x in primes:
# print ("Ja, het getal {} komt voor in mijn primes".format(x))
for x in range(2,1000):
if x % 2 == 0:
for prime1 in primes:
for prime2 in primes:
if prime1 + prime2 == x and [prime1, prime2] not in numpy_pairs and [prime2, prime1] not in numpy_pairs:
np.append(numpy_pairs,[prime1,prime2])
results = ("{}+{}={}".format(i, j, k) for i, j in zip(numpy_pairs[0::2],
numpy_pairs[1::2]) for k in accumulate(islice(numpy_pairs,numpy_pairs.stop)))
print('\n'.join(results))
First things first, there are a lot of optimizations you can do to make the code logic better. the way you find primes can be improved, you only need to check numbers upto square root n to verify if n is a prime. Also, the actual verification of Goldbachs conjecture can be improved as well.
However, just focusing on the current code, You should stick to using lists if you want to append values, and to stop the code you need to use a sort of hack to stop the nested looping using the for-else syntax. Also, you can use f-strings to format strings nicely from python 3.6 onwards.
primes = []
pairs = []
for num in range (2, 1000): #modified. you forgot 2 and 3!
for j in range (2, num):
if (num % j) == 0:
break
else:
primes.append(num)
result = []
for x in range(2,1000):
if x % 2 == 0:
for prime1 in primes:
for prime2 in primes:
if prime1 + prime2 == x:
print(f"{x} = {prime1} + {prime2}")
result.append((prime1, prime2))
break
else: #this is a for-else syntax. enter this block if the for loop did not encounter a break
continue #go to next iteration of the mid-level loop. This prevents the line afterwards from being executed in cases where the inner loop did not "break"
break #break the mid level loop if you reach this line.
else:
print("You have done it! Bingo!!")