This will be much faster. (less than a second to check 44 trillion) It does fewer prime checks, usually less than 100, and does not check numbers that are even for prime. Building a sieve of 700k primes for goldbach(12,000,000) takes forever. This code makes 7 IsPrime() calls for 12 million.
With native Python Prime code it takes about .001 second to find answer for 1 Billion making 42 calls to IsPrime().
Three major differences from other IsPrime(n-p) programs:
Checks Goldbach prime 3 then uses 6k ± 1 for the rest. So 1/3 of the Goldbach checks vs every odd number > 3.
More efficient IsPrime() tests using every 6th odd number using 6k ± 1 as well.
Can use GMPY2 to check for prime. (takes less than .001 second for 25 digit number.
Solutions using a sieve get n*k slower as the number size (k) grows.
10,000,000 takes 2.5 seconds 100,000,000 takes 25 seconds and so on until memory is exhausted building the sieve. The call prime_check only as needed grows linearly (k) as k grows: 1,000,000,000,000 takes 0.037 seconds while 100,000,000,000,000 takes 0.450 seconds.
With GMPY2:
$ python3 goldbach.py 12345678912345678
This is a test of Goldbach's Conjecture that for all even integers
greater than 2 there are two primes that add up to that even number.
61 + 12,345,678,912,345,617 = 12,345,678,912,345,678
42 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 goldbach.py:16(goldbach)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {built-in method builtins.print}
37 0.000 0.000 0.000 0.000 {built-in method gmpy2.gmpy2.is_prime}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
$ python3 goldbach.py 4444444444444444444444
This is a test of Goldbach's Conjecture that for all even integers
greater than 2 there are two primes that add up to that even number.
257 + 4,444,444,444,444,444,444,187 = 4,444,444,444,444,444,444,444
144 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 goldbach.py:16(goldbach)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {built-in method builtins.print}
139 0.000 0.000 0.000 0.000 {built-in method gmpy2.gmpy2.is_prime}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
With my Home Grown IsPrime:
$ python3 goldbach.py 12345678912345678
This is a test of Goldbach's Conjecture that for all even integers
greater than 2 there are two primes that add up to that even number.
61 + 12,345,678,912,345,617 = 12,345,678,912,345,678
42 function calls in 3.869 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 3.869 3.869 <string>:1(<module>)
1 0.000 0.000 3.869 3.869 goldbach.py:16(goldbach)
37 3.869 0.105 3.869 0.105 goldbach.py:6(IsPrime)
1 0.000 0.000 3.869 3.869 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
$ python goldbach.py 1000000000000000
This is a test of Goldbach's Conjecture that for all even integers
greater than 2 there are two primes that add up to that even number.
11 + 999,999,999,999,989 = 1,000,000,000,000,000
14 function calls in 1.101 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.101 1.101 <string>:1(<module>)
1 0.000 0.000 1.101 1.101 goldbach.py:20(goldbach)
3 0.000 0.000 0.000 0.000 goldbach.py:28(<genexpr>)
6 1.101 0.184 1.101 0.184 goldbach.py:7(IsPrime)
1 0.000 0.000 1.101 1.101 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Listing:
# Goldbach's Conjecture tester.
from gmpy2 import is_prime
import sys
import cProfile
# or use home grown IsPrime
def IsPrime(n):
if (n <= 3): return n > 1
if not (n%6 == 1 or n%6 == 5): return False
for p,q in ((i,i+2) for i in range(5,int(n**.5)+1,6)):
if (n%p == 0 or n%q == 0): return False
return True
def goldbach(number):
if number == 4:
print("\n2 + 2 = 4\n")
return
elif IsPrime(number - 3):
print(f"\n3 + {number-3:,} = {number}\n")
return
else:
for p in range(5, number, 6): # just 6k±1 after 3
if IsPrime(p) and IsPrime(number-p):
print(f"\n{p:,} + {number-p:,} = {N:,}\n")
return
elif IsPrime(p+2) and IsPrime(number-(p+2)):
print(f"\n{p+2:,} + {number-(p+2):,} = {N:,}\n")
return
raise Exception(f"Found a counter-example to the Goldbach conjecture: {number}")
if __name__=="__main__":
N = 1
args = len(sys.argv)
if args > 1:
N = int(sys.argv[1])
print("This is a test of Goldbach's Conjecture that for all even integers")
print("greater than 2 there are two primes that add up to that even number.\n")
while (N < 3 or N%2):
N = int(input("Please enter an even number > 3 to check with Goldbach's Conjecture> "))
cProfile.run('goldbach(N)')
primenums
implemented? \$\endgroup\$