<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>8th Light Blog: Ruby and the Art of Computer Programming</title>
    <link>http://blog.8thlight.com/articles/2007/11/06/ruby-and-the-art-of-computer-programming</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In the minds of the craftsmen...</description>
    <item>
      <title>Ruby and the Art of Computer Programming</title>
      <description>&lt;p&gt;Recently, I started to read the &lt;a href="http://www.amazon.com/Art-Computer-Programming-Volumes-Boxed/dp/0201485419/ref=pd_bbs_sr_1/104-8075516-3966305?ie=UTF8&amp;amp;s=books&amp;amp;qid=1194361896&amp;amp;sr=8-1"&gt;Knuth&amp;#8217;s Art of Computer Science&lt;/a&gt;.  To spice up the exercises, I am writing them out in ruby.  Thinking about the basic math of programming and how to implement it in a high level language like ruby has been fun.  The evolution of programming languages has gone far since the book was written.  We can use cool new ruby syntax to do multiple steps in an algorithm!&lt;/p&gt;

&lt;p&gt;Also, this is an exercise in writing clean code.  Some of the algorithms in the book are a bit hard to read in there pure math form.  So, when I write them out in ruby, I try to use as much expressiveness without adding clutter.  This is no easy task, and often times I have to walk away from an algorithm for awhile and come back to it, because I will have my head deep in the math and less in the code. Or vice versa.&lt;/p&gt;

&lt;p&gt;I was showing  one of my solutions to a colleague of mine, &lt;a href="http://blog.8thlight.com/doug"&gt;Doug Bradbury&lt;/a&gt;, who saw a better way in ruby to solve the same problem I was with less lines of code and a higher readability.  So, I decided to share one of the problems, and in a few days, I will post my version of the solution.  We can see different solutions in different languages and different styles.  Go ahead and try it out.&lt;/p&gt;

&lt;p&gt;Here is the algorithm as written in the book.&lt;/p&gt;

&lt;p&gt;This is Euclid&amp;#8217;s Algorithm for the greatest common divisor&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
E0 [Ensure m &gt;= n.] If m &amp;lt; n, exchange m &amp;lt;--&gt; n.&lt;br&gt;
E1 [Find remainder.] Divide m by n and let r be the remainder.  (We will have 0 &amp;lt;= r &amp;lt; n.)&lt;br&gt;
E2 [Is it zero?]  If r = 0, the algorithm terminates;n is the answer.&lt;br&gt;
E3 [Reduce.]  Set m &amp;lt;- n, n &amp;lt;- r, and go back to step E1.&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;UPDATE: Here is a solution&lt;/b&gt;&lt;/p&gt;

&lt;pre&gt;

def are_whole_numbers?(*numbers)
  numbers.each {|number| return false if number.to_i.to_f != number}
  return true
end

def euclid(m, n)
  raise "Must be whole numbers." unless are_whole_numbers?(m, n)
  return euclid(n, m) if m &lt; n

  remainder = m % n

  return n if remainder == 0
  return euclid(n, remainder)
end

#Here are some examples
puts euclid(35.0, 40.0) #should be 17.0
puts euclid(119.0, 544.0) #should be 17.0
puts euclid(555.0, 666.0) #should be 111.0

&lt;/pre&gt;</description>
      <pubDate>Tue, 06 Nov 2007 15:09:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d1449d6a-dd78-4345-8442-b35f58b442ac</guid>
      <author>Paul Pagel</author>
      <link>http://blog.8thlight.com/articles/2007/11/06/ruby-and-the-art-of-computer-programming</link>
      <category>Coding</category>
      <category>Paul</category>
    </item>
  </channel>
</rss>
