Goldbach’s Conjecture
March 2, 2010
Christian Goldbach (1690-1764) was a Prussian mathematician and contemporary of Euler. One of the most famous unproven conjectures in number theory is known as Goldbach’s Conjecture, which states that every even number greater than two is the sum of two prime numbers; for example, 28 = 5 + 23.
Your task is to write a function that finds the two primes that add to a given even number greater than two. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
[…] Praxis – Goldbach’s Conjecture By Remco Niemeijer In today’s Programming Praxis problem we have to test Golbach’s conjecture that every even number […]
My Haskell solution (see http://bonsaicode.wordpress.com/2010/03/02/programming-praxis-goldbach%E2%80%99s-conjecture/ for a version with comments):
Here’s my quick and naive C implementation.
[…] Today’s Praxis is on the Goldbach Conjecture which states that any even number greater than 2 can be expressed as the sum of two primes. The challenge is to write a program that will take in an even number, and spit out the two primes that can be added together to make it. […]
D’oh! The prime flags of the sieve should obviously be bool, and not ulong. Way to waste memory there, Jason. :)
Here’s a managed code solution. No sieve is used, because it seemed like that would be a waste in most cases. A more optimized IsPrime() function could be swapped in.
public static class Goldbach
{
private static List primes = new List() { 2, 3 };
public static void GetGoldbachPrimes(int value, out int prime1, out int prime2)
{
Debug.Assert(value > 2 && value % 2 == 0, “value > 2 && value % 2 == 0”);
if (value <= 2 || value % 2 != 0)
{
throw new ArgumentException("value must be even number greater than 2", "value");
}
foreach (var prime in GetPrimes())
{
int difference = value – prime;
if (IsPrime(difference))
{
prime1 = prime;
prime2 = difference;
return;
}
}
throw new InvalidOperationException("Primes could not be found");
}
private static IEnumerable GetPrimes()
{
for (int i = 0; i < primes.Count; i++)
{
yield return primes[i];
}
for (int i = primes[primes.Count – 1] + 2; i < int.MaxValue; i += 2)
{
if (IsPrime(i))
{
primes.Add(i);
yield return i;
}
}
}
private static bool IsPrime(int value)
{
if (value = 0)
{
return true;
}
int sqrt = Convert.ToInt32(Math.Ceiling(Math.Sqrt(value)));
for (int i = 3; i <= sqrt; i += 2)
{
if (value % i == 0)
{
return false;
}
}
return true;
}
}
I just came across this site and had done some related work with GC, in terms of symmetric prime pair solutions
eg for 24, we have (11/13), (7/17),(5/19) as solutions, where each pair is symmetric about N/2=12
it uses Pari/GP built-in function is ispseudoprime
mirrors(N)=
{
\\ N is the number of interest to find symmetric prime pairs
\\ written as pari/GP script
i=1;cnt=0;Q1=999;
if(N%2!=0|N5! “);return(2) );
print(“selected N = “,N);
while(Q1>5,
Q1=N-i;Q2=N+i;
if(ispseudoprime(Q1)&&ispseudoprime(Q2),
\\## print(” mirror pair at “,Q1,” / “,Q2); \\disable for speed or if N> BB1
cnt++ ); \\ end IF
i=i+2; \\ skip multiples if 5! for speed
if(i%5==0,i=i+2) \\ BB1
); \\ end WHILE
print(“# pairs found = “,cnt);
print(“other candidate N: 6,12,30,60,180,210,360,420,1260,2310,2520,4620,”);
return(0);
}
Two python versions.
For many values of n, it is faster to generate all the primes less than n than it is to generate each prime (p) up to n/2 and test whether n-p is prime. So the second routine runs faster than the first.
Reuses is_prime and primes_to from earlier problems.
Not as elegant but I think this will do for now.
Sample output:
ruby solution (http://codepad.org/So9bEmlF)
import math
from random import choice
from fibonacci_conjecture import is_perfect_square, is_prime # module here -> https://programmingpraxis.com/2015/01/23/fibonacci-conjecture/#comment-50883
def is_even(x):
return x%2==0
def gc_find_primes(x, printall=False):
if not is_even(x): return None
if x<=2: return None
goldbach_pairs = []
for i in range(x):
if is_prime(i) and is_prime(x-i):
goldbach_pairs.append((i, x-i))
if printall: return goldbach_pairs[:len(goldbach_pairs)/2]
else: return choice(goldbach_pairs)
tag typo in previous comment