<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Many Hats of Jason Specland &#187; praxis</title>
	<atom:link href="http://www.jasonspecland.com/tag/praxis/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jasonspecland.com</link>
	<description>The mostly self-deprecating story of a programmer, performer, and daddy.</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:51:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Programming Praxis: Goldbach&#8217;s Conjecture</title>
		<link>http://www.jasonspecland.com/2010/03/02/programming-praxis-goldbachs-conjecture/</link>
		<comments>http://www.jasonspecland.com/2010/03/02/programming-praxis-goldbachs-conjecture/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 03:26:26 +0000</pubDate>
		<dc:creator>jason</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[praxis]]></category>

		<guid isPermaLink="false">http://www.jasonspecland.com/?p=72</guid>
		<description><![CDATA[Since I mentioned in my last post that I live in fear of the tech interview, I decided to do something about it rather than live in fear. To that end, I subscribed to a website called Programming Praxis, which &#8230; <a href="http://www.jasonspecland.com/2010/03/02/programming-praxis-goldbachs-conjecture/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since I mentioned in my last post that I live in fear of the tech interview, I decided to do something about it rather than live in fear.  To that end, I subscribed to a website called Programming Praxis, which publishes short programming problems to sharpen your saw on.  (Well, I&#8217;d actually been a subscriber for a while, but it&#8217;s just now that I&#8217;ve actually took the time to do my own saw sharpening.)</p>
<p>Geek Content Ahead.  Feel Free to Ignore.</p>
<p><a href="http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/">Today&#8217;s Praxis</a> is on the <a href="http://en.wikipedia.org/wiki/Goldbach_conjecture">Goldbach Conjecture</a> 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>
<p>I realize my solution is extremely naive, but it&#8217;s the first time in a long time that I&#8217;ve actually worked on programming problems other than &#8220;get data from database a, transform it somehow, and put it in database b.&#8221;  Also, for extra difficulty (and speed, although my algorithm is the real speed hangup in this case) I wrote it in C.</p>
<p>For the one or two people still reading, here is my extremely naive solution, cobbled together while watching West Side Story on TCM.</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>
<p><b>Edit:</b> See what happens when you&#8217;re watching TV when you&#8217;re doing your homework?  The flags marking numbers as prime can be a bool (or even a bit) and most certainly does <i>not</i> have to be an unsigned long.  Yay for massive, massive wastes of memory!  (But hey, I <i>was</i> watching West Side Story&#8230;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonspecland.com/2010/03/02/programming-praxis-goldbachs-conjecture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

