<?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>CodeMocha &#187; algorithm</title>
	<atom:link href="http://codemocha.com/tag/algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemocha.com</link>
	<description>Freshly ground code.  Thoughts on software and algorithms by Chet Mancini</description>
	<lastBuildDate>Fri, 18 Sep 2009 15:22:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Euclidean Algorithm</title>
		<link>http://codemocha.com/2009/07/euclidean-algorithm/</link>
		<comments>http://codemocha.com/2009/07/euclidean-algorithm/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 03:46:41 +0000</pubDate>
		<dc:creator>Chet</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[ml]]></category>

		<guid isPermaLink="false">http://codemocha.com/?p=9</guid>
		<description><![CDATA[One of my favorite algorithms is the simple, elegant Euclidean algorithm for finding the greatest common denominator of two numbers.  In ML or another functional language it really is beautiful:
12fun gcd&#40;a, 0&#41; = a
&#160; &#160;&#124; gcd&#40;a, b&#41; = gcd&#40;b, a mod b&#41;;
Of course, the same can be done in Java iteratively:
123456789public int gcd&#40;int a, [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favorite algorithms is the simple, elegant Euclidean algorithm for finding the greatest common denominator of two numbers.  In ML or another functional language it really is beautiful:<span id="more-9"></span></p>
<div class="codecolorer-container ocaml vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br /></div></td><td><div class="ocaml codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">fun</span> gcd<span class="br0">&#40;</span>a, <span class="nu0">0</span><span class="br0">&#41;</span> <span class="sy0">=</span> a<br />
&nbsp; &nbsp;<span class="sy0">|</span> gcd<span class="br0">&#40;</span>a, b<span class="br0">&#41;</span> <span class="sy0">=</span> gcd<span class="br0">&#40;</span>b, a <span class="kw1">mod</span> b<span class="br0">&#41;</span><span class="sy0">;</span></div></td></tr></tbody></table></div>
<p>Of course, the same can be done in Java iteratively:</p>
<div class="codecolorer-container java vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="java codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">public</span> <span class="kw4">int</span> gcd<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">int</span> c<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>b<span class="sy0">&gt;</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c <span class="sy0">=</span> a<span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a <span class="sy0">=</span> b<span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; b <span class="sy0">=</span> c<span class="sy0">%</span>b<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> a<span class="sy0">;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>Or recursively:</p>
<div class="codecolorer-container java vibrant" style="overflow:auto;white-space:nowrap;width:600px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="java codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">public</span> <span class="kw4">int</span> gcd<span class="br0">&#40;</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>b<span class="sy0">==</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> a<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><span class="kw1">else</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> gcd<span class="br0">&#40;</span>b, a<span class="sy0">%</span>b<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://codemocha.com/2009/07/euclidean-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
