<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Geekality</title>
	
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Tue, 09 Mar 2010 15:24:59 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/geekality" /><feedburner:info uri="geekality" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license><image><link>http://creativecommons.org/licenses/by-nc-sa/3.0/</link><url>http://creativecommons.org/images/public/somerights20.gif</url><title>Some Rights Reserved</title></image><item>
		<title>Interlude</title>
		<link>http://feedproxy.google.com/~r/geekality/~3/roOOurCA2O8/</link>
		<comments>http://www.geekality.net/2010/02/05/interlude/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 18:43:23 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Life]]></category>
		<category><![CDATA[Update]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=898</guid>
		<description><![CDATA[Not much is happening at the moment. Well, actually quite a bit is happening. It&#8217;s just that I find myself in a rather non-eventful interlude of some sort. 
Last Friday (January, 29th) I had my last day at SMS Development &#038; Support AS where I have been working for the last 1.5 years. It was [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.geekality.net/wp-content/uploads/2010/02/Ink-blot-247x300.jpg" alt="Ink blot" title="Ink blot" width="247" height="300" class="alignright size-medium wp-image-900" />Not much is happening at the moment. Well, actually quite a bit is happening. It&#8217;s just that I find myself in a rather non-eventful interlude of some sort. </p>
<p>Last Friday (January, 29th) I had my last day at <a href="http://www.smsdev.no/">SMS Development &#038; Support AS</a> where I have been working for the last 1.5 years. It was my very first job as a full-time software developer and I learned <em>a lot</em>. Both about developing software and about myself as a developer. Good stuff!</p>
<p>Next up I will return to <a href="http://www.lifestyletv.se">LifeStyleTV</a> where I spent a year as a <a href="http://www.mediamissionary.se">Media Missionary</a>. This time I will be working there as a regular employee and help out with various things. Mainly I will be developing some internal software systems with some of the others there, but I will most likely also help out with the TV production. I am going down there the 11th of February and I am looking forward to get started. <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>What am I doing now, during this interlude? Not a lot. Very little. Extremely little. My computer is dead. I&#8217;m sick. My energy reserves are non-existent. I&#8217;m sleeping, eating, watching tv, reading&#8230; and that&#8217;s pretty much it&#8230; To be honest I am actually kind of happy I am sick now instead of earlier or later. Just hope I will be back to normal before Thursday&#8230; <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':|' title=':|' class='wp-smiley smiley-11' /> </p>
<p><img src="http://www.geekality.net/wp-content/uploads/2010/02/Sleepy-dog-300x234.jpg" alt="Sleepy dog" title="Sleepy dog" width="300" height="234" class="aligncenter size-medium wp-image-902" /></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/geekality?a=roOOurCA2O8:umu_AThIET4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/geekality?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=roOOurCA2O8:umu_AThIET4:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/geekality?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=roOOurCA2O8:umu_AThIET4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/geekality?i=roOOurCA2O8:umu_AThIET4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/geekality/~4/roOOurCA2O8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/02/05/interlude/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.geekality.net/2010/02/05/interlude/</feedburner:origLink></item>
		<item>
		<title>How to check for duplicates</title>
		<link>http://feedproxy.google.com/~r/geekality/~3/8BWhG6V19GM/</link>
		<comments>http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 16:57:09 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Extension methods]]></category>
		<category><![CDATA[IEnumerable]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=884</guid>
		<description><![CDATA[Say you have an IEnumerable&#60;T&#62; of some sort and you want to check if it contains any duplicates. How do you do that?
Terrible solution
I needed to do this a while ago and the first solution that hit me wasn&#8217;t exactly good. Linq has method called Distinct, which returns the distinct items in a sequence (weeds [...]]]></description>
			<content:encoded><![CDATA[<p>Say you have an <code class="codecolorer text default"><span class="text">IEnumerable&lt;T&gt;</span></code> of some sort and you want to check if it contains any duplicates. How do you do that?</p>
<h2>Terrible solution</h2>
<p>I needed to do this a while ago and the first solution that hit me wasn&#8217;t exactly good. Linq has method called Distinct, which returns the distinct items in a sequence (weeds out duplicates). Using that to check if duplicates exists you can do something like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var hasDuplicates <span style="color: #008000;">=</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; subjects.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> subjects.<span style="color: #0000FF;">Distinct</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>This is of course a <strong><em>really bad</em></strong> solution. So <em>don&#8217;t</em> use it! <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<h2>Good solution</h2>
<p>The much better solution is to use a <code class="codecolorer text default"><span class="text">HashSet&lt;T&gt;</span></code>.</p>
<blockquote><p>The <a href="http://msdn.microsoft.com/en-us/library/bb359438.aspx">HashSet&lt;T&gt;</a> class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.</p></blockquote>
<p>In addition to the fact that it cannot contain duplicate items, it also has a very nice add-method which returns false if the item is already in the set.</p>
<p>To check for duplicates using a hash set we can start out by assuming there is no duplicates and then add items to the hash set until we either run out of items, or we try to add an item that already exists. In the first case our assumption was right and in the second it turned out to be wrong.</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 set <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> HashSet<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
var hasDuplicates <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var s <span style="color: #0600FF;">in</span> subjects<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>set.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>s<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hasDuplicates <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; break<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></div>
<h2>Good and handy solution</h2>
<p>Since we don&#8217;t want to type in that stuff over and over again we want this snippet in a nice extension method. Code snippet coming up <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </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;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">namespace</span> Geekality.<span style="color: #0000FF;">Snippets</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> EnumerableExtensions<br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> HasDuplicates<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> subjects<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> HasDuplicates<span style="color: #000000;">&#40;</span>subjects, EqualityComparer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>.<span style="color: #0600FF;">Default</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> HasDuplicates<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> subjects, IEqualityComparer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> comparer<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>subjects <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;subjects&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>comparer <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;comparer&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var set <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> HashSet<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>comparer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var s <span style="color: #0600FF;">in</span> subjects<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>set.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>s<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Simple and handy. You use it for example like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>subjects.<span style="color: #0000FF;">HasDuplicates</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Do something</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Hope it can help someone, somewhere, below and/or over the rainbow <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>Let me know what you think. I&#8217;m always open for improvements and feedback <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='8)' title='8)' class='wp-smiley smiley-3' /> </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/geekality?a=8BWhG6V19GM:UfTkJ5QE3pk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/geekality?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=8BWhG6V19GM:UfTkJ5QE3pk:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/geekality?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=8BWhG6V19GM:UfTkJ5QE3pk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/geekality?i=8BWhG6V19GM:UfTkJ5QE3pk:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/geekality/~4/8BWhG6V19GM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/</feedburner:origLink></item>
		<item>
		<title>Test-Driven Development: By Example</title>
		<link>http://feedproxy.google.com/~r/geekality/~3/uO0OkNj91FA/</link>
		<comments>http://www.geekality.net/2009/12/01/test-driven-development-by-example/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 17:36:16 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Reviews]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=858</guid>
		<description><![CDATA[I earlier wrote about the book, The Art of Unit Testing, which I finished a while ago. That book was very good and was focused on how to write good unit tests. It also mentioned Test-Driven Development, TDD, but not too much. The book I read next, which I finished a few days ago, was [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.geekality.net/wp-content/uploads/2009/12/Test-Driven-Development-By-Example-Cover-239x300.jpg" alt="Test Driven Development By Example (Cover)" title="Test Driven Development By Example (Cover)" width="239" height="300" class="alignright size-medium wp-image-861" />I earlier wrote about the book, <a href="http://www.geekality.net/2009/11/03/the-art-of-unit-testing/">The Art of Unit Testing</a>, which I finished a while ago. That book was very good and was focused on how to write good unit tests. It also mentioned <a href="http://en.wikipedia.org/wiki/Test-driven_development">Test-Driven Development,</a> TDD, but not too much. The book I read next, which I finished a few days ago, was kind of the other way around. Pretty much only about TDD. And from the title, <em>Test-Driven Development: By Example</em>, that shouldn&#8217;t be much of a shocker <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<p><span id="more-858"></span></p>
<p>The book is written by <a href="http://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a> and explains the basics about TDD. It does this pretty well, step by step, by example, just like the title says. The book also talks some about TDD in a more general sense. What it is, how it works, why it works, et cetera.</p>
<p>From the back-cover:</p>
<blockquote><p>Readers will learn to:
<ul>
<li>Solve complicated tasks, beginning with the simple and proceeding to the more complex.</li>
<li>Write automated tests before coding.</li>
<li>Grow a design organically by refactoring to add design decisions one at a time.</li>
<li>Create tests for more complicated logic, including reflection and exceptions.</li>
<li>Use patterns to decide what tests to write.</li>
<li>Create tests using xUnit, the architecture at the heart of many programmer-oriented testing tools.</li>
</ul>
</blockquote>
<p>It pretty much kept the promises <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> The only part I didn&#8217;t really like about this book was that I found much of the coding kind of messy. Could be because the languages used in the examples are Java and Python, which I don&#8217;t really like that much syntax-wise. An even more important reason though, is that I just finished reading The Art of Unit Testing <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> So, I suppose I have become a bit demanding when it comes to cleanliness when coding and writing unit-tests. I suppose that is a good thing <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> (I do however not claim in any way that the code I currently write is extremely clean in any way! But I do strive for it to be so <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=';)' title=';)' class='wp-smiley smiley-20' /> )</p>
<p>I can really recommend this book to anyone who are curious to get familiar with TDD. I certainly see the value of doing TDD and is in the process of trying to incorporate it into my development habits.</p>
<blockquote><ol>
<li>Red &#8212; Write a little test that doesn&#8217;t work, and perhaps doesn&#8217;t even compile at first.</li>
<li>Green &#8212; Make the test work quicky, committing whatever sins necessary in the process.</li>
<li>Refactor &#8212; Eliminate all of the duplication created in merely getting the test to work.</li>
</ol>
<p>Red/green/refactor &#8212; the TDD mantra.</p></blockquote>
<p>You can get it at <a href="http://amzn.com/0321146530">Amazon</a>, and probably a lot of other places too as it is a pretty well-known book.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/geekality?a=uO0OkNj91FA:7qQTJouO4UE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/geekality?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=uO0OkNj91FA:7qQTJouO4UE:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/geekality?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=uO0OkNj91FA:7qQTJouO4UE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/geekality?i=uO0OkNj91FA:7qQTJouO4UE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/geekality/~4/uO0OkNj91FA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/12/01/test-driven-development-by-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekality.net/2009/12/01/test-driven-development-by-example/</feedburner:origLink></item>
		<item>
		<title>How to test asynchronous events</title>
		<link>http://feedproxy.google.com/~r/geekality/~3/aaSDsSBOx7k/</link>
		<comments>http://www.geekality.net/2009/11/26/how-to-test-asynchronous-events/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 18:59:59 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Async]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=826</guid>
		<description><![CDATA[The other day I had to test that an event was raised after some asynchronous work had been done. And since I currently am a total test newbie, this was a new thing for me. Say we have this simple shell of a class:
public class Worker
&#123;
&#160; &#160; public event EventHandler&#60;EventArgs&#62; Done;

&#160; &#160; public void Start&#40;&#41;
&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I had to test that an event was raised after some asynchronous work had been done. And since I currently am a total test newbie, this was a new thing for me. Say we have this simple shell of a class:</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;">public</span> <span style="color: #FF0000;">class</span> Worker<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> EventHandler<span style="color: #008000;">&lt;</span>EventArgs<span style="color: #008000;">&gt;</span> Done<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Start<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;...<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Let&#8217;s just assume it does some work, and is supposed to raise the Done event when it is&#8230; well&#8230; done. <!-- AUSCVUHQEPXB --></p>
<p><span id="more-826"></span></p>
<h2>Synchronous worker</h2>
<p>If the Worker class works synchronously, it would be no problem at all to test it. For example:</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: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WorkerTests<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Start_WhenFinished_DoneEventIsRaised<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var worker <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Worker<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; var eventWasRaised <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; worker.<span style="color: #0000FF;">Done</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span>s, e<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> eventWasRaised <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; worker.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Assert.<span style="color: #0000FF;">That</span><span style="color: #000000;">&#40;</span>eventWasRaised<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>We simply add an event handler which sets a boolean flag when called, start the worker and then check if the flag was set or not. Easy peasy.</p>
<h2>Asynchronous worker</h2>
<p>What now if the Start method just sends the work to a new thread and returns immediately? Our current test can not be trusted at all in that case. It might pass and it might fail. Most likely fail. All depending on if it managed to finish its work and raise the event before we check the flag. And that would be unlikely to happen. Highly unlikely.</p>
<p>So what do we do? We use a <a href="http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx">ManualResetEvent</a> instead of the boolean flag:</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: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WorkerTests<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Start_WhenFinished_DoneEventIsRaised<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var worker <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Worker<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; var eventWasRaised <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ManualResetEvent<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; worker.<span style="color: #0000FF;">Done</span> <span style="color: #008000;">+=</span> <span style="color: #000000;">&#40;</span>s, e<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> eventWasRaised.<span style="color: #0000FF;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; worker.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Assert.<span style="color: #0000FF;">That</span><span style="color: #000000;">&#40;</span>eventWasRaised.<span style="color: #0000FF;">WaitOne</span><span style="color: #000000;">&#40;</span>500<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Instead of checking that a flag has been set, we check that the event handler gives us a signal within a certain period of time. If it doesn&#8217;t give us that signal we can assume that the event was never raised or that the work simply took longer than we expected. Either case could probably be of interest <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>Brilliant, simple and readable. Just how we like it <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='8)' title='8)' class='wp-smiley smiley-3' /> </p>
<h2>Credits</h2>
<p>I am still a total testing newbie at the moment and didn&#8217;t have a clue what to do, so after some failing google-fu I asked <a href="http://stackoverflow.com/questions/1770830/c-how-to-test-a-basic-threaded-worker-class">a question</a> about it at <a href="http://stackoverflow.com">StackOverflow</a>. My solution is based on <a href="http://stackoverflow.com/questions/1770830/c-how-to-test-a-basic-threaded-worker-class/1770862#1770862">one of the answers</a> I got there <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/geekality?a=aaSDsSBOx7k:sVFn_Y9kq8A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/geekality?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=aaSDsSBOx7k:sVFn_Y9kq8A:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/geekality?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=aaSDsSBOx7k:sVFn_Y9kq8A:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/geekality?i=aaSDsSBOx7k:sVFn_Y9kq8A:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/geekality/~4/aaSDsSBOx7k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/26/how-to-test-asynchronous-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekality.net/2009/11/26/how-to-test-asynchronous-events/</feedburner:origLink></item>
		<item>
		<title>Generics and checking for null</title>
		<link>http://feedproxy.google.com/~r/geekality/~3/hLKGjjCEr40/</link>
		<comments>http://www.geekality.net/2009/11/13/generics-and-checking-for-null/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 16:19:15 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Null]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=788</guid>
		<description><![CDATA[When writing C#, in Visual Studio, using generics&#8230; have you ever tried checking for null? I have always found that a bit of a hassle. 
Say we have this method which returns the subject if it is not null, and the result of a createNew() function if it is null.
public static T NewIfNull&#60;T&#62;&#40;this T subject, [...]]]></description>
			<content:encoded><![CDATA[<p>When writing C#, in Visual Studio, using generics&#8230; have you ever tried checking for null? I have always found that a bit of a hassle. </p>
<p>Say we have this method which returns the subject if it is not null, and the result of a <code class="codecolorer text default"><span class="text">createNew()</span></code> function if it <em>is</em> null.</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;">public</span> <span style="color: #0600FF;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>subject <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> createNew<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Not claiming this is a very useful method, but it was the simplest thing I could come up with <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Anyways, in Visual Studio you will now probably have a blue (is blue here at least) squiggly line under the <a href="http://msdn.microsoft.com/en-us/library/53k8ybth.aspx">equality operator</a>. It states you are doing a <cite>Possible compare of value type with &#8216;null&#8217;</cite>, which of course is reasonable and correct. We <em>could</em> just ignore it and move on&#8230; but we don&#8217;t really like squiggly lines, do we? I sure don&#8217;t&#8230; so lets get rid of it.</p>
<p><span id="more-788"></span></p>
<h2>Faulty solution</h2>
<p>ReSharper suggests that I use <a href="http://msdn.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx">default(T)</a> instead of null, like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>subject <span style="color: #008000;">==</span> <span style="color: #0600FF;">default</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> createNew<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Now, this won&#8217;t do at all. First of all, we don&#8217;t really want to check for <a href="http://msdn.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx">default(T)</a>, we want to check for null. And second of all, we now have a long red squiggly line under the whole equality statement! It says that it <cite>Cannot apply operator &#8216;==&#8217; to operands of type &#8216;T&#8217; and &#8216;T&#8217;</cite>. That messages doesn&#8217;t really make much sense to me, to be honest, but I trust that it is true. Either way, we have to get rid of it.</p>
<h2>Good solution</h2>
<p><a href="http://msdn.microsoft.com/en-us/library/system.object.referenceequals(VS.80).aspx">ReferenceEquals</a> to the rescue! We can use that method, instead of the equality operator, and compare with null like we intended! <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> </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;">public</span> <span style="color: #0600FF;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ReferenceEquals<span style="color: #000000;">&#40;</span>subject, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> createNew<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>That is kind of ugly though&#8230; And it is <em>so</em> much longer to type. <em>And</em>, most importantly, it kind of makes the code a bit unreadable&#8230; At least if you have a bunch of things you have to check for null, like I did today.</p>
<p>But fear not! Today I came up with a tiny, but brilliant extension method which makes things a lot better:</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;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> IsNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> T subject<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">return</span> ReferenceEquals<span style="color: #000000;">&#40;</span>subject, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>subject.<span style="color: #0000FF;">IsNull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> createNew<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Doesn&#8217;t that just look <em>a lot</em> cleaner? I think so <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> </p>
<p>And yes, I know this isn&#8217;t really a big issue. And I realize you have probably all come up with something similar already. Yes, I am slow <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> But hey, I figured it might be worth sharing this anyways, just in case you hadn&#8217;t thought about it before. I sure hadn&#8217;t! And I really do think that tiny snippet of code made a huge difference in code readability. And we all want that, don&#8217;t we? Yes, yes we do&#8230;</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/geekality?a=hLKGjjCEr40:3WI-FdaoBpI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/geekality?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=hLKGjjCEr40:3WI-FdaoBpI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/geekality?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=hLKGjjCEr40:3WI-FdaoBpI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/geekality?i=hLKGjjCEr40:3WI-FdaoBpI:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/geekality/~4/hLKGjjCEr40" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/13/generics-and-checking-for-null/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekality.net/2009/11/13/generics-and-checking-for-null/</feedburner:origLink></item>
		<item>
		<title>Project Euler: Problem 25</title>
		<link>http://feedproxy.google.com/~r/geekality/~3/42dQsXz-HGo/</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 Fibonacci sequence to contain 1000 digits?

Solution
We touched on the Fibonacci sequence a while ago in the second euler problem. [...]]]></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;">public</span> <span style="color: #FF0000;">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: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>IntX<span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var a <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> IntX<span style="color: #000000;">&#40;</span>0<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var b <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> IntX<span style="color: #000000;">&#40;</span>1<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield <span style="color: #0600FF;">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: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; IEnumerator IEnumerable.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> GetEnumerator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#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> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> LargeFibonacciSequence<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var e <span style="color: #008000;">=</span> sequence.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">MoveNext</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> e.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&lt;</span> 1000<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; n<span style="color: #008000;">++;</span><br />
<span style="color: #000000;">&#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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/geekality?a=42dQsXz-HGo:YHvyn2n0qWc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/geekality?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=42dQsXz-HGo:YHvyn2n0qWc:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/geekality?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=42dQsXz-HGo:YHvyn2n0qWc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/geekality?i=42dQsXz-HGo:YHvyn2n0qWc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/geekality/~4/42dQsXz-HGo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/06/project-euler-problem-25/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekality.net/2009/11/06/project-euler-problem-25/</feedburner:origLink></item>
		<item>
		<title>Project Euler: Problem 16</title>
		<link>http://feedproxy.google.com/~r/geekality/~3/UTMCUHw8hdU/</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 much as easy as you&#8217;d think.
var answer = IntX
&#160; &#160; &#160; &#160; .Pow&#40;2, 1000&#41;
&#160; &#160; &#160; &#160; [...]]]></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: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">1000</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">-</span> <span style="color: #666666;">'0'</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#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 (&#8216;0&#8242;, &#8216;1&#8242;, and so on) comes after each other, which means that for example &#8216;1&#8242; has a character code value which is 1 larger than the character code of &#8216;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 &#8216;0&#8242; from &#8216;0&#8242;, we get 0. And if we subtract &#8216;0&#8242; from &#8216;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>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/geekality?a=UTMCUHw8hdU:_vDNVNuo7QM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/geekality?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=UTMCUHw8hdU:_vDNVNuo7QM:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/geekality?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/geekality?a=UTMCUHw8hdU:_vDNVNuo7QM:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/geekality?i=UTMCUHw8hdU:_vDNVNuo7QM:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/geekality/~4/UTMCUHw8hdU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/05/project-euler-problem-16/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekality.net/2009/11/05/project-euler-problem-16/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 18.483 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-03-11 09:33:55 --><!-- Compression = gzip -->
