Skip to main content
edited body
Source Link
# Goldbach's Conjecture tester. from gmpy2 import is_prime import sys import cProfile   # or use home grown IsPrime def IsPrime(n): if (n<=1): return False if (n <= 3): return True  if (n %> 21 == 0 or n % 3 ==   0):    if not (n%6 == 1 or n%6 == 5): return False   # use 6k   ± 1 for the rest  stopp,q =in int(n**.5(i,i+2)+1  for i  in range(5, stopint(n**.5)+1, 6)):   if (n % in%p == 0 or n % (i + 2)n%q == 0):    return False 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 odds6k±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)') 
# Goldbach's Conjecture tester. from gmpy2 import is_prime import sys import cProfile # or use home grown IsPrime def IsPrime(n): if (n<=1): return False if (n <= 3): return True  if (n % 2 == 0 or n % 3 == 0):    return False   # use 6k ± 1 for the rest  stop = int(n**.5)+1  for i  in range(5, stop, 6):   if (n % i == 0 or n % (i + 2) == 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 odds 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)') 
# 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)') 
added 96 characters in body
Source Link
 $ 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}  $ python3python goldbach.py 10000000001000000000000000 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. 7111 + 999,999,929999,999,989 = 1,000,000,000,000,000 4714 function calls in 01.001101 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 01.001101 01.001101 <string>:1(<module>) 1 0.000 0.000 01.001101 01.001101 goldbach.py:1620(goldbach) 42 3 0.001000 0.000 0.001000 0.000 goldbach.py:28(<genexpr>) 6 1.101 0.184 1.101 0.184 goldbach.py:7(IsPrime) 1 0.000 0.000 01.001101 01.001101 {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} 
 $ 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}  $ python3 goldbach.py 1000000000 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. 71 + 999,999,929 = 1,000,000,000 47 function calls in 0.001 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.001 0.001 <string>:1(<module>) 1 0.000 0.000 0.001 0.001 goldbach.py:16(goldbach) 42 0.001 0.000 0.001 0.000 goldbach.py:6(IsPrime) 1 0.000 0.000 0.001 0.001 {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} 
 $ 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} 
added 45 characters in body
Source Link

Only checks every 6th and 7th odd number forChecks Goldbach prime 3 then uses 6k ± 1 for the rest. SoSo 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.

# Goldbach's Conjecture tester. from gmpy2 import is_prime import sys import cProfile # or use home grown IsPrime def IsPrime(n): if (n<=1): return False  if (n == 2 or n ==<= 3):  return True   if (n <= 1 or n % 2 == 0 or n % 3 == 0):   return False # use 6k ± 1 for ithe rest  in range(5, stop = int(n**.5)+1,   6):   for i in range(5, stop, 6):    if (n % i == 0 or n % (i + 2) == 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 odds 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)') 

Only checks every 6th and 7th odd number for prime. So 1/3 of the checks.

More efficient IsPrime() tests using every 6th odd number as well.

# Goldbach's Conjecture tester. from gmpy2 import is_prime import sys import cProfile # or use home grown IsPrime def IsPrime(n): if (n == 2 or n == 3):  return True   if (n <= 1 or n % 2 == 0 or n % 3 == 0):   return False for i in range(5, int(n**.5)+1, 6):    if (n % i == 0 or n % (i + 2) == 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 odds 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)') 

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.

# Goldbach's Conjecture tester. from gmpy2 import is_prime import sys import cProfile # or use home grown IsPrime def IsPrime(n): if (n<=1): return False  if (n <= 3): return True if (n % 2 == 0 or n % 3 == 0): return False # use 6k ± 1 for the rest  stop = int(n**.5)+1   for i in range(5, stop, 6):    if (n % i == 0 or n % (i + 2) == 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 odds 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)') 
added 210 characters in body
Source Link
Loading
added 155 characters in body
Source Link
Loading
Added some feature infomation to better answer the original question.
Source Link
Loading
added 82 characters in body
Source Link
Loading
added 2299 characters in body
Source Link
Loading
Post Undeleted by Andy Richter
Post Deleted by Andy Richter
added 340 characters in body
Source Link
Loading
added 350 characters in body
Source Link
Loading
added 24 characters in body
Source Link
Loading
added 4 characters in body
Source Link
Loading
added 297 characters in body
Source Link
Loading
added 56 characters in body
Source Link
Loading
added 43 characters in body
Source Link
Loading
added 155 characters in body
Source Link
Loading
deleted 16 characters in body
Source Link
Loading
Source Link
Loading