<?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; Fibonacci</title>
	<atom:link href="http://www.geekality.net/tag/fibonacci/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 2</title>
		<link>http://www.geekality.net/2009/09/17/project-euler-problem-2/</link>
		<comments>http://www.geekality.net/2009/09/17/project-euler-problem-2/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 20:18:17 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fibonacci]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=165</guid>
		<description><![CDATA[Alright, next Project Euler problem: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, &#8230; <a href="http://www.geekality.net/2009/09/17/project-euler-problem-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Alright, next Project Euler problem:</p>
<blockquote><p>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:</p>
<p style="text-align: center">1, 2, 3, 5, 8, 13, 21, 34, 55, 89, &#8230;</p>
<p>Find the sum of all the even-valued terms in the sequence which do not exceed four million.</p></blockquote>
<p><span id="more-165"></span></p>
<h2>Solution</h2>
<p>I pretty soon decided I wanted to solve these problems in a good way, and also to sort of try to build up a collection of useful tools that I could hopefully use when solving later Euler problems as well. And even if I didn&#8217;t find any use for them later, at least I would have gotten some practice when making them <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<p>So, the first thing I figured I needed, was a source of Fibonacci numbers. So I created an interface for it, and an implementation (in case I wanted to swap out the implementation more easily later).</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IFibonacciSequence <span style="color: #008000;">:</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> FibonacciSequence <span style="color: #008000;">:</span> IFibonacciSequence<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region IFibonacciSequence Members</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var a <span style="color: #008000;">=</span> 0UL<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var b <span style="color: #008000;">=</span> 1UL<span style="color: #008000;">;</span><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 />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> c<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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; &nbsp; &nbsp; a <span style="color: #008000;">=</span> b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #008000;">=</span> c<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; IEnumerator IEnumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">#endregion</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></div>
<p>After having created that class, solving the problem was pretty straight forward with a simple LINQ statement. I simple start taking numbers from the generator, filtering out the ones that are not even, up till I reach the highest number, and then I sum all of those up.</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> FibonacciSequence<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;">Where</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">%</span>2 <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">TakeWhile</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">4000000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Aggregate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>sum, x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> sum <span style="color: #008000;">+</span> x<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>Voila <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> The reason why I don&#8217;t use the <code class="codecolorer text default"><span class="text">Sum</span></code>method is that it doesn&#8217;t seem to be one for <code class="codecolorer text default"><span class="text">ulong</span></code>, only for <code class="codecolorer text default"><span class="text">long</span></code>. And I decided that I for the first would try to use <code class="codecolorer text default"><span class="text">long</span></code> instead of <code class="codecolorer text default"><span class="text">int</span></code> since the answer to some of these problems seems to be quite large. And to use <code class="codecolorer text default"><span class="text">ulong</span></code> here since there aren&#8217;t really any need for negative numbers&#8230;</p>
<p>Anyways, that was my solution to this problem. According to my test results, it takes less than 5 ms to find it, which is fast enough to me. </p>
<p>So, what do you think? How did you do it?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/09/17/project-euler-problem-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
