<?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; Factorization</title>
	<atom:link href="http://www.geekality.net/tag/factorization/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>Project Euler: Problem 5</title>
		<link>http://www.geekality.net/2009/09/21/project-euler-problem-5/</link>
		<comments>http://www.geekality.net/2009/09/21/project-euler-problem-5/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 20:20:01 +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>

		<guid isPermaLink="false">http://www.geekality.net/?p=411</guid>
		<description><![CDATA[2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20? Solution &#8230; <a href="http://www.geekality.net/2009/09/21/project-euler-problem-5/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.</p>
<p>What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?</p></blockquote>
<p><span id="more-411"></span></p>
<h2>Solution</h2>
<p>This one I solved fairly quickly. That is, I wrote a solution to it fairly quickly. However, that solution was <em>not</em> a quick one. It was a pure brute-force without much cleverness behind, and it took between 30 and 100 seconds, if I remember correctly, to run. Not good. It basically just took the numbers, one by one, and tried to divide them by all the dividers. If the number was not evenly divisible by a divisor, it skipped to the next number and so on until it found the one that was divisible by all of them.</p>
<p>However, with some reading around I found a better and faster solution.</p>
<p>It apparently uses something called Folding, but I haven&#8217;t quite managed to grasp what that means, so I will just give you my own explanation (which I think and hope is correct <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> ).</p>
<p>What we are looking for here is something called the <a href="http://en.wikipedia.org/wiki/Least_common_multiple">Lowest Common Multiple</a> (LCM). The LCM of all the divisors, 1 to 20, is the answer to this problem. To calculate the LCM of all those numbers we first need two methods. </p>
<p>The first is one that finds the LCM of two numbers. The formula for that, however, is based on another method which finds the <a href="http://en.wikipedia.org/wiki/Greatest_common_divisor">Greatest Common Divisor</a> (GCD) of two numbers. In other words we need to make that method first. </p>
<p>I implemented the GCD method using the <a href="http://en.wikipedia.org/wiki/Euclidean_algorithm">Euclidean algorithm</a>, and it goes 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"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">ulong</span> GetGreatestCommonDivisor<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> a, <span style="color: #6666cc; font-weight: bold;">ulong</span> b<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>b <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">ulong</span> t <span style="color: #008000;">=</span> b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #008000;">=</span> a<span style="color: #008000;">%</span>b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #008000;">=</span> t<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> a<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Now we can find the LCM.</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: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">ulong</span> GetLowestCommonMultiple<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> a, <span style="color: #6666cc; font-weight: bold;">ulong</span> b<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> a <span style="color: #008000;">/</span> GetGreatestCommonDivisor<span style="color: #008000;">&#40;</span>a, b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> b<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>With those two in place, we can then create a simple &#8220;expanded&#8221; version of that which can take an arbitrary number of divisors using a smart method called Aggregate.</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: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">ulong</span> GetLowestCommonMultiple<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> divisors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> divisors<span style="color: #008000;">.</span><span style="color: #0000FF;">Aggregate</span><span style="color: #008000;">&#40;</span>1UL, <span style="color: #008000;">&#40;</span>lcm, n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> GetLowestCommonMultiple<span style="color: #008000;">&#40;</span>lcm, n<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>It goes like this: It starts with 1 as the LCM and finds the LCM of 1 and the first divisor, n, and stores it in lcm. It then goes to the next divisor, and finds the LCM of that and the stored lcm, which is again stored in lcm. This is repeated until there are no more divisors, and we have the answer. As it turns out this method can also be simplified a bit because we can just give the LCM method to aggregate as a method group, instead of creating a lambda. So we end up with 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"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">ulong</span> GetLowestCommonMultiple<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> divisors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> divisors<span style="color: #008000;">.</span><span style="color: #0000FF;">Aggregate</span><span style="color: #008000;">&#40;</span>1UL, GetLowestCommonMultiple<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Quite a simple method, and we can now use that to find the answer to our original problem.</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> GetLowestCommonMultiple<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">11</span>, <span style="color: #FF0000;">12</span>, <span style="color: #FF0000;">13</span>, <span style="color: #FF0000;">14</span>, <span style="color: #FF0000;">15</span>, <span style="color: #FF0000;">16</span>, <span style="color: #FF0000;">17</span>, <span style="color: #FF0000;">18</span>, <span style="color: #FF0000;">19</span>, <span style="color: #FF0000;">20</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Notice that I skip the numbers 1 to 10, since they are already &#8220;covered&#8221; in the numbers 11 to 20. With this solution it doesn&#8217;t really make much difference in the time taken though.</p>
<p>According to the StopWatch, this solution takes 0 milliseconds, or 522 ticks. Quite an improvement from over 30 seconds!  <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='^_^' title='^_^' class='wp-smiley smiley-9' /> </p>
<p>While writing this post I also discovered that I, of course, could get rid of that first LCM method all together by doing the LCM of two numbers inline, and marking the parameter as params. So, the resulting LCM method becomes 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"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">ulong</span> GetLowestCommonMultiple<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> divisors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> divisors<span style="color: #008000;">.</span><span style="color: #0000FF;">Aggregate</span><span style="color: #008000;">&#40;</span>1UL, <span style="color: #008000;">&#40;</span>a, b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> a <span style="color: #008000;">/</span> GetGreatestCommonDivisor<span style="color: #008000;">&#40;</span>a, b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> b<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>And that method does the work for both. All my test cases still run. Love it <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='8)' title='8)' class='wp-smiley smiley-3' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/09/21/project-euler-problem-5/feed/</wfw:commentRss>
		<slash:comments>0</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>
