<?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; Big Int</title>
	<atom:link href="http://www.geekality.net/tag/big-int/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>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 8</title>
		<link>http://www.geekality.net/2009/09/24/project-euler-problem-8/</link>
		<comments>http://www.geekality.net/2009/09/24/project-euler-problem-8/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 17:14: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>

		<guid isPermaLink="false">http://www.geekality.net/?p=505</guid>
		<description><![CDATA[Find the greatest product of five consecutive digits in the 1000-digit number. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 Solution This one wasn&#8217;t too complicated to do, &#8230; <a href="http://www.geekality.net/2009/09/24/project-euler-problem-8/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>Find the greatest product of five consecutive digits in the 1000-digit number.</p>
<blockquote><p>73167176531330624919225119674426574742355349194934<br />
96983520312774506326239578318016984801869478851843<br />
85861560789112949495459501737958331952853208805511<br />
12540698747158523863050715693290963295227443043557<br />
66896648950445244523161731856403098711121722383113<br />
62229893423380308135336276614282806444486645238749<br />
30358907296290491560440772390713810515859307960866<br />
70172427121883998797908792274921901699720888093776<br />
65727333001053367881220235421809751254540594752243<br />
52584907711670556013604839586446706324415722155397<br />
53697817977846174064955149290862569321978468622482<br />
83972241375657056057490261407972968652414535100474<br />
82166370484403199890008895243450658541227588666881<br />
16427171479924442928230863465674813919123162824586<br />
17866458359124566529476545682848912883142607690042<br />
24219022671055626321111109370544217506941658960408<br />
07198403850962455444362981230987879927244284909188<br />
84580156166097919133875499200524063689912560717606<br />
05886116467109405077541002256983155200055935729725<br />
71636269561882670428252483600823257530420752963450</p></blockquote>
</blockquote>
<p><span id="more-505"></span></p>
<h2>Solution</h2>
<p>This one wasn&#8217;t too complicated to do, and there isn&#8217;t really that much optimizations or super fancy math stuff that can help either.</p>
<p>Simply put the numbers in an array, and walk through it.</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 d <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;73167176531330624919225119674426574742355349194934&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;96983520312774506326239578318016984801869478851843&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;85861560789112949495459501737958331952853208805511&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;12540698747158523863050715693290963295227443043557&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;66896648950445244523161731856403098711121722383113&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;62229893423380308135336276614282806444486645238749&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;30358907296290491560440772390713810515859307960866&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;70172427121883998797908792274921901699720888093776&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;65727333001053367881220235421809751254540594752243&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;52584907711670556013604839586446706324415722155397&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;53697817977846174064955149290862569321978468622482&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;83972241375657056057490261407972968652414535100474&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;82166370484403199890008895243450658541227588666881&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;16427171479924442928230863465674813919123162824586&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;17866458359124566529476545682848912883142607690042&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;24219022671055626321111109370544217506941658960408&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;07198403850962455444362981230987879927244284909188&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;84580156166097919133875499200524063689912560717606&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;05886116467109405077541002256983155200055935729725&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> <span style="color: #666666;">&quot;71636269561882670428252483600823257530420752963450&quot;</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> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToUInt64</span><span style="color: #008000;">&#40;</span>x<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;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
var maxProduct <span style="color: #008000;">=</span> 0UL<span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> d<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; var product <span style="color: #008000;">=</span> d<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">*</span> d<span style="color: #008000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">*</span> d<span style="color: #008000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">*</span> d<span style="color: #008000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">*</span> d<span style="color: #008000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>product <span style="color: #008000;">&gt;</span> maxProduct<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; maxProduct <span style="color: #008000;">=</span> product<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
var answer <span style="color: #008000;">=</span> maxProduct<span style="color: #008000;">;</span></div></div>
<p>Nothing fancy, but works nicely. Finishes in around 3 milliseconds.</p>
<p>How did you do it? Do you have a more clever or efficient way to do this? Please 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/09/24/project-euler-problem-8/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
