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)')