<?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>Ståle&#039;s weblog &#187; Math</title>
	<atom:link href="http://blog.staale.org/tag/math/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.staale.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 18 Oct 2010 07:43:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Euler problem 3 in Scala</title>
		<link>http://blog.staale.org/2009/03/euler-problem-3-in-scala.html</link>
		<comments>http://blog.staale.org/2009/03/euler-problem-3-in-scala.html#comments</comments>
		<pubDate>Wed, 25 Mar 2009 13:28:13 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Euler]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=64</guid>
		<description><![CDATA[The third project Euler problem is about finding the largest divisor for a single number: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 One small &#8220;problem&#8221; here is that the number 600851475143 exceeds Integer.MAX_VALUE which is 2147483647, so we have to use [...]]]></description>
			<content:encoded><![CDATA[<p>The third project Euler problem is about finding the largest divisor for a single number:</p>
<blockquote><p>The prime factors of 13195 are 5, 7, 13 and 29.</p>
<p>What is the largest prime factor of the number 600851475143</p></blockquote>
<p>One small &#8220;problem&#8221; here is that the number 600851475143 exceeds Integer.MAX_VALUE which is 2147483647, so we have to use either Long or BigInt. I went for BigInt for this one. The algorithm I choose is pretty simple:</p>
<p>Take the number n, and divisor d:</p>
<ul>
<li>if n mod d is 0, divide n by d and check again</li>
<li>if n is larger than d, increase d by 1 and check again</li>
<li>d is the answer</li>
</ul>
<p>In Scala code, the method turns out as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> largestDivisor<span style="color: #F78811;">&#40;</span>n<span style="color: #000080;">:</span>BigInt, d<span style="color: #000080;">:</span>BigInt<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>BigInt <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>n<span style="color: #000080;">%</span>d <span style="color: #000080;">==</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> largestDivisor<span style="color: #F78811;">&#40;</span>n/d,d<span style="color: #F78811;">&#41;</span> 
    <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>n<span style="color: #000080;">&gt;</span>d<span style="color: #F78811;">&#41;</span> largestDivisor<span style="color: #F78811;">&#40;</span>n,d+<span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> 
    <span style="color: #0000ff; font-weight: bold;">else</span> d
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">def</span> largestDivisor<span style="color: #F78811;">&#40;</span>n<span style="color: #000080;">:</span>BigInt<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> BigInt <span style="color: #000080;">=</span> largestDivisor<span style="color: #F78811;">&#40;</span>n,<span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>We have a convinience method here to help us out, as we always start the divisor with a value off 2. If we started with 1 we would have an endless loop, as all numbers mod 1 is 0. It&#8217;s then just a matter of printing out the result.</p>
<p>Again we are using tail recursion, which Scala will rewrite to a loop instead. To look at the actual result off the code, I ran the resulting class file through <a href="http://www.varaneckas.com/jad">jad</a>, and got the following result:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> BigInt largestDivisor<span style="color: #009900;">&#40;</span>BigInt n, BigInt d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">;</span> BoxesRunTime.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>n.$percent<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span>, BoxesRunTime.<span style="color: #006633;">boxToInteger</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> n <span style="color: #339933;">=</span> n.$div<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>n.$greater<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                d <span style="color: #339933;">=</span> d.$plus<span style="color: #009900;">&#40;</span>BigInt$.<span style="color: #006633;">MODULE</span>$.<span style="color: #006633;">int2bigInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #000000; font-weight: bold;">return</span> d<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In line 3 we see the continual division off n/d as long as n%d == 0. This corresponds exactly to my Scala code in line 2, recursivly calling it self while dividing n by d. Line 4-7 corresponds to line 3-4 in the Scala progam, increasing d by 1 if d is less than n, or returning d. By running my Scala compiled class file through jad, I am able to exactly see how Scala converts tail recursion to loop code.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/03/euler-problem-3-in-scala.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

