<?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; Project Euler</title>
	<atom:link href="http://www.geekality.net/category/project-euler/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 25</title>
		<link>http://www.geekality.net/2009/11/06/project-euler-problem-25/</link>
		<comments>http://www.geekality.net/2009/11/06/project-euler-problem-25/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 22:51:56 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Big Int]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fibonacci]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=751</guid>
		<description><![CDATA[The Fibonacci sequence is defined by the recurrence relation: , where and . Hence the first 12 terms will be: &#8230; The 12th term, , is the first term to contain three digits. What is the first term in the &#8230; <a href="http://www.geekality.net/2009/11/06/project-euler-problem-25/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>The Fibonacci sequence is defined by the recurrence relation:<br />
<img src="http://quicklatex.com/cache/ql_0fdba176bee77c46041880def6869c26.gif" alt="F_n = F_{n-1} + F_{n-2}" title="F_n = F_{n-1} + F_{n-2}" style="vertical-align: -4px; border: none;"/>, where <img src="http://quicklatex.com/cache/ql_9abbbedef650f272f894f1980cfc726d.gif" alt="F_1 = 1" title="F_1 = 1" style="vertical-align: -4px; border: none;"/> and <img src="http://quicklatex.com/cache/ql_c25d7581be770023ed580dd28a11e2ff.gif" alt="F_2 = 1" title="F_2 = 1" style="vertical-align: -3px; border: none;"/>.</p>
<p>Hence the first 12 terms will be:</p>
<ul>
<li><img src="http://quicklatex.com/cache/ql_a286990398936a23b6a69c6dc468f5f3.gif" alt="F_1=1" title="F_1=1" style="vertical-align: -4px; border: none;"/></li>
<li><img src="http://quicklatex.com/cache/ql_43740a8477bf58183df88835dae8b0c3.gif" alt="F_2=1" title="F_2=1" style="vertical-align: -3px; border: none;"/></li>
<li>&#8230;</li>
<li><img src="http://quicklatex.com/cache/ql_fb13cc1a61dde7cb48f20e8330b12e46.gif" alt="F_{11}=89" title="F_{11}=89" style="vertical-align: -4px; border: none;"/></li>
<li><img src="http://quicklatex.com/cache/ql_84019eaa270ede73f5285cc8dbb74e98.gif" alt="F_{12}=144" title="F_{12}=144" style="vertical-align: -4px; border: none;"/></li>
</ul>
<p>The 12th term, <img src="http://quicklatex.com/cache/ql_ada838adeba1adb6961337c649fb8d67.gif" alt="F_{12}" title="F_{12}" style="vertical-align: -4px; border: none;"/>, is the first term to contain three digits.</p>
<p>What is the first term in the Fibonacci sequence to contain 1000 digits?</p></blockquote>
<p><span id="more-751"></span></p>
<h2>Solution</h2>
<p>We touched on the Fibonacci sequence a while ago in the <a href="/?p=165">second euler problem</a>. This time, however, we are in a whole different league. That time we were supposed to sum up all even Fibonacci numbers below 4 million, which might sound like a lot, but not when we compare it to the number we are asked for this time! The last Fibonacci number below 4 million is 3524578. That would be 7 digits. The maximum value of the datatype I used in the generator I made for that (ulong) is 18446744073709551615. That would be 20 digits. Quite a distance left to a thousand!</p>
<p>So, once again we turn to our big integer class, <a href="http://intx.codeplex.com/">IntX</a>. And since we have that, a solution to this problem is actually pretty straight forward. We simply have to make a new generator that uses the IntX data type instead of tiny ulong (it&#8217;s all relative&#8230;).</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> LargeFibonacciSequence <span style="color: #008000;">:</span> IEnumerable<span style="color: #008000;">&lt;</span>IntX<span style="color: #008000;">&gt;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>IntX<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; var a <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var b <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</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;">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; <span style="color: #008000;">&#123;</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> a<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var c <span style="color: #008000;">=</span> a <span style="color: #008000;">+</span> b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #008000;">=</span> b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #008000;">=</span> c<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><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 />
<span style="color: #008000;">&#125;</span></div></div>
<p>The straight forward solution should then be fairly obvious.</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 n <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
var sequence <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LargeFibonacciSequence<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var e <span style="color: #008000;">=</span> sequence<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><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>e<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveNext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; n<span style="color: #008000;">++;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
var answer <span style="color: #008000;">=</span> n<span style="color: #008000;">;</span></div></div>
<p>That pretty much just moves through one Fibonacci number after the other, increasing <code class="codecolorer text default"><span class="text">n</span></code> as it goes, until it reaches the first one that has 1000 or more digits.</p>
<p>Very brute-force, not very fast (takes around 500 ms) and not very interesting, but that&#8217;s pretty much it&#8230; ooor is it?</p>
<h2>Take two!</h2>
<p>Alright, while reading up on Fibonacci numbers I found some interesting mathematical formulas and such. I&#8217;m not going to come with an in-depth explanation or lots proofs here, but I will share the formulas that matters and go through how we use them. If you want the elaborate explanations you can find them where smarter people than I reign. For example at <a href="http://en.wikipedia.org/wiki/Fibonacci_series">Wikipedia</a>, or at <a href="http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html">this site</a>. Anyways, here we go. Take cover if you fear math&#8230;</p>
<h3>The Fibonacci sequence</h3>
<p>The <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/>th number in the Fibonacci sequence, <img src="http://quicklatex.com/cache/ql_f67871cd00ac973d0e2b80db93f3bcd3.gif" alt="F_n" title="F_n" style="vertical-align: -3px; border: none;"/>, is defined by the recurrence relation</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_0fdba176bee77c46041880def6869c26.gif" alt="F_n = F_{n-1} + F_{n-2}" title="F_n = F_{n-1} + F_{n-2}" style="vertical-align: -4px; border: none;"/></p></blockquote>
<p>with the seed values <img src="http://quicklatex.com/cache/ql_0aeae6a4e96c1dbcf45b521e505c7569.gif" alt="F_0=0" title="F_0=0" style="vertical-align: -3px; border: none;"/> and <img src="http://quicklatex.com/cache/ql_9abbbedef650f272f894f1980cfc726d.gif" alt="F_1 = 1" title="F_1 = 1" style="vertical-align: -4px; border: none;"/>.</p>
<p>This recurrence, since it is linear, can be expressed by a <a href="http://en.wikipedia.org/wiki/Closed-form_expression">closed-form solution</a> known as the Binet&#8217;s formula:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_0ee0f9cdc2a0daeb61a218d0edf60f26.gif" alt="Fib(n)=\dfrac{\varphi^n-(1-\varphi)^n}{\sqrt{5}}=\dfrac{\varphi^n-(\frac{-1}{\varphi})^n}{\sqrt{5}}" title="Fib(n)=\dfrac{\varphi^n-(1-\varphi)^n}{\sqrt{5}}=\dfrac{\varphi^n-(\frac{-1}{\varphi})^n}{\sqrt{5}}" style="vertical-align: -17px; border: none;"/><br />
<img src="http://quicklatex.com/cache/ql_68252e18a2b40c61e0c66506189f3cc7.gif" alt="\varphi=\dfrac{1+\sqrt{5}}{2}\approx1.6180339887\ldots" title="\varphi=\dfrac{1+\sqrt{5}}{2}\approx1.6180339887\ldots" style="vertical-align: -12px; border: none;"/> &#8212; (The Golden ratio)</p></blockquote>
<p>Now, why <img src="http://quicklatex.com/cache/ql_c9dae022cefb2682530b2aa3885538b9.gif" alt="1-\varphi" title="1-\varphi" style="vertical-align: -4px; border: none;"/> and <img src="http://quicklatex.com/cache/ql_b828f2117f6246a0460a91c0cde036e7.gif" alt="\frac{-1}{\varphi}" title="\frac{-1}{\varphi}" style="vertical-align: -9px; border: none;"/> is considered the same, or even why that formula works or looks like that, I will leave as an exercise for the reader (Cause I have no idea <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Please share if you do&#8230;). But anyways, as an example, if you put 20 into that formula, you will get 6765 which is the 20th number in the Fibonacci sequence. Fantastic. We could now use that formula to make another brute-force solution to find the term we are after. But that wouldn&#8217;t make much sense and would be much much slower. So, we must move on further into this crazy land.</p>
<p>Before we move on to the next part, since we will be working with quite large values of <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/>, we can simplify that expression a bit. That is because <img src="http://quicklatex.com/cache/ql_69e8045f0432b985de5f2b55c21140e8.gif" alt="\frac{-1}{\varphi}^n" title="\frac{-1}{\varphi}^n" style="vertical-align: -9px; border: none;"/> will move pretty fast towards 0 as <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/> increases. In other words, we can pretty much just skip that part and use the following formula instead:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_7c09cd950e24a31d6319adfd2e1693e5.gif" alt="Fib(n)=\dfrac{\varphi^n}{\sqrt{5}}" title="Fib(n)=\dfrac{\varphi^n}{\sqrt{5}}" style="vertical-align: -17px; border: none;"/></p></blockquote>
<h3>The length of a number</h3>
<p>How do you calculate the length of a number? I actually had to do this in an earlier post and then I kind of cheated. I just converted the number into a string and just count the characters (In C#, <code class="codecolorer text default"><span class="text">string.Length</span></code>). But, I recently discovered that there actually is a way you can do this mathematically! I had no idea&#8230; The key lays in the <a href="http://en.wikipedia.org/wiki/Log10">logarithm to base 10</a>.</p>
<p><img src="http://quicklatex.com/cache/ql_5e4f1016aaa4ce9a1b18798cd4ab8774.gif" alt="\log_{10}{n}" title="\log_{10}{n}" style="vertical-align: -5px; border: none;"/> (often written as just <img src="http://quicklatex.com/cache/ql_b2fd4a0c24f17cef1f3cf4a6f14573ad.gif" alt="\log{n}" title="\log{n}" style="vertical-align: -3px; border: none;"/>) for any 1-digit number will give you 0.something. For any 2-digit number it gives you 1.something, and so on. So a formula for getting the length of any number:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_d35af591fd67f30ad1bdbcfa89361096.gif" alt="Len(n)=\lfloor\log{n}\rfloor+1" title="Len(n)=\lfloor\log{n}\rfloor+1" style="vertical-align: -5px; border: none;"/></p></blockquote>
<p>In case you were wondering, those weird square brackets are called the <a href="http://en.wikipedia.org/wiki/Floor_function">floor function</a>.</p>
<h3>The length of a Fibonacci number</h3>
<p>Now we are able to calculate the length of a certain Fibonacci number by using the following formula, which I for no good reason will call G:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_afd0ad738732831b1a6c4cd618cd916b.gif" alt="G(n)=Len(Fib(n))=\lfloor\log{\dfrac{\varphi^n}{\sqrt{5}}}\rfloor+1" title="G(n)=Len(Fib(n))=\lfloor\log{\dfrac{\varphi^n}{\sqrt{5}}}\rfloor+1" style="vertical-align: -17px; border: none;"/></p></blockquote>
<p>There is a problem here though. And the problem is <img src="http://quicklatex.com/cache/ql_793ba3f348c170a1bd0ad338c7abea40.gif" alt="\varphi^n" title="\varphi^n" style="vertical-align: -4px; border: none;"/>. Why is it a problem? Because it get&#8217;s <em>seriously</em> large very quick. A value of around 500 actually makes my calculator overflow and just give me an error.</p>
<p>But fear not! There are a couple of formulas having to do with roots and logarithms that we can use to our advantage:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_a6d99d0e5fc3214b85bf8ff764bf0eae.gif" alt="\sqrt{c}=\sqrt[2]{c}" title="\sqrt{c}=\sqrt[2]{c}" style="vertical-align: -5px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_bdd731176b3acd420b9f3b5a6be51028.gif" alt="\sqrt[b]{c}=c^{\frac{1}{b}}" title="\sqrt[b]{c}=c^{\frac{1}{b}}" style="vertical-align: -5px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_fa72a9e784a5a9b5522448e6a34511c4.gif" alt="\log_a{\dfrac{b}{c}}=\log_a{b}-\log_a{c}" title="\log_a{\dfrac{b}{c}}=\log_a{b}-\log_a{c}" style="vertical-align: -12px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_1b1dafabfbc4d3a1d23aae1456b5b5f8.gif" alt="\log_a{b^c}=c\log_a{b}" title="\log_a{b^c}=c\log_a{b}" style="vertical-align: -4px; border: none;"/></p></blockquote>
<p>With those formulas we can handily rewrite our formula into one that is easier to handle:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_fd38d11ece4c20c897ce531c6f9579e4.gif" alt="G(n)=\lfloor\log{\dfrac{\varphi^n}{\sqrt{5}}}\rfloor+1" title="G(n)=\lfloor\log{\dfrac{\varphi^n}{\sqrt{5}}}\rfloor+1" style="vertical-align: -17px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_06ca9e3545b294e3724dd36b01c011bb.gif" alt="G(n)=\lfloor\log{\varphi^n}-\log{5^{\frac{1}{2}}}\rfloor+1" title="G(n)=\lfloor\log{\varphi^n}-\log{5^{\frac{1}{2}}}\rfloor+1" style="vertical-align: -5px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_3162d75a6efd53bb980fbd3b95df4316.gif" alt="G(n)=\lfloor n\log{\varphi}-\dfrac{\log{5}}{2}\rfloor+1" title="G(n)=\lfloor n\log{\varphi}-\dfrac{\log{5}}{2}\rfloor+1" style="vertical-align: -12px; border: none;"/></p></blockquote>
<p>Tadaa! Now we can push in 20 and get 4. And as we calculated earlier, the 20th Fibonacci number is 6769, which in fact is 4 digits long! Amazing&#8230;</p>
<h3>The first Fibonacci number with 1000 digits</h3>
<p>Now we are almost ready to solve our problem. The formula we have now works perfectly. The only problem is that it&#8217;s backwards! We are not looking for the length of a certain Fibonacci number, but rather what Fibonacci number has a certain length. In other words, we want <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/>, not <img src="http://quicklatex.com/cache/ql_a417655e890e7887367fc17c4fd83bbf.gif" alt="G(n)" title="G(n)" style="vertical-align: -4px; border: none;"/>. Luckily, using pretty basic algebra and some guessing, we can make a new formula that finds exactly what we are looking for. (My way of skipping the floor stuff and introducing a ceiling function in the end is purely based on guessing and observation. But it gives the correct answer <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Now, if you know the proper way of handling it, please let me know! )</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_3162d75a6efd53bb980fbd3b95df4316.gif" alt="G(n)=\lfloor n\log{\varphi}-\dfrac{\log{5}}{2}\rfloor+1" title="G(n)=\lfloor n\log{\varphi}-\dfrac{\log{5}}{2}\rfloor+1" style="vertical-align: -12px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_40db6dd9aa223bd01d050d018a23b95d.gif" alt="n\log{\varphi}=G(n)+\dfrac{\log{5}}{2}-1" title="n\log{\varphi}=G(n)+\dfrac{\log{5}}{2}-1" style="vertical-align: -12px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_ded0cb679459c3d86d75cd647dd9224c.gif" alt="n=\lceil\dfrac{G(n)+\dfrac{\log{5}}{2}-1}{\log\varphi}\rceil" title="n=\lceil\dfrac{G(n)+\dfrac{\log{5}}{2}-1}{\log\varphi}\rceil" style="vertical-align: -16px; border: none;"/></p></blockquote>
<p>Now we can just substitute <img src="http://quicklatex.com/cache/ql_a417655e890e7887367fc17c4fd83bbf.gif" alt="G(n)" title="G(n)" style="vertical-align: -4px; border: none;"/> with 1000, and calculate <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/>, which should be the answer to this problem!</p>
<p>As usual, I will not put the actual answer here though <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=';)' title=';)' class='wp-smiley smiley-20' /> </p>
<p>This has been the longest blog post yet about these problems, I think, but I must say it was pretty fun to solve this one. I didn&#8217;t really get much at first, just saw that the formulas worked, but now that I have written about it and gone through it all step by step, it actually makes sense. Most of it anyways. If you know how to properly handle that floor and ceiling stuff, let me know <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<p>Anyways, that was it for now! Stay tuned for more good stuff like this <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/06/project-euler-problem-25/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 16</title>
		<link>http://www.geekality.net/2009/11/05/project-euler-problem-16/</link>
		<comments>http://www.geekality.net/2009/11/05/project-euler-problem-16/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 16:46:16 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Big Int]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=740</guid>
		<description><![CDATA[and the sum of its digits is. What is the sum of the digits of the number ? Solution With the big integer type IntX (which I added to my project to solve an earlier problem) this one is pretty &#8230; <a href="http://www.geekality.net/2009/11/05/project-euler-problem-16/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p><img src="http://quicklatex.com/cache/ql_1c50c7f8d9c3e1a0c8a1c256d4adfd7e.gif" alt="2^{15} = 32768" title="2^{15} = 32768" style="vertical-align: 0px; border: none;"/> and the sum of its digits is<br/><img src="http://quicklatex.com/cache/ql_668af5ed992c9e14c23ce75b24c3f209.gif" alt="3 + 2 + 7 + 6 + 8 = 26" title="3 + 2 + 7 + 6 + 8 = 26" style="vertical-align: -1px; border: none;"/>.</p>
<p>What is the sum of the digits of the number <img src="http://quicklatex.com/cache/ql_8c307f59bbe61715d115649a7bc3282c.gif" alt="2^{1000}" title="2^{1000}" style="vertical-align: 0px; border: none;"/>?</p></blockquote>
<p><span id="more-740"></span></p>
<h2>Solution</h2>
<p>With the big integer type <a href="http://intx.codeplex.com/">IntX</a> (which I added to my project to solve an earlier problem) this one is pretty much as easy as you&#8217;d think.</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> IntX<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><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: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">-</span> <span style="color: #666666;">'0'</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Sum</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>It first does the calculation and then gets the string representation of the result. And in case you were wondering, that would be:</p>
<blockquote><p>10715086071862673209484250490600018105614048117<br />
05533607443750388370351051124936122493198378815<br />
69585812759467291755314682518714528569231404359<br />
84577574698574803934567774824230985421074605062<br />
37114187795418215304647498358194126739876755916<br />
55439460770629145711964776865421676604298316526<br />
24386837205668069376</p></blockquote>
<p>And as a comparison, here is the largest number that can be represented using built-in .net types. <code class="codecolorer text default"><span class="text">decimal.MaxValue</span></code>.</p>
<blockquote><p>79228162514264337593543950335</p></blockquote>
<p>As you can see, the first number is kind of bigger than the second <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Aaaanyways, the next bit might be a bit cryptic. But fear not, it is actually quite simple:</p>
<ul>
<li>A string is an array of characters.</li>
<li>All characters have a <a href="http://en.wikipedia.org/wiki/Character_code">character code</a> (a numeric value).</li>
<li>The character codes for the numeric characters (&#8217;0&#8242;, &#8217;1&#8242;, and so on) comes after each other, which means that for example &#8217;1&#8242; has a character code value which is 1 larger than the character code of &#8217;0&#8242;.</li>
<li>If we take a character code and subtracts it from itself, we get zero. <cite>(Thank you captain obvious&#8230;)</cite></li>
<li>This means that if we subtract &#8217;0&#8242; from &#8217;0&#8242;, we get 0. And if we subtract &#8217;0&#8242; from &#8217;5&#8242;, we get 5. <cite>(Oh, right, clever!)</cite></li>
</ul>
<p>So, the last two lines in my code simply converts all the characters in the string into actual numbers and then sums them together. Which would get us the answer! Tadaa <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='^_^' title='^_^' class='wp-smiley smiley-9' /> </p>
<p>How did you do? Have you found a more interesting solution? Please do share <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/05/project-euler-problem-16/feed/</wfw:commentRss>
		<slash:comments>0</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 14</title>
		<link>http://www.geekality.net/2009/10/19/project-euler-problem-14/</link>
		<comments>http://www.geekality.net/2009/10/19/project-euler-problem-14/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 17:47:38 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Collatz conjecture]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=616</guid>
		<description><![CDATA[The following iterative sequence is defined for the set of positive integers: (n is even) (n is odd) Using the rule above and starting with 13, we generate the following sequence: It can be seen that this sequence (starting at &#8230; <a href="http://www.geekality.net/2009/10/19/project-euler-problem-14/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>The following iterative sequence is defined for the set of positive integers:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_16d692c04fd5addb4db08f2a03d86071.gif" alt="n \to n/2" title="n \to n/2" style="vertical-align: -5px; border: none;"/> (n is even)<br />
<img src="http://quicklatex.com/cache/ql_acf5d060fd1c9c5cf8e01a4b6733bb9b.gif" alt="n \to 3n + 1" title="n \to 3n + 1" style="vertical-align: -1px; border: none;"/> (n is odd)</p></blockquote>
<p>Using the rule above and starting with 13, we generate the following sequence:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_eaff8a5b7b53554f48190d7934550b25.gif" alt="13 \to 40 \to 20 \to 10 \to 5 \to 16 \to 8 \to 4 \to 2 \to 1" title="13 \to 40 \to 20 \to 10 \to 5 \to 16 \to 8 \to 4 \to 2 \to 1" style="vertical-align: -1px; border: none;"/></p></blockquote>
<p>It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.</p>
<p>Which starting number, under one million, produces the longest chain?</p>
<p>NOTE: Once the chain starts the terms are allowed to go above one million.</p></blockquote>
<p><span id="more-616"></span></p>
<h2>Solution</h2>
<p>This problem has to do with the <a href="http://en.wikipedia.org/wiki/Collatz_conjecture">Collatz conjecture</a>. In short, from Wikipedia:</p>
<blockquote><p>The Collatz conjecture is an unsolved conjecture in mathematics. It is named after Lothar Collatz, who first proposed it in 1937. The conjecture is also known as the 3n + 1 conjecture, as the Ulam conjecture (after Stanislaw Ulam), or as the Kakutani&#8217;s Problem, or as the Syracuse problem; the sequence of numbers involved is referred to as the hailstone sequence or hailstone numbers, or as wondrous numbers.</p>
<p>We take any whole number n greater than 0. If n is even, we halve it (n/2), else we do &#8220;triple plus one&#8221; and get 3n+1. The conjecture is that for all numbers this process converges to 1. Hence it has been called &#8220;Half Or Triple Plus One&#8221;, sometimes called HOTPO.</p>
<p>Paul Erdős said about the Collatz conjecture: &#8220;Mathematics is not yet ready for such confusing, troubling, and hard problems.&#8221; He offered $500 for its solution. (Lagarias 1985)</p></blockquote>
<p>Interesting stuff. Ok, maybe not <em>that</em> interesting, but it is at least a problem that we can solve (not the Collatz Problem, but the Euler problem <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=';)' title=';)' class='wp-smiley smiley-20' /> ). And solving it is pretty simple. Just go through all the numbers below one million, and follow the mentioned algorithm for each number and see which one results in the longest chain.</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 startOfLongest <span style="color: #008000;">=</span> 0U<span style="color: #008000;">;</span><br />
var longestChain <span style="color: #008000;">=</span> 0U<span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span>var start <span style="color: #008000;">=</span> 1000000U<span style="color: #008000;">;</span> start <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> start<span style="color: #008000;">--</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; var s <span style="color: #008000;">=</span> start<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var length <span style="color: #008000;">=</span> 1U<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>s <span style="color: #008000;">!=</span> <span style="color: #FF0000;">1</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>s <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s <span style="color: #008000;">/=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span> <span style="color: #008000;">*</span> s <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; length<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;">if</span> <span style="color: #008000;">&#40;</span>length <span style="color: #008000;">&lt;</span> longestChain<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; startOfLongest <span style="color: #008000;">=</span> start<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; longestChain <span style="color: #008000;">=</span> length<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
var answer <span style="color: #008000;">=</span> startOfLongest<span style="color: #008000;">;</span></div></div>
<p>I don&#8217;t know if there really is much to say about that code. It is pretty straight forward. For each number, do the chain stuff until you reach 1. If the chain was longer than what we have found so far, store it. Then move on to the next one.</p>
<p>It runs in around 480 milliseconds so not among the fastest solutions I have had so far, but fast enough. I tried to come up with a more fancy solution, but they just took longer or didn&#8217;t work at all, so I&#8217;ll spare you the details of those <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<p>How did you do? Do you see any obvious inefficient parts in my code? Can it be optimized somehow? Please let me know in the comments below <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/10/19/project-euler-problem-14/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 13</title>
		<link>http://www.geekality.net/2009/10/04/project-euler-problem-13/</link>
		<comments>http://www.geekality.net/2009/10/04/project-euler-problem-13/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 20:58:59 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Big Int]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=612</guid>
		<description><![CDATA[Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 74324986199524741059474233309513058123726617309629 91942213363574161572522430563301811072406154908250 23067588207539346171171980310421047513778063246676 89261670696623633820136378418383684178734361726757 28112879812849979408065481931592621691275889832738 44274228917432520321923589422876796487670272189318 47451445736001306439091167216856844588711603153276 70386486105843025439939619828917593665686757934951 62176457141856560629502157223196586755079324193331 64906352462741904929101432445813822663347944758178 92575867718337217661963751590579239728245598838407 58203565325359399008402633568948830189458628227828 80181199384826282014278194139940567587151170094390 35398664372827112653829987240784473053190104293586 86515506006295864861532075273371959191420517255829 71693888707715466499115593487603532921714970056938 54370070576826684624621495650076471787294438377604 53282654108756828443191190634694037855217779295145 36123272525000296071075082563815656710885258350721 45876576172410976447339110607218265236877223636045 17423706905851860660448207621209813287860733969412 81142660418086830619328460811191061556940512689692 51934325451728388641918047049293215058642563049483 &#8230; <a href="http://www.geekality.net/2009/10/04/project-euler-problem-13/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.</p>
<p>37107287533902102798797998220837590246510135740250<br />
46376937677490009712648124896970078050417018260538<br />
74324986199524741059474233309513058123726617309629<br />
91942213363574161572522430563301811072406154908250<br />
23067588207539346171171980310421047513778063246676<br />
89261670696623633820136378418383684178734361726757<br />
28112879812849979408065481931592621691275889832738<br />
44274228917432520321923589422876796487670272189318<span id="more-612"></span><br />
47451445736001306439091167216856844588711603153276<br />
70386486105843025439939619828917593665686757934951<br />
62176457141856560629502157223196586755079324193331<br />
64906352462741904929101432445813822663347944758178<br />
92575867718337217661963751590579239728245598838407<br />
58203565325359399008402633568948830189458628227828<br />
80181199384826282014278194139940567587151170094390<br />
35398664372827112653829987240784473053190104293586<br />
86515506006295864861532075273371959191420517255829<br />
71693888707715466499115593487603532921714970056938<br />
54370070576826684624621495650076471787294438377604<br />
53282654108756828443191190634694037855217779295145<br />
36123272525000296071075082563815656710885258350721<br />
45876576172410976447339110607218265236877223636045<br />
17423706905851860660448207621209813287860733969412<br />
81142660418086830619328460811191061556940512689692<br />
51934325451728388641918047049293215058642563049483<br />
62467221648435076201727918039944693004732956340691<br />
15732444386908125794514089057706229429197107928209<br />
55037687525678773091862540744969844508330393682126<br />
18336384825330154686196124348767681297534375946515<br />
80386287592878490201521685554828717201219257766954<br />
78182833757993103614740356856449095527097864797581<br />
16726320100436897842553539920931837441497806860984<br />
48403098129077791799088218795327364475675590848030<br />
87086987551392711854517078544161852424320693150332<br />
59959406895756536782107074926966537676326235447210<br />
69793950679652694742597709739166693763042633987085<br />
41052684708299085211399427365734116182760315001271<br />
65378607361501080857009149939512557028198746004375<br />
35829035317434717326932123578154982629742552737307<br />
94953759765105305946966067683156574377167401875275<br />
88902802571733229619176668713819931811048770190271<br />
25267680276078003013678680992525463401061632866526<br />
36270218540497705585629946580636237993140746255962<br />
24074486908231174977792365466257246923322810917141<br />
91430288197103288597806669760892938638285025333403<br />
34413065578016127815921815005561868836468420090470<br />
23053081172816430487623791969842487255036638784583<br />
11487696932154902810424020138335124462181441773470<br />
63783299490636259666498587618221225225512486764533<br />
67720186971698544312419572409913959008952310058822<br />
95548255300263520781532296796249481641953868218774<br />
76085327132285723110424803456124867697064507995236<br />
37774242535411291684276865538926205024910326572967<br />
23701913275725675285653248258265463092207058596522<br />
29798860272258331913126375147341994889534765745501<br />
18495701454879288984856827726077713721403798879715<br />
38298203783031473527721580348144513491373226651381<br />
34829543829199918180278916522431027392251122869539<br />
40957953066405232632538044100059654939159879593635<br />
29746152185502371307642255121183693803580388584903<br />
41698116222072977186158236678424689157993532961922<br />
62467957194401269043877107275048102390895523597457<br />
23189706772547915061505504953922979530901129967519<br />
86188088225875314529584099251203829009407770775672<br />
11306739708304724483816533873502340845647058077308<br />
82959174767140363198008187129011875491310547126581<br />
97623331044818386269515456334926366572897563400500<br />
42846280183517070527831839425882145521227251250327<br />
55121603546981200581762165212827652751691296897789<br />
32238195734329339946437501907836945765883352399886<br />
75506164965184775180738168837861091527357929701337<br />
62177842752192623401942399639168044983993173312731<br />
32924185707147349566916674687634660915035914677504<br />
99518671430235219628894890102423325116913619626622<br />
73267460800591547471830798392868535206946944540724<br />
76841822524674417161514036427982273348055556214818<br />
97142617910342598647204516893989422179826088076852<br />
87783646182799346313767754307809363333018982642090<br />
10848802521674670883215120185883543223812876952786<br />
71329612474782464538636993009049310363619763878039<br />
62184073572399794223406235393808339651327408011116<br />
66627891981488087797941876876144230030984490851411<br />
60661826293682836764744779239180335110989069790714<br />
85786944089552990653640447425576083659976645795096<br />
66024396409905389607120198219976047599490197230297<br />
64913982680032973156037120041377903785566085089252<br />
16730939319872750275468906903707539413042652315011<br />
94809377245048795150954100921645863754710598436791<br />
78639167021187492431995700641917969777599028300699<br />
15368713711936614952811305876380278410754449733078<br />
40789923115535562561142322423255033685442488917353<br />
44889911501440648020369068063960672322193204149535<br />
41503128880339536053299340368006977710650566631954<br />
81234880673210146739058568557934581403627822703280<br />
82616570773948327592232845941706525094512325230608<br />
22918802058777319719839450180888072429661980811197<br />
77158542502016545090413245809786882778948721859617<br />
72107838435069186155435662884062257473692284509516<br />
20849603980134001723930671666823555245252804609722<br />
53503534226472524250874054075591789781264330331690</p></blockquote>
<h2>Solution</h2>
<p>This would&#8217;ve been pretty straight forward if it wasn&#8217;t for the fact that the numbers are too long. The UInt32 datatype can hold numbers up to 4294967295 (10 digits) and the UInt64 up to 18446744073709551615 (20 digits). That is at least 30 digits too little for those numbers we have to sum up.</p>
<p>We have mainly two options here. We could implement our own addition algorithm, for example by adding the numbers kind of like you would do by hand. Or we can find something that handles this large numbers already. </p>
<p>I decided to go with the last option. Later problems will probably become easier with such a class too, so might as well go hunting for one now.</p>
<p>After some looking around I found one on <a href="http://www.codeplex.com/">CodePlex</a> called <a href="http://intx.codeplex.com/">IntX</a>. When that was added to my project, solving the problem was easy. (The code looks a bit long, but it&#8217;s only because of the 50 numbers <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> )</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 Numbers <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;37107287533902102798797998220837590246510135740250&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;46376937677490009712648124896970078050417018260538&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;74324986199524741059474233309513058123726617309629&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;91942213363574161572522430563301811072406154908250&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;23067588207539346171171980310421047513778063246676&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;89261670696623633820136378418383684178734361726757&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;28112879812849979408065481931592621691275889832738&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;44274228917432520321923589422876796487670272189318&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;47451445736001306439091167216856844588711603153276&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;70386486105843025439939619828917593665686757934951&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;62176457141856560629502157223196586755079324193331&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;64906352462741904929101432445813822663347944758178&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;92575867718337217661963751590579239728245598838407&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;58203565325359399008402633568948830189458628227828&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;80181199384826282014278194139940567587151170094390&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;35398664372827112653829987240784473053190104293586&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;86515506006295864861532075273371959191420517255829&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;71693888707715466499115593487603532921714970056938&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;54370070576826684624621495650076471787294438377604&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;53282654108756828443191190634694037855217779295145&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;36123272525000296071075082563815656710885258350721&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;45876576172410976447339110607218265236877223636045&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;17423706905851860660448207621209813287860733969412&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;81142660418086830619328460811191061556940512689692&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;51934325451728388641918047049293215058642563049483&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;62467221648435076201727918039944693004732956340691&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;15732444386908125794514089057706229429197107928209&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;55037687525678773091862540744969844508330393682126&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;18336384825330154686196124348767681297534375946515&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;80386287592878490201521685554828717201219257766954&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;78182833757993103614740356856449095527097864797581&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;16726320100436897842553539920931837441497806860984&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;48403098129077791799088218795327364475675590848030&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;87086987551392711854517078544161852424320693150332&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;59959406895756536782107074926966537676326235447210&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;69793950679652694742597709739166693763042633987085&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;41052684708299085211399427365734116182760315001271&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;65378607361501080857009149939512557028198746004375&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;35829035317434717326932123578154982629742552737307&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;94953759765105305946966067683156574377167401875275&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;88902802571733229619176668713819931811048770190271&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;25267680276078003013678680992525463401061632866526&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;36270218540497705585629946580636237993140746255962&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;24074486908231174977792365466257246923322810917141&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;91430288197103288597806669760892938638285025333403&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;34413065578016127815921815005561868836468420090470&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;23053081172816430487623791969842487255036638784583&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;11487696932154902810424020138335124462181441773470&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;63783299490636259666498587618221225225512486764533&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;67720186971698544312419572409913959008952310058822&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;95548255300263520781532296796249481641953868218774&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;76085327132285723110424803456124867697064507995236&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;37774242535411291684276865538926205024910326572967&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;23701913275725675285653248258265463092207058596522&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;29798860272258331913126375147341994889534765745501&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;18495701454879288984856827726077713721403798879715&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;38298203783031473527721580348144513491373226651381&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;34829543829199918180278916522431027392251122869539&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;40957953066405232632538044100059654939159879593635&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;29746152185502371307642255121183693803580388584903&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;41698116222072977186158236678424689157993532961922&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;62467957194401269043877107275048102390895523597457&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;23189706772547915061505504953922979530901129967519&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;86188088225875314529584099251203829009407770775672&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;11306739708304724483816533873502340845647058077308&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;82959174767140363198008187129011875491310547126581&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;97623331044818386269515456334926366572897563400500&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;42846280183517070527831839425882145521227251250327&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;55121603546981200581762165212827652751691296897789&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;32238195734329339946437501907836945765883352399886&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;75506164965184775180738168837861091527357929701337&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;62177842752192623401942399639168044983993173312731&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;32924185707147349566916674687634660915035914677504&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;99518671430235219628894890102423325116913619626622&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;73267460800591547471830798392868535206946944540724&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;76841822524674417161514036427982273348055556214818&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;97142617910342598647204516893989422179826088076852&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;87783646182799346313767754307809363333018982642090&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;10848802521674670883215120185883543223812876952786&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;71329612474782464538636993009049310363619763878039&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;62184073572399794223406235393808339651327408011116&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;66627891981488087797941876876144230030984490851411&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;60661826293682836764744779239180335110989069790714&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;85786944089552990653640447425576083659976645795096&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;66024396409905389607120198219976047599490197230297&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;64913982680032973156037120041377903785566085089252&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;16730939319872750275468906903707539413042652315011&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;94809377245048795150954100921645863754710598436791&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;78639167021187492431995700641917969777599028300699&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;15368713711936614952811305876380278410754449733078&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;40789923115535562561142322423255033685442488917353&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;44889911501440648020369068063960672322193204149535&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;41503128880339536053299340368006977710650566631954&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;81234880673210146739058568557934581403627822703280&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;82616570773948327592232845941706525094512325230608&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;22918802058777319719839450180888072429661980811197&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;77158542502016545090413245809786882778948721859617&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;72107838435069186155435662884062257473692284509516&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;20849603980134001723930671666823555245252804609722&quot;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;53503534226472524250874054075591789781264330331690&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
var sum <span style="color: #008000;">=</span> Numbers<span style="color: #008000;">.</span><span style="color: #0000FF;">Aggregate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">&#40;</span>s, x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> s <span style="color: #008000;">+</span> x<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
var answer <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToUInt64</span><span style="color: #008000;">&#40;</span>sum<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Truncate</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Runs in less than a millisecond, around 335 ticks. Not much more to say about this one really&#8230; how did you do it? </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/10/04/project-euler-problem-13/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 12</title>
		<link>http://www.geekality.net/2009/10/04/project-euler-problem-12/</link>
		<comments>http://www.geekality.net/2009/10/04/project-euler-problem-12/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 23:43:54 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Triangle Numbers]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=588</guid>
		<description><![CDATA[The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: &#8230; <a href="http://www.geekality.net/2009/10/04/project-euler-problem-12/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:</p>
<blockquote><p>1, 3, 6, 10, 15, 21, 28, 36, 45, 55, &#8230;</p></blockquote>
<p>Let us list the factors of the first seven triangle numbers:</p>
<blockquote><p>&nbsp;1: 1<br />
&nbsp;3: 1,3<br />
&nbsp;6: 1,2,3,6<br />
10: 1,2,5,10<br />
15: 1,3,5,15<br />
21: 1,3,7,21<br />
28: 1,2,4,7,14,28</p></blockquote>
<p>We can see that 28 is the first triangle number to have over five divisors.</p>
<p>What is the value of the first triangle number to have over five hundred divisors?</p></blockquote>
<p>At first it seemed like a piece of cake, however it wasn&#8217;t as easy as I thought it was&#8230;</p>
<p><span id="more-588"></span></p>
<h2>Solution</h2>
<p>Well, what we first need here, irregardless of how you do it (pretty much), is a source of triangle numbers.</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> TriangleSequence <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;">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: #6666cc; font-weight: bold;">ulong</span> sum <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</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;">1</span><span style="color: #008000;">;;</span> n<span style="color: #008000;">++</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; sum <span style="color: #008000;">+=</span> n<span style="color: #008000;">;</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> sum<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><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 />
<span style="color: #008000;">&#125;</span></div></div>
<p>Pretty simple and straight forward. We can then move on to the next issue.</p>
<h3>Take one</h3>
<p>Like mentioned, I thought this one would be a piece of cake. I mean, having the sequence of triangle numbers in place, how hard could it be? My first solution, probably the most obvious one, is to go through each triangle number, find the factors of it, see if there are more than 500 of them, and move to the next if it isn&#8217;t. </p>
<p>I created a basic factorization method using the rather naive trial-division method and wrote a simple Linq statement to get 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"><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> GetDivisors<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> number<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>number <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&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 />
<br />
&nbsp; &nbsp; var limit <span style="color: #008000;">=</span> number <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span><br />
&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> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> limit<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>number <span style="color: #008000;">%</span> i <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: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> i<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> number<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<br />
var answer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TriangleSequence<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Number <span style="color: #008000;">=</span> x,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DivisorCount <span style="color: #008000;">=</span> Factorization<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDivisors</span><span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">DivisorCount</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">500</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Number</span><span style="color: #008000;">;</span></div></div>
<p>And then I ran it. A few seconds went by and no answer. Minutes went by and still no answer. Considering the fact that most of my other problems was solved in less than a few seconds, this was just taking too long. A smarter solution is needed.</p>
<h3>Take two</h3>
<p>The way to speed this up is &#8220;of course&#8221; to find a faster factorization algorithm, so I went hunting for a more effective one. But, while I was on that hunt, I realized that I was perhaps on the wrong path here. I mean, what do I even need those factors for? What I <em>really</em> need is just the <em>count</em> of the factors. I wasn&#8217;t sure if such a method existed though, but while roaming around on <a href="http://stackoverflow.com/">StackOverflow</a> (like I tend to do sometimes) I stumbled over <a href="http://stackoverflow.com/questions/110344/algorithm-to-calculate-the-number-of-divisors-of-a-given-number/118712#118712">some Python code</a>. Later I also found some formulas. So, time for some math.</p>
<p>Any integer can be expressed as</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_826f7f9ede1ef0a329915d07541e2504.gif" alt="N = p_1^{a_1} \cdot p_2^{a_2} \cdot p_3^{a_3} \cdot \ldots" title="N = p_1^{a_1} \cdot p_2^{a_2} \cdot p_3^{a_3} \cdot \ldots" style="vertical-align: -6px; border: none;"/></p></blockquote>
<p>where <img src="http://quicklatex.com/cache/ql_6cbb60d59d04d1d7c9e64fd2a001c8c6.gif" alt="p_n" title="p_n" style="vertical-align: -4px; border: none;"/> is a distinct prime number and <img src="http://quicklatex.com/cache/ql_825b3fd5bafbc46b9a560ea9f16b21dd.gif" alt="a_n" title="a_n" style="vertical-align: -3px; border: none;"/> is its exponent. The count of divisors <img src="http://quicklatex.com/cache/ql_f6720d6c86520671edc8a66dabea7903.gif" alt="D(N)" title="D(N)" style="vertical-align: -4px; border: none;"/> can then be found by the formula</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_5c513fb417006301d3d753d59d8df285.gif" alt="D(N) = (a_1 + 1) \cdot (a_2 + 1) \cdot (a_3 + 1) \cdot \ldots" title="D(N) = (a_1 + 1) \cdot (a_2 + 1) \cdot (a_3 + 1) \cdot \ldots" style="vertical-align: -4px; border: none;"/></p></blockquote>
<p>For example:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_778e97f73c9d6c33409b20ace9ef1b12.gif" alt="N = 36 = 3^2 \cdot 2^2 = 9 \cdot 4" title="N = 36 = 3^2 \cdot 2^2 = 9 \cdot 4" style="vertical-align: -1px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_21488445b81811fe87f75802082b7dcb.gif" alt="D(32) = (2 + 1) \cdot (2 + 1) = 9" title="D(32) = (2 + 1) \cdot (2 + 1) = 9" style="vertical-align: -4px; border: none;"/></p></blockquote>
<p>With this knowledge and an example implementation in python, I was able to throw together the following 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"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ProbablePrimeSequence <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;">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;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">ulong</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span><br />
&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; <span style="color: #008000;">&#123;</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> i<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #008000;">%</span> <span style="color: #FF0000;">6</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i <span style="color: #008000;">+=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i <span style="color: #008000;">+=</span> <span style="color: #FF0000;">2</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><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 />
<span style="color: #008000;">&#125;</span><br />
<br />
<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> GetCountOfDivisors<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">ulong</span> number<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>number <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; var divisors <span style="color: #008000;">=</span> 1UL<span style="color: #008000;">;</span><br />
<br />
&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> <span style="color: #008000;">new</span> ProbablePrimeSequence<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var exponent <span style="color: #008000;">=</span> 0UL<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>number <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; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exponent <span style="color: #008000;">+=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number <span style="color: #008000;">/=</span> prime<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: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>exponent <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; divisors <span style="color: #008000;">*=</span> exponent <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>number <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: #0600FF; font-weight: bold;">break</span><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> divisors<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Now, why this works by just using probable primes and not pure primes, I am not sure to be honest. But it sure is a lot faster. My tests doesn&#8217;t fail and I get the right answer so I&#8217;ll just move on <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> (However, leave a comment if you know, cause I would like to know too <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> )</p>
<p>Anyways, now we can finally find our answer using the following straight forward statement.</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> TriangleSequence<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;">First</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span>GetCountOfDivisors<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">500</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>The running time of that lays around 320 milliseconds, which I think is pretty acceptable although not blazingly fast.</p>
<p>What do you think of my solution? Have I overlooked something obvious? How would you solve this one? I am curious and would like to know, so please leave a comment <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/10/04/project-euler-problem-12/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 11</title>
		<link>http://www.geekality.net/2009/10/03/project-euler-problem-11/</link>
		<comments>http://www.geekality.net/2009/10/03/project-euler-problem-11/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 17:44:58 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=569</guid>
		<description><![CDATA[In the 20&#215;20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 &#8230; <a href="http://www.geekality.net/2009/10/03/project-euler-problem-11/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>In the 20&#215;20 grid below, four numbers along a diagonal line have been marked in red.</p></blockquote>
<p style="font-family:courier new;text-align:center;font-size:9pt;">
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08<br />
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00<br />
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65<br />
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91<br />
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80<br />
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50<br />
32 98 81 28 64 23 67 10 <span style="color:#ff0000;"><b>26</b></span> 38 40 67 59 54 70 66 18 38 64 70<br />
67 26 20 68 02 62 12 20 95 <span style="color:#ff0000;"><b>63</b></span> 94 39 63 08 40 91 66 49 94 21<br />
24 55 58 05 66 73 99 26 97 17 <span style="color:#ff0000;"><b>78</b></span> 78 96 83 14 88 34 89 63 72<br />
21 36 23 09 75 00 76 44 20 45 35 <span style="color:#ff0000;"><b>14</b></span> 00 61 33 97 34 31 33 95<br />
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92<br />
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57<br />
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58<br />
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40<br />
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66<br />
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69<br />
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36<br />
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16<br />
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54<br />
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
</p>
<blockquote><p>The product of these numbers is 26 x 63 x 78 x 14 = 1788696.</p>
<p>What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20&#215;20 grid?</p></blockquote>
<p><span id="more-569"></span></p>
<h2>Solution</h2>
<p>Not really much interesting you can do with this one really. Going through the whole grid while checking every single possibility is really the only way you can do it. We have take every number in the grid and multiply it with the three numbers to the left of it, below it, diagonally up to the right, and so on, like shown below.</p>
<p><img src="http://www.geekality.net/wp-content/uploads/2009/10/EulerProblem11-Example1.png" alt="EulerProblem11-Example-1" title="EulerProblem11-Example-1" width="241" height="192" class="aligncenter size-full wp-image-570" /></p>
<p>However, this way we would actually be checking all the possibilities twice because a product doesn&#8217;t care in what order its factors are. This means we can for example just check the ones to the right and not the ones to the left. We then end up with only four of the original eight &#8220;arms&#8221; of factors to multiply.</p>
<p><img src="http://www.geekality.net/wp-content/uploads/2009/10/EulerProblem11-Example2.png" alt="EulerProblem11-Example-2" title="EulerProblem11-Example-2" width="241" height="192" class="aligncenter size-full wp-image-571" /></p>
<p>I put the grid in a two dimensional array and then just looped through all of them looking for the highest product. It is very simple to make. Only thing that can be a bit tricky is to make sure you do try out all the possibilities without going out the side the array bounds. Anyways, you can see my result below.</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 grid <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #008000;">&#91;</span>,<span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span>08, 02, <span style="color: #FF0000;">22</span>, <span style="color: #FF0000;">97</span>, <span style="color: #FF0000;">38</span>, <span style="color: #FF0000;">15</span>, 00, <span style="color: #FF0000;">40</span>, 00, <span style="color: #FF0000;">75</span>, 04, 05, 07, <span style="color: #FF0000;">78</span>, <span style="color: #FF0000;">52</span>, <span style="color: #FF0000;">12</span>, <span style="color: #FF0000;">50</span>, <span style="color: #FF0000;">77</span>, <span style="color: #FF0000;">91</span>, 08<span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">49</span>, <span style="color: #FF0000;">49</span>, <span style="color: #FF0000;">99</span>, <span style="color: #FF0000;">40</span>, <span style="color: #FF0000;">17</span>, <span style="color: #FF0000;">81</span>, <span style="color: #FF0000;">18</span>, <span style="color: #FF0000;">57</span>, <span style="color: #FF0000;">60</span>, <span style="color: #FF0000;">87</span>, <span style="color: #FF0000;">17</span>, <span style="color: #FF0000;">40</span>, <span style="color: #FF0000;">98</span>, <span style="color: #FF0000;">43</span>, <span style="color: #FF0000;">69</span>, <span style="color: #FF0000;">48</span>, 04, <span style="color: #FF0000;">56</span>, <span style="color: #FF0000;">62</span>, 00<span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">81</span>, <span style="color: #FF0000;">49</span>, <span style="color: #FF0000;">31</span>, <span style="color: #FF0000;">73</span>, <span style="color: #FF0000;">55</span>, <span style="color: #FF0000;">79</span>, <span style="color: #FF0000;">14</span>, <span style="color: #FF0000;">29</span>, <span style="color: #FF0000;">93</span>, <span style="color: #FF0000;">71</span>, <span style="color: #FF0000;">40</span>, <span style="color: #FF0000;">67</span>, <span style="color: #FF0000;">53</span>, <span style="color: #FF0000;">88</span>, <span style="color: #FF0000;">30</span>, 03, <span style="color: #FF0000;">49</span>, <span style="color: #FF0000;">13</span>, <span style="color: #FF0000;">36</span>, <span style="color: #FF0000;">65</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">52</span>, <span style="color: #FF0000;">70</span>, <span style="color: #FF0000;">95</span>, <span style="color: #FF0000;">23</span>, 04, <span style="color: #FF0000;">60</span>, <span style="color: #FF0000;">11</span>, <span style="color: #FF0000;">42</span>, <span style="color: #FF0000;">69</span>, <span style="color: #FF0000;">24</span>, <span style="color: #FF0000;">68</span>, <span style="color: #FF0000;">56</span>, 01, <span style="color: #FF0000;">32</span>, <span style="color: #FF0000;">56</span>, <span style="color: #FF0000;">71</span>, <span style="color: #FF0000;">37</span>, 02, <span style="color: #FF0000;">36</span>, <span style="color: #FF0000;">91</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">22</span>, <span style="color: #FF0000;">31</span>, <span style="color: #FF0000;">16</span>, <span style="color: #FF0000;">71</span>, <span style="color: #FF0000;">51</span>, <span style="color: #FF0000;">67</span>, <span style="color: #FF0000;">63</span>, <span style="color: #FF0000;">89</span>, <span style="color: #FF0000;">41</span>, <span style="color: #FF0000;">92</span>, <span style="color: #FF0000;">36</span>, <span style="color: #FF0000;">54</span>, <span style="color: #FF0000;">22</span>, <span style="color: #FF0000;">40</span>, <span style="color: #FF0000;">40</span>, <span style="color: #FF0000;">28</span>, <span style="color: #FF0000;">66</span>, <span style="color: #FF0000;">33</span>, <span style="color: #FF0000;">13</span>, <span style="color: #FF0000;">80</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">24</span>, <span style="color: #FF0000;">47</span>, <span style="color: #FF0000;">32</span>, <span style="color: #FF0000;">60</span>, <span style="color: #FF0000;">99</span>, 03, <span style="color: #FF0000;">45</span>, 02, <span style="color: #FF0000;">44</span>, <span style="color: #FF0000;">75</span>, <span style="color: #FF0000;">33</span>, <span style="color: #FF0000;">53</span>, <span style="color: #FF0000;">78</span>, <span style="color: #FF0000;">36</span>, <span style="color: #FF0000;">84</span>, <span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">35</span>, <span style="color: #FF0000;">17</span>, <span style="color: #FF0000;">12</span>, <span style="color: #FF0000;">50</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">32</span>, <span style="color: #FF0000;">98</span>, <span style="color: #FF0000;">81</span>, <span style="color: #FF0000;">28</span>, <span style="color: #FF0000;">64</span>, <span style="color: #FF0000;">23</span>, <span style="color: #FF0000;">67</span>, <span style="color: #FF0000;">10</span>, <span style="color: #FF0000;">26</span>, <span style="color: #FF0000;">38</span>, <span style="color: #FF0000;">40</span>, <span style="color: #FF0000;">67</span>, <span style="color: #FF0000;">59</span>, <span style="color: #FF0000;">54</span>, <span style="color: #FF0000;">70</span>, <span style="color: #FF0000;">66</span>, <span style="color: #FF0000;">18</span>, <span style="color: #FF0000;">38</span>, <span style="color: #FF0000;">64</span>, <span style="color: #FF0000;">70</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">67</span>, <span style="color: #FF0000;">26</span>, <span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">68</span>, 02, <span style="color: #FF0000;">62</span>, <span style="color: #FF0000;">12</span>, <span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">95</span>, <span style="color: #FF0000;">63</span>, <span style="color: #FF0000;">94</span>, <span style="color: #FF0000;">39</span>, <span style="color: #FF0000;">63</span>, 08, <span style="color: #FF0000;">40</span>, <span style="color: #FF0000;">91</span>, <span style="color: #FF0000;">66</span>, <span style="color: #FF0000;">49</span>, <span style="color: #FF0000;">94</span>, <span style="color: #FF0000;">21</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">24</span>, <span style="color: #FF0000;">55</span>, <span style="color: #FF0000;">58</span>, 05, <span style="color: #FF0000;">66</span>, <span style="color: #FF0000;">73</span>, <span style="color: #FF0000;">99</span>, <span style="color: #FF0000;">26</span>, <span style="color: #FF0000;">97</span>, <span style="color: #FF0000;">17</span>, <span style="color: #FF0000;">78</span>, <span style="color: #FF0000;">78</span>, <span style="color: #FF0000;">96</span>, <span style="color: #FF0000;">83</span>, <span style="color: #FF0000;">14</span>, <span style="color: #FF0000;">88</span>, <span style="color: #FF0000;">34</span>, <span style="color: #FF0000;">89</span>, <span style="color: #FF0000;">63</span>, <span style="color: #FF0000;">72</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">21</span>, <span style="color: #FF0000;">36</span>, <span style="color: #FF0000;">23</span>, 09, <span style="color: #FF0000;">75</span>, 00, <span style="color: #FF0000;">76</span>, <span style="color: #FF0000;">44</span>, <span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">45</span>, <span style="color: #FF0000;">35</span>, <span style="color: #FF0000;">14</span>, 00, <span style="color: #FF0000;">61</span>, <span style="color: #FF0000;">33</span>, <span style="color: #FF0000;">97</span>, <span style="color: #FF0000;">34</span>, <span style="color: #FF0000;">31</span>, <span style="color: #FF0000;">33</span>, <span style="color: #FF0000;">95</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">78</span>, <span style="color: #FF0000;">17</span>, <span style="color: #FF0000;">53</span>, <span style="color: #FF0000;">28</span>, <span style="color: #FF0000;">22</span>, <span style="color: #FF0000;">75</span>, <span style="color: #FF0000;">31</span>, <span style="color: #FF0000;">67</span>, <span style="color: #FF0000;">15</span>, <span style="color: #FF0000;">94</span>, 03, <span style="color: #FF0000;">80</span>, 04, <span style="color: #FF0000;">62</span>, <span style="color: #FF0000;">16</span>, <span style="color: #FF0000;">14</span>, 09, <span style="color: #FF0000;">53</span>, <span style="color: #FF0000;">56</span>, <span style="color: #FF0000;">92</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">16</span>, <span style="color: #FF0000;">39</span>, 05, <span style="color: #FF0000;">42</span>, <span style="color: #FF0000;">96</span>, <span style="color: #FF0000;">35</span>, <span style="color: #FF0000;">31</span>, <span style="color: #FF0000;">47</span>, <span style="color: #FF0000;">55</span>, <span style="color: #FF0000;">58</span>, <span style="color: #FF0000;">88</span>, <span style="color: #FF0000;">24</span>, 00, <span style="color: #FF0000;">17</span>, <span style="color: #FF0000;">54</span>, <span style="color: #FF0000;">24</span>, <span style="color: #FF0000;">36</span>, <span style="color: #FF0000;">29</span>, <span style="color: #FF0000;">85</span>, <span style="color: #FF0000;">57</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">86</span>, <span style="color: #FF0000;">56</span>, 00, <span style="color: #FF0000;">48</span>, <span style="color: #FF0000;">35</span>, <span style="color: #FF0000;">71</span>, <span style="color: #FF0000;">89</span>, 07, 05, <span style="color: #FF0000;">44</span>, <span style="color: #FF0000;">44</span>, <span style="color: #FF0000;">37</span>, <span style="color: #FF0000;">44</span>, <span style="color: #FF0000;">60</span>, <span style="color: #FF0000;">21</span>, <span style="color: #FF0000;">58</span>, <span style="color: #FF0000;">51</span>, <span style="color: #FF0000;">54</span>, <span style="color: #FF0000;">17</span>, <span style="color: #FF0000;">58</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">19</span>, <span style="color: #FF0000;">80</span>, <span style="color: #FF0000;">81</span>, <span style="color: #FF0000;">68</span>, 05, <span style="color: #FF0000;">94</span>, <span style="color: #FF0000;">47</span>, <span style="color: #FF0000;">69</span>, <span style="color: #FF0000;">28</span>, <span style="color: #FF0000;">73</span>, <span style="color: #FF0000;">92</span>, <span style="color: #FF0000;">13</span>, <span style="color: #FF0000;">86</span>, <span style="color: #FF0000;">52</span>, <span style="color: #FF0000;">17</span>, <span style="color: #FF0000;">77</span>, 04, <span style="color: #FF0000;">89</span>, <span style="color: #FF0000;">55</span>, <span style="color: #FF0000;">40</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span>04, <span style="color: #FF0000;">52</span>, 08, <span style="color: #FF0000;">83</span>, <span style="color: #FF0000;">97</span>, <span style="color: #FF0000;">35</span>, <span style="color: #FF0000;">99</span>, <span style="color: #FF0000;">16</span>, 07, <span style="color: #FF0000;">97</span>, <span style="color: #FF0000;">57</span>, <span style="color: #FF0000;">32</span>, <span style="color: #FF0000;">16</span>, <span style="color: #FF0000;">26</span>, <span style="color: #FF0000;">26</span>, <span style="color: #FF0000;">79</span>, <span style="color: #FF0000;">33</span>, <span style="color: #FF0000;">27</span>, <span style="color: #FF0000;">98</span>, <span style="color: #FF0000;">66</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">88</span>, <span style="color: #FF0000;">36</span>, <span style="color: #FF0000;">68</span>, <span style="color: #FF0000;">87</span>, <span style="color: #FF0000;">57</span>, <span style="color: #FF0000;">62</span>, <span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">72</span>, 03, <span style="color: #FF0000;">46</span>, <span style="color: #FF0000;">33</span>, <span style="color: #FF0000;">67</span>, <span style="color: #FF0000;">46</span>, <span style="color: #FF0000;">55</span>, <span style="color: #FF0000;">12</span>, <span style="color: #FF0000;">32</span>, <span style="color: #FF0000;">63</span>, <span style="color: #FF0000;">93</span>, <span style="color: #FF0000;">53</span>, <span style="color: #FF0000;">69</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span>04, <span style="color: #FF0000;">42</span>, <span style="color: #FF0000;">16</span>, <span style="color: #FF0000;">73</span>, <span style="color: #FF0000;">38</span>, <span style="color: #FF0000;">25</span>, <span style="color: #FF0000;">39</span>, <span style="color: #FF0000;">11</span>, <span style="color: #FF0000;">24</span>, <span style="color: #FF0000;">94</span>, <span style="color: #FF0000;">72</span>, <span style="color: #FF0000;">18</span>, 08, <span style="color: #FF0000;">46</span>, <span style="color: #FF0000;">29</span>, <span style="color: #FF0000;">32</span>, <span style="color: #FF0000;">40</span>, <span style="color: #FF0000;">62</span>, <span style="color: #FF0000;">76</span>, <span style="color: #FF0000;">36</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">69</span>, <span style="color: #FF0000;">36</span>, <span style="color: #FF0000;">41</span>, <span style="color: #FF0000;">72</span>, <span style="color: #FF0000;">30</span>, <span style="color: #FF0000;">23</span>, <span style="color: #FF0000;">88</span>, <span style="color: #FF0000;">34</span>, <span style="color: #FF0000;">62</span>, <span style="color: #FF0000;">99</span>, <span style="color: #FF0000;">69</span>, <span style="color: #FF0000;">82</span>, <span style="color: #FF0000;">67</span>, <span style="color: #FF0000;">59</span>, <span style="color: #FF0000;">85</span>, <span style="color: #FF0000;">74</span>, 04, <span style="color: #FF0000;">36</span>, <span style="color: #FF0000;">16</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">73</span>, <span style="color: #FF0000;">35</span>, <span style="color: #FF0000;">29</span>, <span style="color: #FF0000;">78</span>, <span style="color: #FF0000;">31</span>, <span style="color: #FF0000;">90</span>, 01, <span style="color: #FF0000;">74</span>, <span style="color: #FF0000;">31</span>, <span style="color: #FF0000;">49</span>, <span style="color: #FF0000;">71</span>, <span style="color: #FF0000;">48</span>, <span style="color: #FF0000;">86</span>, <span style="color: #FF0000;">81</span>, <span style="color: #FF0000;">16</span>, <span style="color: #FF0000;">23</span>, <span style="color: #FF0000;">57</span>, 05, <span style="color: #FF0000;">54</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span>01, <span style="color: #FF0000;">70</span>, <span style="color: #FF0000;">54</span>, <span style="color: #FF0000;">71</span>, <span style="color: #FF0000;">83</span>, <span style="color: #FF0000;">51</span>, <span style="color: #FF0000;">54</span>, <span style="color: #FF0000;">69</span>, <span style="color: #FF0000;">16</span>, <span style="color: #FF0000;">92</span>, <span style="color: #FF0000;">33</span>, <span style="color: #FF0000;">48</span>, <span style="color: #FF0000;">61</span>, <span style="color: #FF0000;">43</span>, <span style="color: #FF0000;">52</span>, 01, <span style="color: #FF0000;">89</span>, <span style="color: #FF0000;">19</span>, <span style="color: #FF0000;">67</span>, <span style="color: #FF0000;">48</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
var rows <span style="color: #008000;">=</span> grid<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLength</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
var columns <span style="color: #008000;">=</span> grid<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLength</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
var greatest <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span>var r <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> r <span style="color: #008000;">&lt;</span> rows<span style="color: #008000;">;</span> r<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span>var c <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> c <span style="color: #008000;">&lt;</span> columns<span style="color: #008000;">;</span> c<span style="color: #008000;">++</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>c <span style="color: #008000;">&lt;</span> columns <span style="color: #008000;">-</span> <span style="color: #FF0000;">3</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;">// Right and &quot;Left&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>greatest <span style="color: #008000;">&lt;</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greatest <span style="color: #008000;">=</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</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: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>r <span style="color: #008000;">&lt;</span> rows <span style="color: #008000;">-</span> <span style="color: #FF0000;">3</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;">// Down and &quot;Up&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>greatest <span style="color: #008000;">&lt;</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span>,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span>,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span>,c<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greatest <span style="color: #008000;">=</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span>,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span>,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span>,c<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Diagonally, down to the right</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>c <span style="color: #008000;">&lt;</span> columns <span style="color: #008000;">-</span> <span style="color: #FF0000;">3</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>greatest <span style="color: #008000;">&lt;</span> Grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> Grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span>,c<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> Grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span>,c<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span>,c<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greatest <span style="color: #008000;">=</span> Grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> Grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span>,c<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> Grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span>,c<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span>,c<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Diagonally, down to the left</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>c <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">3</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>greatest <span style="color: #008000;">&lt;</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span>,c<span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span>,c<span style="color: #008000;">-</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span>,c<span style="color: #008000;">-</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greatest <span style="color: #008000;">=</span> grid<span style="color: #008000;">&#91;</span>r,c<span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span>,c<span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">2</span>,c<span style="color: #008000;">-</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">*</span> grid<span style="color: #008000;">&#91;</span>r<span style="color: #008000;">+</span><span style="color: #FF0000;">3</span>,c<span style="color: #008000;">-</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</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><br />
<br />
var answer <span style="color: #008000;">=</span> greatest<span style="color: #008000;">;</span></div></div>
<p>And that&#8217;s pretty much it! Takes around 150 ticks, and is not really interesting to be honest <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Next Euler problem however is a bit more tricky&#8230; will write about that one next, so stay tuned <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/10/03/project-euler-problem-11/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
