<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Goldbach&#8217;s Conjecture</title>
	<atom:link href="http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Mike</title>
		<link>http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/#comment-1203</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Thu, 29 Apr 2010 00:07:26 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2041#comment-1203</guid>
		<description><![CDATA[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.

[sourcecode lang=&quot;python&quot;]
from primes import primes_to, is_prime

def prime_pairs1( x ):
    return ((p,x-p) for p in primes_to(x/2) if is_prime( x-p ))


def prime_pairs2( x ):
    prime = list( primes_to( x ) )
    
    lo, hi = 0, len(prime)-1
    while lo &lt;= hi:
	n = prime[hi] + prime[lo]
	if n &lt; x:
		lo += 1
	elif n == x:
		yield prime[lo], prime[hi]
		lo += 1
	else:
		hi -= 1
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Two python versions.</p>
<p>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.</p>
<p>Reuses is_prime and primes_to from earlier problems.</p>
<pre class="brush: python;">
from primes import primes_to, is_prime

def prime_pairs1( x ):
    return ((p,x-p) for p in primes_to(x/2) if is_prime( x-p ))

def prime_pairs2( x ):
    prime = list( primes_to( x ) )

    lo, hi = 0, len(prime)-1
    while lo &lt;= hi:
	n = prime[hi] + prime[lo]
	if n &lt; x:
		lo += 1
	elif n == x:
		yield prime[lo], prime[hi]
		lo += 1
	else:
		hi -= 1
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill McEachen</title>
		<link>http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/#comment-1071</link>
		<dc:creator><![CDATA[Bill McEachen]]></dc:creator>
		<pubDate>Sun, 14 Mar 2010 17:57:21 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2041#comment-1071</guid>
		<description><![CDATA[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&#124;N5! &quot;);return(2) );

   print(&quot;selected N = &quot;,N);

while(Q1&gt;5,

   Q1=N-i;Q2=N+i;

   if(ispseudoprime(Q1)&amp;&amp;ispseudoprime(Q2),
\\## print(&quot; mirror pair at &quot;,Q1,&quot; /  &quot;,Q2);  \\disable for speed or if N&gt;      BB1
cnt++ );  \\ end IF

   i=i+2;    \\ skip multiples if 5! for speed
   if(i%5==0,i=i+2)                      \\ BB1

);   \\ end WHILE

   print(&quot;# pairs found = &quot;,cnt);
   print(&quot;other candidate N:  6,12,30,60,180,210,360,420,1260,2310,2520,4620,&quot;);

   return(0);
}]]></description>
		<content:encoded><![CDATA[<p>I just came across this site and had done some related work with GC, in terms of symmetric prime pair solutions<br />
eg for 24, we have (11/13), (7/17),(5/19) as solutions, where each pair is symmetric about N/2=12<br />
it uses Pari/GP built-in function is ispseudoprime</p>
<p>mirrors(N)=<br />
{<br />
\\ N is the number of interest to find symmetric prime pairs<br />
\\   written as pari/GP script<br />
  i=1;cnt=0;Q1=999;</p>
<p>   if(N%2!=0|N5! &#8220;);return(2) );</p>
<p>   print(&#8220;selected N = &#8220;,N);</p>
<p>while(Q1&gt;5,</p>
<p>   Q1=N-i;Q2=N+i;</p>
<p>   if(ispseudoprime(Q1)&amp;&amp;ispseudoprime(Q2),<br />
\\## print(&#8221; mirror pair at &#8220;,Q1,&#8221; /  &#8220;,Q2);  \\disable for speed or if N&gt;      BB1<br />
cnt++ );  \\ end IF</p>
<p>   i=i+2;    \\ skip multiples if 5! for speed<br />
   if(i%5==0,i=i+2)                      \\ BB1</p>
<p>);   \\ end WHILE</p>
<p>   print(&#8220;# pairs found = &#8220;,cnt);<br />
   print(&#8220;other candidate N:  6,12,30,60,180,210,360,420,1260,2310,2520,4620,&#8221;);</p>
<p>   return(0);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/#comment-1060</link>
		<dc:creator><![CDATA[Dave]]></dc:creator>
		<pubDate>Fri, 05 Mar 2010 15:10:44 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2041#comment-1060</guid>
		<description><![CDATA[Here&#039;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 &gt; 2 &amp;&amp; value % 2 == 0, &quot;value &gt; 2 &amp;&amp; value % 2 == 0&quot;);
            if (value &lt;= 2 &#124;&#124; value % 2 != 0)
            {
                throw new ArgumentException(&quot;value must be even number greater than 2&quot;, &quot;value&quot;);
            }

            foreach (var prime in GetPrimes())
            {
                int difference = value - prime;
                if (IsPrime(difference))
                {
                    prime1 = prime;
                    prime2 = difference;
                    return;
                }
            }

            throw new InvalidOperationException(&quot;Primes could not be found&quot;);
        }

        private static IEnumerable GetPrimes()
        {
            for (int i = 0; i &lt; primes.Count; i++)
            {
                yield return primes[i];
            }

            for (int i = primes[primes.Count - 1] + 2; i &lt; 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 &lt;= sqrt; i += 2)
            {
                if (value % i == 0)
                {
                    return false;
                }
            }

            return true;
        }
    }]]></description>
		<content:encoded><![CDATA[<p>Here&#8217;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.</p>
<p>    public static class Goldbach<br />
    {<br />
        private static List primes = new List() { 2, 3 };</p>
<p>        public static void GetGoldbachPrimes(int value, out int prime1, out int prime2)<br />
        {<br />
            Debug.Assert(value &gt; 2 &amp;&amp; value % 2 == 0, &#8220;value &gt; 2 &amp;&amp; value % 2 == 0&#8243;);<br />
            if (value &lt;= 2 || value % 2 != 0)<br />
            {<br />
                throw new ArgumentException(&quot;value must be even number greater than 2&quot;, &quot;value&quot;);<br />
            }</p>
<p>            foreach (var prime in GetPrimes())<br />
            {<br />
                int difference = value &#8211; prime;<br />
                if (IsPrime(difference))<br />
                {<br />
                    prime1 = prime;<br />
                    prime2 = difference;<br />
                    return;<br />
                }<br />
            }</p>
<p>            throw new InvalidOperationException(&quot;Primes could not be found&quot;);<br />
        }</p>
<p>        private static IEnumerable GetPrimes()<br />
        {<br />
            for (int i = 0; i &lt; primes.Count; i++)<br />
            {<br />
                yield return primes[i];<br />
            }</p>
<p>            for (int i = primes[primes.Count - 1] + 2; i &lt; int.MaxValue; i += 2)<br />
            {<br />
                if (IsPrime(i))<br />
                {<br />
                    primes.Add(i);<br />
                    yield return i;<br />
                }<br />
            }<br />
        }</p>
<p>        private static bool IsPrime(int value)<br />
        {<br />
            if (value = 0)<br />
            {<br />
                return true;<br />
            }</p>
<p>            int sqrt = Convert.ToInt32(Math.Ceiling(Math.Sqrt(value)));</p>
<p>            for (int i = 3; i &lt;= sqrt; i += 2)<br />
            {<br />
                if (value % i == 0)<br />
                {<br />
                    return false;<br />
                }<br />
            }</p>
<p>            return true;<br />
        }<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/#comment-1054</link>
		<dc:creator><![CDATA[Jason]]></dc:creator>
		<pubDate>Wed, 03 Mar 2010 12:36:14 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2041#comment-1054</guid>
		<description><![CDATA[D&#039;oh!  The prime flags of the sieve should obviously be bool, and not ulong.  Way to waste memory there, Jason.  :)]]></description>
		<content:encoded><![CDATA[<p>D&#8217;oh!  The prime flags of the sieve should obviously be bool, and not ulong.  Way to waste memory there, Jason.  :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: The Many Hats of Jason Specland &#187; Programming Praxis: Goldbach&#8217;s Conjecture</title>
		<link>http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/#comment-1053</link>
		<dc:creator><![CDATA[The Many Hats of Jason Specland &#187; Programming Praxis: Goldbach&#8217;s Conjecture]]></dc:creator>
		<pubDate>Wed, 03 Mar 2010 03:26:33 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2041#comment-1053</guid>
		<description><![CDATA[[...] Today&#8217;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. [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Today&#8217;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. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/#comment-1052</link>
		<dc:creator><![CDATA[Jason]]></dc:creator>
		<pubDate>Wed, 03 Mar 2010 03:08:12 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2041#comment-1052</guid>
		<description><![CDATA[Here&#039;s my quick and naive C implementation.

[sourcecode language=&quot;cpp&quot;]
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

void show_usage();
unsigned long *create_sieve_to_number(unsigned long number);

int main (int argc, const char * argv[]) {

    if (argc != 2) {
		show_usage();
		return -1;
	}
	
	
	unsigned long number = atol(argv[1]);
	
	if ((number % 2) != 0) {
		show_usage();
		return -1;
	}
	
	unsigned long *sieve = create_sieve_to_number(number);
	
	for (unsigned long i = 2; i &lt; number; i++) {
		if (sieve[i] == 1) {
			for (unsigned long j = i; j &lt; number; j++) {
				if (sieve[j] == 1) {
					if (i + j == number) {
						printf(&quot;Solution found: %d + %d\n&quot;, i, j);
						return 0;
					}
				}
			}
		}
	}
	
	printf(&quot;no solution found!  pick up your Fields Medal!\n&quot;);
	
		
	return 0;
	
}

void show_usage(void) {
	printf(&quot;usage: goldbach [even number]\n&quot;);
}

unsigned long *create_sieve_to_number(unsigned long number) {
	unsigned long *sieve;
	
	sieve = (unsigned long *)malloc(sizeof(unsigned long) * (number + 1));
	
	for (int i = 0; i &lt; number; i++) {
		sieve[i] = 1;
	}
	
	for (unsigned long i = 2; i &lt; number; i++) {
		if (sieve[i] == 1) {
			for (unsigned long j = i * i; j &lt; number; j = j + i) {
				sieve[j] = 0;
			}
		}
	}
	
	return sieve;
}
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Here&#8217;s my quick and naive C implementation.</p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

void show_usage();
unsigned long *create_sieve_to_number(unsigned long number);

int main (int argc, const char * argv[]) {

    if (argc != 2) {
		show_usage();
		return -1;
	}

	unsigned long number = atol(argv[1]);

	if ((number % 2) != 0) {
		show_usage();
		return -1;
	}

	unsigned long *sieve = create_sieve_to_number(number);

	for (unsigned long i = 2; i &lt; number; i++) {
		if (sieve[i] == 1) {
			for (unsigned long j = i; j &lt; number; j++) {
				if (sieve[j] == 1) {
					if (i + j == number) {
						printf(&quot;Solution found: %d + %d\n&quot;, i, j);
						return 0;
					}
				}
			}
		}
	}

	printf(&quot;no solution found!  pick up your Fields Medal!\n&quot;);

	return 0;

}

void show_usage(void) {
	printf(&quot;usage: goldbach [even number]\n&quot;);
}

unsigned long *create_sieve_to_number(unsigned long number) {
	unsigned long *sieve;

	sieve = (unsigned long *)malloc(sizeof(unsigned long) * (number + 1));

	for (int i = 0; i &lt; number; i++) {
		sieve[i] = 1;
	}

	for (unsigned long i = 2; i &lt; number; i++) {
		if (sieve[i] == 1) {
			for (unsigned long j = i * i; j &lt; number; j = j + i) {
				sieve[j] = 0;
			}
		}
	}

	return sieve;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/#comment-1048</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Tue, 02 Mar 2010 10:27:15 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2041#comment-1048</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2010/03/02/programming-praxis-goldbach%E2%80%99s-conjecture/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
import Data.Numbers.Primes

goldbach :: Integer -&gt; (Integer, Integer)
goldbach n = head [(p, n - p) &#124; p &lt;- takeWhile (&lt; n) primes, isPrime (n - p)]
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2010/03/02/programming-praxis-goldbach%E2%80%99s-conjecture/" rel="nofollow">http://bonsaicode.wordpress.com/2010/03/02/programming-praxis-goldbach%E2%80%99s-conjecture/</a> for a version with comments):</p>
<pre class="brush: css;">
import Data.Numbers.Primes

goldbach :: Integer -&gt; (Integer, Integer)
goldbach n = head [(p, n - p) | p &lt;- takeWhile (&lt; n) primes, isPrime (n - p)]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Goldbach’s Conjecture &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/#comment-1047</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Goldbach’s Conjecture &#171; Bonsai Code]]></dc:creator>
		<pubDate>Tue, 02 Mar 2010 10:26:51 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2041#comment-1047</guid>
		<description><![CDATA[[...] Praxis &#8211; Goldbach’s&#160;Conjecture By Remco Niemeijer  In today&#8217;s Programming Praxis problem we have to test Golbach&#8217;s conjecture that every even number [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Goldbach’s&nbsp;Conjecture By Remco Niemeijer  In today&#8217;s Programming Praxis problem we have to test Golbach&#8217;s conjecture that every even number [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

