<?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>diehealthy.org &#187; math</title>
	<atom:link href="http://diehealthy.org/category/math/feed" rel="self" type="application/rss+xml" />
	<link>http://diehealthy.org</link>
	<description>The technopolitical world needs thinkers.</description>
	<lastBuildDate>Fri, 03 Sep 2010 01:37:29 +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>The Euclidean Algorithm</title>
		<link>http://diehealthy.org/math/euclidean-algorithm</link>
		<comments>http://diehealthy.org/math/euclidean-algorithm#comments</comments>
		<pubDate>Fri, 12 Oct 2007 21:11:37 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Euclidean Algorithm]]></category>
		<category><![CDATA[GCD]]></category>

		<guid isPermaLink="false">http://diehealthy.org/math/euclidean-algorithm</guid>
		<description><![CDATA[Implementation of the Euclidean Algorithm for finding the Greatest Common Divisor of two integers.<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>This post regards the <a href="http://en.wikipedia.org/wiki/Euclidean_algorithm">Euclidean Algorithm</a> for determining the <a href="http://en.wikipedia.org/wiki/Greatest_common_divisor">Greatest Common Divisor (GCD)</a> of two integers.</p>
<p>I happened to read about this fun and simple algorithm and decided since I&#8217;ve been doing some <a href="http://en.wikipedia.org/wiki/C_programming_language">C</a> programming to take a little time to implement this in C. The version below outputs to the console via printf, but could easily be modified to return a value and thus be used as part of other programs.</p>
<p>Here it is:</p>
<pre>
[snip out GPL notice, it's in the actual file though]
#include &lt;stdio .h&gt;
#include &lt;stdlib .h&gt;
#include &lt;limits .h&gt;
#include &lt;errno .h&gt;

int
main(int argc, char **argv)
{
&nbsp;&nbsp;int a,b,r; // declare variables

&nbsp;&nbsp;if (argc &lt; 3)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;printf("usage: gcd &lt;value&gt; &lt;value&gt; where neither == 0\n");
&nbsp;&nbsp;&nbsp;&nbsp;exit(-1);
&nbsp;&nbsp;}

&nbsp;&nbsp;b = strtol(argv[1], NULL, 10); // get values from command line
&nbsp;&nbsp;if (errno != 0 &#038;&#038; (b == LONG_MIN || b == LONG_MAX)) // ensure valid input
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;perror("Bad argument");
&nbsp;&nbsp;&nbsp;&nbsp;exit(-1);
&nbsp;&nbsp;}

&nbsp;&nbsp;r = strtol(argv[2], NULL, 10);
&nbsp;&nbsp;if (errno != 0 &#038;&#038; (r == LONG_MIN || r == LONG_MAX))
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;perror("Bad argument");
&nbsp;&nbsp;&nbsp;&nbsp;exit(-1);
&nbsp;&nbsp;}

&nbsp;&nbsp;if (b &lt; r) // order them properly
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;a = r;
&nbsp;&nbsp;&nbsp;&nbsp;r = b;
&nbsp;&nbsp;&nbsp;&nbsp;b = a;
&nbsp;&nbsp;}

&nbsp;&nbsp;while (r != 0) // keep going until GCD found (worst case it's 1)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;a = b; // shuffle values to correct spots
&nbsp;&nbsp;&nbsp;&nbsp;b = r;

&nbsp;&nbsp;&nbsp;&nbsp;if ((r = a % b) == 0) // compute the remainder, if it's 0 then GCD found
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("The GCD is: %d\n", b);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(0);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&nbsp;&nbsp;printf("usage: gcd &lt;value&gt; &lt;value&gt; where neither == 0\n");
&nbsp;&nbsp;exit(-1);
}

</pre>
<p>If I&#8217;ve made any grievous errors or there&#8217;s just a better way please let me know. This code is released under the GPL.</p>
<p>Also, you can download this file here: <a href="http://diehealthy.org/code/gcd.c">gcd.c</a></p>
<p>An example of input/output:</p>
<blockquote><div class="corners-top">
<div></div>
</div>
<p>./gcd 184965 385<br />
The GCD is: 55
<div class="corners-bottom">
<div></div>
</div>
</blockquote>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://diehealthy.org/math/euclidean-algorithm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
