Reputation: 711
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))
Upvotes: 0
Views: 202
Reputation: 189
I didn't use numpy Here:
n = 1000
primes = [2,3]
pairs = []
for num in range (5, n+1,2):
if num%6 not in [1,5]: continue
for j in range (5, int(num**.5)+1,6):
if num%j == 0 or num%(j+2) == 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(4,n+1,2):
for prime1 in primes:
if prime1 >= x//2+1: break
prime2 = x-prime1
if prime2 in primes: # got one
pairs += [(prime1,prime2)]
print(*[f"{i}+{j}={i+j}\n" for i, j in pairs])
Output:
2+2=4
3+3=6
3+5=8
3+7=10
5+5=10
5+7=12
3+11=14
7+7=14
3+13=16
5+11=16
5+13=18
7+11=18
...
257+743=1000
281+719=1000
317+683=1000
347+653=1000
353+647=1000
359+641=1000
383+617=1000
401+599=1000
431+569=1000
443+557=1000
479+521=1000
491+509=1000
Upvotes: 0
Reputation: 6246
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!!")
Upvotes: 0