<?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; Collatz conjecture</title>
	<atom:link href="http://www.geekality.net/tag/collatz-conjecture/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 14</title>
		<link>http://www.geekality.net/2009/10/19/project-euler-problem-14/</link>
		<comments>http://www.geekality.net/2009/10/19/project-euler-problem-14/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 17:47:38 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Collatz conjecture]]></category>

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