<?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>Geekality &#187; Primes</title>
	<atom:link href="http://www.geekality.net/tag/primes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:15:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>The Sieve of Atkin in C#</title>
		<link>http://www.geekality.net/2009/10/19/the-sieve-of-atkin-in-c/</link>
		<comments>http://www.geekality.net/2009/10/19/the-sieve-of-atkin-in-c/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 19:46:26 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Primes]]></category>
		<category><![CDATA[Sieve of Atkin]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=674</guid>
		<description><![CDATA[I have previously written about the Sieve of Eratosthenes, which is an algorithm for finding primes. This algorithm worked very well for most of the prime related Euler Problems. However, for one of them it just didn&#8217;t do it. Well, &#8230; <a href="http://www.geekality.net/2009/10/19/the-sieve-of-atkin-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I have previously written about the <a href="/?p=626">Sieve of Eratosthenes</a>, which is an algorithm for finding primes. This algorithm worked very well for most of the prime related Euler Problems. However, for one of them it just didn&#8217;t do it. Well, it did it, but it did it kind of slow. <a href="/?p=543" title="Problem 10">The problem</a> was to calculate the sum of all the primes below two million, and with that algorithm this took close to 2 seconds on my machine. Not too long you might think, but compared to my other solutions it is ages and ages. So I started to see if I could find a more efficient algorithm to use for that problem.</p>
<p>I quickly found one called the <a href="http://en.wikipedia.org/wiki/Sieve_of_Atkin">Sieve of Atkin</a>. The Atkin algorithm works similarly to the original Eratosthenes one, but has a more fancy way of sieving out numbers which means it can work a lot faster. However, this also means that it needs to work towards an upper limit and that it have to find all the primes up to that limit in advance. In other words it cannot work incrementally and <a href="http://en.wikipedia.org/wiki/Lazy_loading">lazily</a> like my Eratosthenes implementation.</p>
<p><span id="more-674"></span></p>
<p>Anyways, without further ado, here is my C# implementation of the Sieve of Atkin:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Atkin <span style="color: #008000;">:</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> primes<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">ulong</span> limit<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> Atkin<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> limit<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">limit</span> <span style="color: #008000;">=</span> limit<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; primes <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>primes<span style="color: #008000;">.</span><span style="color: #0000FF;">Any</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FindPrimes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var p <span style="color: #0600FF; font-weight: bold;">in</span> primes<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> p<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; IEnumerator IEnumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> FindPrimes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var isPrime <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#91;</span>limit <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var sqrt <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sqrt</span><span style="color: #008000;">&#40;</span>limit<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;=</span> sqrt<span style="color: #008000;">;</span> x<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> y <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> y <span style="color: #008000;">&lt;=</span> sqrt<span style="color: #008000;">;</span> y<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var n <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span> <span style="color: #008000;">*</span> x <span style="color: #008000;">*</span> x <span style="color: #008000;">+</span> y <span style="color: #008000;">*</span> y<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #008000;">&lt;=</span> limit <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>n <span style="color: #008000;">%</span> <span style="color: #FF0000;">12</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">||</span> n <span style="color: #008000;">%</span> <span style="color: #FF0000;">12</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isPrime<span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span> <span style="color: #008000;">^=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span> <span style="color: #008000;">*</span> x <span style="color: #008000;">*</span> x <span style="color: #008000;">+</span> y <span style="color: #008000;">*</span> y<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #008000;">&lt;=</span> limit <span style="color: #008000;">&amp;&amp;</span> n <span style="color: #008000;">%</span> <span style="color: #FF0000;">12</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">7</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isPrime<span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span> <span style="color: #008000;">^=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span> <span style="color: #008000;">*</span> x <span style="color: #008000;">*</span> x <span style="color: #008000;">-</span> y <span style="color: #008000;">*</span> y<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>x <span style="color: #008000;">&gt;</span> y <span style="color: #008000;">&amp;&amp;</span> n <span style="color: #008000;">&lt;=</span> limit <span style="color: #008000;">&amp;&amp;</span> n <span style="color: #008000;">%</span> <span style="color: #FF0000;">12</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">11</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isPrime<span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span> <span style="color: #008000;">^=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span> n <span style="color: #008000;">&lt;=</span> sqrt<span style="color: #008000;">;</span> n<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>isPrime<span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var s <span style="color: #008000;">=</span> n <span style="color: #008000;">*</span> n<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> k <span style="color: #008000;">=</span> s<span style="color: #008000;">;</span> k <span style="color: #008000;">&lt;=</span> limit<span style="color: #008000;">;</span> k <span style="color: #008000;">+=</span> s<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isPrime<span style="color: #008000;">&#91;</span>k<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; primes<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; primes<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span> n <span style="color: #008000;">&lt;=</span> limit<span style="color: #008000;">;</span> n<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>isPrime<span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primes<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>I am not going to explain my code. Instead I will just tell you to look at the <a href="http://en.wikipedia.org/wiki/Sieve_of_Atkin#Algorithm">algorithm at Wikipedia</a> and look at my code while reading the pseudocode provided there. Why? Because it is pretty much a direct copy of it, just translated into C# <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>As a usage example, we can sum up all the primes below a 1000 like so:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var sum <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Atkin<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Aggregate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>sum, x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> sum <span style="color: #008000;">+</span> x<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Fantastic huh? The even more fantastic part is that when I used this class instead of the Eratosthenes class to solve the problem I mentioned in the beginning, my solution went from taking almost 2 seconds down to around 160 milliseconds <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> </p>
<p>This is just a very straight forward implementation though, so if you have any suggestions to how it can be improved, made faster, cleaner, more efficient, et cetera, please let me know by leaving a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/10/19/the-sieve-of-atkin-in-c/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Sieve of Eratosthenes in C#</title>
		<link>http://www.geekality.net/2009/10/19/the-sieve-of-eratosthenes-in-c/</link>
		<comments>http://www.geekality.net/2009/10/19/the-sieve-of-eratosthenes-in-c/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 18:37:47 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Primes]]></category>
		<category><![CDATA[Sieve of Eratosthenes]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=626</guid>
		<description><![CDATA[In some of the Project Euler problems we have needed a source of primes. One algorithm for finding primes is called the Sieve of Eratosthenes. This algorithm is both pretty simple to understand and to implement. It is also fairly &#8230; <a href="http://www.geekality.net/2009/10/19/the-sieve-of-eratosthenes-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In <a href="/?p=176" title="Problem 3">some</a> <a href="/?p=496" title="Problem 7">of</a> <a href="/?p=543" title="Problem 10">the</a> <a href="http://projecteuler.net/">Project Euler</a> <a href="http://projecteuler.net/index.php?section=problems">problems</a> we have needed a source of <a href="http://en.wikipedia.org/wiki/Primes">primes</a>. One algorithm for finding primes is called the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>. This algorithm is both pretty simple to understand and to implement. It is also fairly fast and usable, at least for the lower primes.</p>
<p>My implementation is based upon the algorithm described on the Wikipedia page and some helpful optimizations I found in an article at <a href="http://www.blackwasp.co.uk/Eratosthenes.aspx">Black Wasp</a>. The differences from the original algorithm and the solution at Black Wasp is that it finds the primes incrementally and that it only looks for a new prime when asked.</p>
<p><span id="more-626"></span></p>
<p>My code should be pretty self explanatory, but the basic idea is that we have a list of known primes and for each odd number we check it against the these. If a number is not evenly divisible by any of the known primes, then we have found a new one and can add it to our list.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Eratosthenes <span style="color: #008000;">:</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> knownPrimes<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> Eratosthenes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; knownPrimes <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Return the ones we know first</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var prime <span style="color: #0600FF; font-weight: bold;">in</span> knownPrimes<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> prime<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Then find new ones</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var possible <span style="color: #008000;">=</span> primes<span style="color: #008000;">.</span><span style="color: #0000FF;">Last</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>IsPrime<span style="color: #008000;">&#40;</span>possible <span style="color: #008000;">+=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> possible<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; knownPrimes<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>possible<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; IEnumerator IEnumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsPrime<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> value<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sqrt <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&#41;</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sqrt</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">!</span>knownPrimes<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">TakeWhile</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&lt;=</span> sqrt<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Any</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> value<span style="color: #008000;">%</span>x <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span></div></div>
<p>How do you use it? Simple! For example, to print the first 500 primes you can do this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var primes <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Eratosthenes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Take</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">500</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span>var prime <span style="color: #0600FF; font-weight: bold;">in</span> primes<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>prime<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>And that&#8217;s all there is to it! What do you think? Let me know if you have any questions about it or any suggestions to how it can be improved. I want to know <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=';)' title=';)' class='wp-smiley smiley-20' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/10/19/the-sieve-of-eratosthenes-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 10</title>
		<link>http://www.geekality.net/2009/09/30/project-euler-problem-10/</link>
		<comments>http://www.geekality.net/2009/09/30/project-euler-problem-10/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 20:29:59 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Primes]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=543</guid>
		<description><![CDATA[The sum of the primes below 10 is Find the sum of all the primes below two million. Solution Using our previously made Eratosthenes class for generating primes, this problem can be solved in a very easy way. var answer &#8230; <a href="http://www.geekality.net/2009/09/30/project-euler-problem-10/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>The sum of the primes below 10 is </p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_591d27dfcf803317c601fb5b4b3ae096.gif" alt="2 + 3 + 5 + 7 = 17" title="2 + 3 + 5 + 7 = 17" style="vertical-align: -1px; border: none;"/></p></blockquote>
<p>Find the sum of all the primes below two million.</p></blockquote>
<p><span id="more-543"></span></p>
<h2>Solution</h2>
<p>Using our <a href="/?p=176">previously made</a> Eratosthenes class for generating primes, this problem can be solved in a very easy way.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var answer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Eratosthenes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">TakeWhile</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">2000000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Aggregate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>sum, x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> sum <span style="color: #008000;">+</span> x<span style="color: #008000;">&#41;</span></div></div>
<p>And that was it actually. The only problem here is that it takes a bit of time. On my machine this solution takes over 2 seconds. Not very long maybe, but compared to the others that is quite a bit of time actually. Especially when I know that it <em>could</em> go a lot faster if I just had a faster algorithm for generating prime numbers. However, I haven&#8217;t found that yet. Actually, I have, but I haven&#8217;t managed to implement any of them yet&#8230; <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<h2>Update October, 19th 2009</h2>
<p>Managed to finally implement another algorithm called the <a href="http://en.wikipedia.org/wiki/Sieve_of_Atkin">Sieve of Atkin</a>. I have written about it in <a href="/?p=674">a different post</a>.</p>
<p>Using that class, the solution to this problem looks like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var answer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Atkin<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2000000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Aggregate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>sum, x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> sum <span style="color: #008000;">+</span> x<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><span style="color: #008000;">&#41;</span></div></div>
<p>Pretty similar. But! The solution now takes only about 160 milliseconds, instead of the original 2 seconds <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> Is that cool or what?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/09/30/project-euler-problem-10/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 7</title>
		<link>http://www.geekality.net/2009/09/24/project-euler-problem-7/</link>
		<comments>http://www.geekality.net/2009/09/24/project-euler-problem-7/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 16:53:22 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Primes]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=496</guid>
		<description><![CDATA[By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? Solution This will be a very quick one thanks to the &#8230; <a href="http://www.geekality.net/2009/09/24/project-euler-problem-7/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.</p>
<p>What is the 10001st prime number?</p></blockquote>
<p><span id="more-496"></span></p>
<h2>Solution</h2>
<p>This will be a very quick one thanks to <a href="/?p=626">the prime number generator</a> created earlier for <a href="http://www.geekality.net/?p=176">problem 3</a>. All we need is actually this one line of code:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var answer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Eratosthenes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Take</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10001</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Last</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>And that&#8217;s it. It takes around 50 milliseconds, which is totally acceptable I would say. It can be sped up quite a bit though if we find a more efficient prime number generator. This, however, I will try to do in a later problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/09/24/project-euler-problem-7/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 3</title>
		<link>http://www.geekality.net/2009/09/17/project-euler-problem-3/</link>
		<comments>http://www.geekality.net/2009/09/17/project-euler-problem-3/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 21:26:36 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Factorization]]></category>
		<category><![CDATA[Primes]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=176</guid>
		<description><![CDATA[The third Euler problem has to do with prime factorization: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? Solution This one I struggled with a bit, cause &#8230; <a href="http://www.geekality.net/2009/09/17/project-euler-problem-3/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The third Euler problem has to do with prime factorization:</p>
<blockquote><p>The prime factors of 13195 are 5, 7, 13 and 29.</p>
<p>What is the largest prime factor of the number 600851475143?</p></blockquote>
<p><span id="more-176"></span></p>
<h2>Solution</h2>
<p>This one I struggled with a bit, cause I wasn&#8217;t sure exactly how to do this prime factorization. I knew what it was, and how to do it by hand, but wasn&#8217;t quite sure how to do it effective in code. Either way I ended up using something called the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>. Not the most efficient algorithm for this, if I have understood correctly, but it worked well enough for this use, and it was fairly straight forward to implement with some help from <a href="http://www.blackwasp.co.uk/Eratosthenes.aspx">Black</a> <a href="http://www.blackwasp.co.uk/PrimeFactors.aspx">Wasp</a>.</p>
<p>I started to make a prime number generator similar to the one I created for Fibonacci numbers in Problem 2.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IPrimeSequence <span style="color: #008000;">:</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Eratosthenes <span style="color: #008000;">:</span> IPrimeSequence<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> knownPrimes<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> Eratosthenes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; knownPrimes <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region IPrimeSequence Members</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Return the ones we know first</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var prime <span style="color: #0600FF; font-weight: bold;">in</span> knownPrimes<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> prime<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Then find new ones</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var possible <span style="color: #008000;">=</span> primes<span style="color: #008000;">.</span><span style="color: #0000FF;">Last</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>IsPrime<span style="color: #008000;">&#40;</span>possible <span style="color: #008000;">+=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> possible<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; knownPrimes<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>possible<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; IEnumerator IEnumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsPrime<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> value<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sqrt <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&#41;</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sqrt</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">!</span>knownPrimes<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">TakeWhile</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&lt;=</span> sqrt<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Any</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> value<span style="color: #008000;">%</span>x <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></div>
<p>It basically starts with the 2 first primes. And for each new requested prime number, it goes through the following odd numbers until it finds one that is not dividable by any of the primes ones we know so far.</p>
<p><em><strong>Note:</strong> I&#8217;ve created <a href="/?p=626">a separate post</a> about the Sieve of Eratosthenes, which might be updated with some optimizations, et cetera <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </em></p>
<p>With that prime number generator in place, finding prime factors is pretty straight forward here.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> GetPrimeFactors<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> value, IPrimeSequence primeSequence<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var prime <span style="color: #0600FF; font-weight: bold;">in</span> primeSequence<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>value<span style="color: #008000;">%</span>prime <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="color: #008000;">/=</span> prime<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> prime<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></div>
<p>Now, that is pretty much what is needed to solve this problem. A simple LINQ statement finds the answer.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; var answer <span style="color: #008000;">=</span> GetPrimeFactors<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">600851475143</span>, <span style="color: #008000;">new</span> Eratosthenes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Max</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Tadaa <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/09/17/project-euler-problem-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
