<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;AkIDRXY7eSp7ImA9WhRVGUg.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388</id><updated>2012-01-19T02:36:14.801-05:00</updated><category term="linux" /><category term="ruby" /><category term="Personal" /><category term="jokes" /><category term="best-practice" /><category term="internet news" /><category term="project euler" /><category term="testing" /><category term="Windows" /><category term="how-to" /><category term="vbscript" /><category term="wshbuild" /><category term="blog" /><category term="yabe" /><category term="msaccess" /><category term="announcements" /><title>Random Code Patterns</title><subtitle type="html">Ideas, Thoughts, and Code</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://hsteinhilber.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>50</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/RandomCodePatterns" /><feedburner:info uri="randomcodepatterns" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;AkQBQH8-cCp7ImA9WhRVGUg.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-2298363423746827934</id><published>2012-01-19T02:00:00.000-05:00</published><updated>2012-01-19T02:32:31.158-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-19T02:32:31.158-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="internet news" /><title>SOPA Blackout</title><content type="html">Well, the blackout on January 18 worked to get the SOPA and PIPA bills noticed on national media. However, coverage is about what you would expect considering most TV networks are owned by the very media conglomerates that want to shut down the Internet as we know it. &lt;br /&gt;
&lt;br /&gt;
Just check out this &lt;a href="http://mediamatters.org/blog/201201180006" target="_blank" title="Media Matters"&gt;report from Media Matters on FOX News' coverage&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-2298363423746827934?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/YGJNjR_BLdnIhd94YK3tbquTHG4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/YGJNjR_BLdnIhd94YK3tbquTHG4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/YGJNjR_BLdnIhd94YK3tbquTHG4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/YGJNjR_BLdnIhd94YK3tbquTHG4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/mG0Nd_roonQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/2298363423746827934/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=2298363423746827934" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/2298363423746827934?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/2298363423746827934?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/mG0Nd_roonQ/well-blackout-on-january-18-worked-to.html" title="SOPA Blackout" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2012/01/well-blackout-on-january-18-worked-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ak8GSX4_fSp7ImA9WhRVGEw.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-237608357405689300</id><published>2012-01-17T11:42:00.001-05:00</published><updated>2012-01-17T11:47:08.045-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-17T11:47:08.045-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="internet news" /><title>More SOPA and PIPA news</title><content type="html">&lt;p&gt;A couple more links on the SOPA front. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="We the People" href="https://wwws.whitehouse.gov/petition-tool/response/combating-online-piracy-while-protecting-open-and-innovative-internet" target="_blank"&gt;The White House has declared it’s opposition to SOPA.&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a title="YouTube.com" href="http://www.youtube.com/watch?v=yDX8Lyl16Qs&amp;amp;feature=youtu.be" target="_blank"&gt;A great explanation of how PIPA will break the Internet.&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a title="The Hill" href="http://thehill.com/blogs/hillicon-valley/technology/204167-sopa-shelved-until-consensus-is-found" target="_blank"&gt;Congress shelves SOPA indefinitely.&lt;/a&gt;&amp;#160; (Don’t be confused, though, PIPA is still alive in the Senate.)&lt;/li&gt;    &lt;li&gt;&lt;a title="Wikipedia" href="http://en.wikipedia.org/wiki/Wikipedia:SOPA_initiative" target="_blank"&gt;Wikipedia is doing a great job covering the status of these two bills.&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Haven’t heard of these bills before: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="Wikipedia" href="http://en.wikipedia.org/wiki/Stop_Online_Piracy_Act" target="_blank"&gt;SOPA&lt;/a&gt; – Stop Online Piracy Act      &lt;br /&gt;Proposes to stop online piracy by barring sites that post, link to, or otherwise make available copyright content. However, this bill would also allow your site to be shut down if someone simply posts a copyrighted image in the comments section of the site. The full text of the bill can be found &lt;a title="GovTrack.us - H.R.3261" href="http://www.govtrack.us/congress/bill.xpd?bill=h112-3261" target="_blank"&gt;here&lt;/a&gt;.&lt;/li&gt;    &lt;li&gt;&lt;a title="Wikipedia" href="http://en.wikipedia.org/wiki/PROTECT_IP_Act" target="_blank"&gt;PIPA&lt;/a&gt; – PROTECT IP Act (Preventing Real Online Threats to Economic Creativity and Theft of Intellectual Property)      &lt;br /&gt;This bill would give the government the ability to completely block sites that corporations don’t like. Again, this is ostensibly to allow them to block piracy, except that it doesn’t since downloaders will still be able to access these sites via their IP addresses. The full text of the bill can be found &lt;a title="GovTrack.us - S.968" href="http://www.govtrack.us/congress/bill.xpd?bill=s112-968" target="_blank"&gt;here&lt;/a&gt;.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We need everybody’s support to make sure these bills don’t happen. Write your Senator and your Congressman. Or you can fill out one of the many joint letters being compiled online, such as the one at &lt;a title="Fight for the Future - PIPA" href="http://fightforthefuture.org/pipa" target="_blank"&gt;Fight for the Future&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-237608357405689300?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/RIqxAO4eh38ACOZNZaIrhiN6K2I/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RIqxAO4eh38ACOZNZaIrhiN6K2I/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/RIqxAO4eh38ACOZNZaIrhiN6K2I/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RIqxAO4eh38ACOZNZaIrhiN6K2I/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/PAvkbPm97CQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/237608357405689300/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=237608357405689300" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/237608357405689300?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/237608357405689300?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/PAvkbPm97CQ/more-sopa-and-pipa-news.html" title="More SOPA and PIPA news" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2012/01/more-sopa-and-pipa-news.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQCQ34yeip7ImA9WhRVGUg.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-6675151664751227149</id><published>2012-01-07T05:00:00.000-05:00</published><updated>2012-01-19T02:32:42.092-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-19T02:32:42.092-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="internet news" /><title>More SOPA in the news, or not...</title><content type="html">&lt;p&gt;Is it just me, or is &lt;a href="http://en.wikipedia.org/wiki/SOPA"&gt;SOPA&lt;/a&gt; not getting very much coverage despite its horrendous implications. Oh, guess it isn't just me...&lt;/p&gt;&lt;br /&gt;
&lt;a href="http://mediamatters.org/blog/201201050008"&gt;REPORT: News Networks Ignore Controversial SOPA Legislation&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Controversial legislation that the co-founder of Google has warned "would put us on a par with the most oppressive nations in the world" has received virtually no coverage from major American television news outlets during their evening newscasts and opinion programming. The parent companies of most of these networks, as well as two of the networks themselves, are listed as official "supporters" of this legislation on the U.S. House of Representatives' website. &lt;/blockquote&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-6675151664751227149?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/IumQ0AOvwIBguoxtZKohtyd2Dec/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IumQ0AOvwIBguoxtZKohtyd2Dec/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/IumQ0AOvwIBguoxtZKohtyd2Dec/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IumQ0AOvwIBguoxtZKohtyd2Dec/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/CK065ir3g-k" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/6675151664751227149/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=6675151664751227149" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6675151664751227149?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6675151664751227149?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/CK065ir3g-k/more-sopa-in-news-or-not.html" title="More SOPA in the news, or not..." /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2012/01/more-sopa-in-news-or-not.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0UFQX4-eCp7ImA9WhRXE0o.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-8044837876028450746</id><published>2011-12-20T05:00:00.000-05:00</published><updated>2011-12-20T05:00:10.050-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-20T05:00:10.050-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="internet news" /><title>Congress vs. The Internet</title><content type="html">It is just sad that in this day and age, our elected officials have no idea how the internet works nor do they wish to speak to people who do. &lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://motherboard.vice.com/2011/12/16/dear-congress-it-s-no-longer-ok-to-not-know-how-the-internet-works"&gt;Dear Congress, It's No Longer OK To Not Know How The Internet Works&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-8044837876028450746?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/1_ge-miVkGOBmteM02tWc0EfC_Y/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1_ge-miVkGOBmteM02tWc0EfC_Y/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/1_ge-miVkGOBmteM02tWc0EfC_Y/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1_ge-miVkGOBmteM02tWc0EfC_Y/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/154krF1quyI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/8044837876028450746/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=8044837876028450746" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/8044837876028450746?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/8044837876028450746?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/154krF1quyI/congress-vs-internet.html" title="Congress vs. The Internet" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/12/congress-vs-internet.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DE4FSX4zfCp7ImA9WhZWE0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-1896804122370751977</id><published>2011-05-13T12:00:00.001-04:00</published><updated>2011-05-13T16:01:58.084-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-13T16:01:58.084-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 21</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 21" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=21" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Let d(&lt;i&gt;n&lt;/i&gt;) be defined as the sum of proper divisors of &lt;i&gt;n&lt;/i&gt; (numbers less than &lt;i&gt;n&lt;/i&gt; which divide evenly into &lt;i&gt;n&lt;/i&gt;).       &lt;br /&gt;If d(&lt;i&gt;a&lt;/i&gt;) = &lt;i&gt;b&lt;/i&gt; and d(&lt;i&gt;b&lt;/i&gt;) = &lt;i&gt;a&lt;/i&gt;, where &lt;i&gt;a&lt;/i&gt; ≠ &lt;i&gt;b&lt;/i&gt;, then &lt;i&gt;a&lt;/i&gt; and &lt;i&gt;b&lt;/i&gt; are an amicable pair and each of &lt;i&gt;a&lt;/i&gt; and &lt;i&gt;b&lt;/i&gt; are called amicable numbers.&lt;/p&gt;    &lt;p&gt;For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.&lt;/p&gt;    &lt;p&gt;Evaluate the sum of all the amicable numbers under 10000.&lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;This problem is best broken down into steps. The first thing we need is an easy way to get the proper divisors of a number.&lt;/p&gt;  &lt;pre class="brush: ruby"&gt;def divisors(n)
  return [] if n &amp;lt;=1
  limit = Math.sqrt(n).floor
  (2..limit).select { |d| n % d == 0 }.inject([1]) do |result,d|
    result &amp;lt;&amp;lt; n &amp;lt;&amp;lt; n / d
  end.uniq
end&lt;/pre&gt;

&lt;p&gt;This method is fairly complex for a change, so we’ll break it down. First, we exit fast with an empty set if the number is less than or equal to one. Notice that since a proper divisor must be less than the number, one has no proper divisors.&lt;/p&gt;

&lt;p&gt;Next, we figure out what the square root of the number &lt;code&gt;n&lt;/code&gt; is. This gives us a good cutoff, since if we add the divisors in pairs, by the time we reach &lt;code&gt;limit&lt;/code&gt; we will already have added all the values greater than &lt;code&gt;limit&lt;/code&gt;. With this number in hand, we can iterate through the entire range from 2 to &lt;code&gt;limit&lt;/code&gt; and &lt;code&gt;select&lt;/code&gt; only the numbers that divide evenly into &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will create an array by beginning with &lt;code&gt;[1]&lt;/code&gt; (remember, &lt;code&gt;n&lt;/code&gt; is not a proper divisor of itself, so 1 has no pair and must be added manually). We then proceed to add each divisor to &lt;code&gt;result&lt;/code&gt; in pairs, the current divisor and it’s matching dividend above &lt;code&gt;limit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is one more thing we have to take into account. If &lt;code&gt;n&lt;/code&gt; is a perfect square, then it will have a divisor equal to &lt;code&gt;limit&lt;/code&gt;. This divisor will actually be added twice (once as a divisor and once as a dividend). There is a number of ways we could correct this, but I found it easiest to just ask Ruby to return the &lt;code&gt;uniq&lt;/code&gt;ue results.&lt;/p&gt;

&lt;p&gt;The next method we will need is to test if a number is amicable or not. For this, I've decided to implement a method that will return the other amicable number in a pair, or &lt;code&gt;nil&lt;/code&gt; if no such number exists.&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;# extracted summing proper divisors
def dsum(n)
  divisors(n).reduce(0, :+)
end

def amicable_pair(n)
  pair = dsum(n)
  pair if dsum(pair) == n and pair != n
end&lt;/pre&gt;

&lt;p&gt;The last thing to do is find all of the amicable numbers from 1 to 10,000. We can use the fact that &lt;code&gt;select&lt;/code&gt; will treat &lt;code&gt;nil&lt;/code&gt; as &lt;code&gt;false&lt;/code&gt; when querying an &lt;a href="http://ruby-doc.org/core/classes/Enumerable.html"&gt;&lt;code&gt;Enumerable&lt;/code&gt;&lt;/a&gt; and just call &lt;code&gt;amicable_pair&lt;/code&gt; on each value. Then just sum up the results and we are done.&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;(1..10_000).select { |n| amicable_pair(n) }.reduce(0, :+)&lt;/pre&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_021.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_021_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, what is the total of all the name scores in a given file of first names?&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-1896804122370751977?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/c_ehLdtLAWCMuN87ykGXHNIRPeY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/c_ehLdtLAWCMuN87ykGXHNIRPeY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/c_ehLdtLAWCMuN87ykGXHNIRPeY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/c_ehLdtLAWCMuN87ykGXHNIRPeY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/uIrGc2sluYg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/1896804122370751977/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=1896804122370751977" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/1896804122370751977?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/1896804122370751977?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/uIrGc2sluYg/project-euler-problem-21.html" title="Project Euler-Problem 21" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/05/project-euler-problem-21.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0EERHo8fCp7ImA9WhZWEU8.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-5233373466814092514</id><published>2011-05-11T12:00:00.000-04:00</published><updated>2011-05-11T12:00:05.474-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-11T12:00:05.474-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 20</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 20" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=20" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;n&lt;/i&gt;! means &lt;i&gt;n&lt;/i&gt; x (&lt;i&gt;n&lt;/i&gt; - 1) x ... x 3 x 2 x 1&lt;/p&gt;    &lt;p&gt;For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800,      &lt;br /&gt;and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.&lt;/p&gt;    &lt;p&gt;Find the sum of the digits in the number 100!&lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;The first thing that we need to solve this problem is a quick method for computing factorials. Since &lt;a title="Ruby - A programmer&amp;#39;s best friend" href="http://ruby-lang.org" target="_blank"&gt;Ruby&lt;/a&gt; supports arbitrary precision integers, computing 100! will not be too big of a task.&lt;/p&gt;  &lt;pre class="brush: ruby"&gt;class ::Integer
  def fact
    (1..self).reduce(1, :*)
  end
end&lt;/pre&gt;

&lt;p&gt;We will also need our &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/integer.rb#L15"&gt;&lt;code&gt;digits&lt;/code&gt;&lt;/a&gt; method from back in &lt;a title="Project Euler-Problem 8" href="http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-8.html"&gt;Problem 8&lt;/a&gt;. With these two things together, we can put together a fairly trivial solution.&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;100.fact.digits.reduce(0, :+)&lt;/pre&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_020.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_020_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, evaluating the sum of all amicable pairs under 10,000.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-5233373466814092514?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/61lrnQWPdiAVbA_2JgoZ5HTYB98/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/61lrnQWPdiAVbA_2JgoZ5HTYB98/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/61lrnQWPdiAVbA_2JgoZ5HTYB98/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/61lrnQWPdiAVbA_2JgoZ5HTYB98/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/wBAgzhEKdf4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/5233373466814092514/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=5233373466814092514" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/5233373466814092514?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/5233373466814092514?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/wBAgzhEKdf4/project-euler-problem-20.html" title="Project Euler-Problem 20" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/05/project-euler-problem-20.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUQMQXc5eip7ImA9WhZXGU4.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-5243055832179638416</id><published>2011-05-08T22:49:00.001-04:00</published><updated>2011-05-09T06:36:20.922-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-09T06:36:20.922-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 19</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 19" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=19" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;You are given the following information, but you may prefer to do some research for yourself.&lt;/p&gt;    &lt;ul&gt;     &lt;li&gt;1 Jan 1900 was a Monday. &lt;/li&gt;      &lt;li&gt;Thirty days has September,        &lt;br /&gt;April, June and November.         &lt;br /&gt;All the rest have thirty-one,         &lt;br /&gt;Saving February alone,         &lt;br /&gt;Which has twenty-eight, rain or shine.         &lt;br /&gt;And on leap years, twenty-nine. &lt;/li&gt;      &lt;li&gt;A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. &lt;/li&gt;   &lt;/ul&gt;    &lt;p&gt;How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?&lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;Most modern languages (&lt;a title="Ruby - A programmer&amp;#39;s best friend" href="http://ruby-lang.org" target="_blank"&gt;Ruby&lt;/a&gt; included) have decent basic date support baked in. In our case, Ruby makes it easy to check what day of the week a given date falls on through the use of the &lt;code&gt;sunday?&lt;/code&gt; through &lt;code&gt;saturday?&lt;/code&gt; methods. So all we really have to do here is iterate through the 1,200 months (12 months/year * 100 years) and check the first of each month:&lt;/p&gt;  &lt;pre class="brush: ruby"&gt;(1..12).to_a.product((1901..2000).to_a).count { |month,year| Time.new(year,month,1).sunday? }&lt;/pre&gt;

&lt;p&gt;Another simple solution provided by the power of Ruby. One thing I would have liked to see, though, was the &lt;code&gt;product&lt;/code&gt; method on the &lt;code&gt;Range&lt;/code&gt; class. Or even better, having the &lt;code&gt;*&lt;/code&gt; operator as an alias to the &lt;code&gt;product&lt;/code&gt; method. This can be achieved easily enough, though:&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;class ::Range
  def product(other)
    self.to_a.product(other.to_a)
  end
  alias :* :product
end

((1..12) * (1901..2000)).count { |month,year| Time.new(year,month,1).sunday?}&lt;/pre&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_019.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_019_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, finding the sum of digits in 100!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-5243055832179638416?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/vZxRxguTZV9XpixNxdmJbSVroUM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vZxRxguTZV9XpixNxdmJbSVroUM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/vZxRxguTZV9XpixNxdmJbSVroUM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vZxRxguTZV9XpixNxdmJbSVroUM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/JvyqFTXoxjk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/5243055832179638416/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=5243055832179638416" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/5243055832179638416?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/5243055832179638416?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/JvyqFTXoxjk/project-euler-problem-19.html" title="Project Euler-Problem 19" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/05/project-euler-problem-19.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0AHRH87eyp7ImA9WhZSFUU.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-1362399885913828455</id><published>2011-03-31T11:35:00.001-04:00</published><updated>2011-03-31T11:35:35.103-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-31T11:35:35.103-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problems 18 &amp; 67</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 18" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=18" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;p&gt;By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.&lt;/p&gt;  &lt;p style="text-align: center; font-family: consolas,courier new,monospace"&gt;&lt;strong&gt;3&lt;/strong&gt;     &lt;br /&gt;&lt;strong&gt;7&lt;/strong&gt; 4     &lt;br /&gt;2 &lt;strong&gt;4&lt;/strong&gt; 6     &lt;br /&gt;8 5 &lt;strong&gt;9&lt;/strong&gt; 3&lt;/p&gt;  &lt;p&gt;That is, 3 + 7 + 4 + 9 = 23.&lt;/p&gt;  &lt;p&gt;Find the maximum total from top to bottom of the triangle below:&lt;/p&gt;  &lt;p style="text-align: center; font-family: consolas,courier new,monospace"&gt;75    &lt;br /&gt;95 64     &lt;br /&gt;17 47 82     &lt;br /&gt;18 35 87 10     &lt;br /&gt;20 04 82 47 65     &lt;br /&gt;19 01 23 75 03 34     &lt;br /&gt;88 02 77 73 07 63 67     &lt;br /&gt;99 65 04 28 06 16 70 92     &lt;br /&gt;41 41 26 56 83 40 80 70 33     &lt;br /&gt;41 48 72 33 47 32 37 16 94 29     &lt;br /&gt;53 71 44 65 25 43 91 52 97 51 14     &lt;br /&gt;70 11 33 28 77 73 17 78 39 68 17 57     &lt;br /&gt;91 71 52 38 17 14 91 43 58 50 27 29 48     &lt;br /&gt;63 66 04 68 89 53 67 30 73 16 69 87 40 31     &lt;br /&gt;04 62 98 27 23 09 70 98 73 93 38 53 60 04 23&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; As there are only 16384 routes, it is possible to solve this problem by trying every route. However, &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=67"&gt;Problem 67&lt;/a&gt;, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)&lt;/p&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;As the note in the problem statement mentions, this problem is pretty easy to brute force by simply adding together every possible path. We will assume the triangle is stored as an array of arrays, like so:&lt;/p&gt;  &lt;pre class="brush: ruby"&gt;class Problem018 
  TRIANGLE = [ [75],
               [95,64],
               [17,47,82],
               [18,35,87,10],

               # additional rows

               [4,62,98,27,23,9,70,98,73,93,38,53,60,4,23]
             ]
end&lt;/pre&gt;

&lt;p&gt;Now we can build a method that recursively evaluates the “top” of the triangle, plus the two smaller sub-triangles underneath it:&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;module TriangleSolver
  def add_triangle(triangle, row = 0, col = 0)
    return 0 if row &amp;gt;= triangle.length
    return triangle[row][col] if row == triangle.length - 1
    
    left = add_triangle(triangle, row + 1, col)  # compute the maximal sum of the left sub-triangle
    right = add_triangle(triangle, row + 1, col + 1) # compute the maximal sum of the right sub-triangle

    triangle[row][col] + (left + right ? left : right)
  end
end&lt;/pre&gt;

&lt;p&gt;Notice that here I’ve used a module because we will be making use of this solution for problem 67 as well. This works well for this problem since there are relatively few paths to compute. But as the triangle gets larger this will become too slow to be able to solve. However, we can greatly improve the number of paths that have to actually be computed.&lt;/p&gt;

&lt;p&gt;The first step towards this goal is to realize that each sub-triangle (except the ones on the very edge) get reused. How? Take a look at the original (smaller) example triangle. The triangle 4-5-9 is both the right sub-triangle of the 7 in the second row and it is the left sub-triangle of the 4 in the second row. In our current implementation, we would compute the maximal sum of this sub-triangle both times, but we really only need to compute it once and then reuse that result. So, with some judicious use of &lt;a title="Wikipedia - Memoization" href="http://en.wikipedia.org/wiki/Memoization" target="_blank"&gt;memoization&lt;/a&gt;, we can actually reduce the number of triangle computations by a large degree!&lt;/p&gt;

&lt;pre class="brush: ruby; highlight: [2,3,4,9,14,15]"&gt;module TriangleSolver
  def initialize
    @cache = {}
  end

  def add_triangle(triangle, row = 0, col = 0)
    return 0 if row &amp;gt;= triangle.length
    return triangle[row][col] if row == triangle.length - 1
    return @cache[row][col] if @cache.include? row and @cache[row].include? col
    
    left = add_triangle(triangle, row + 1, col)  # compute the maximal sum of the left sub-triangle
    right = add_triangle(triangle, row + 1, col + 1) # compute the maximal sum of the right sub-triangle

    @cache[row] ||= {}
    @cache[row][col] = triangle[row][col] + (left + right ? left : right)
  end
end&lt;/pre&gt;

&lt;p&gt;With this change, our solution is much more efficient for larger triangles. How can we test that fact? Well, &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=67"&gt;problem 67&lt;/a&gt; is the same exact problem, just a much larger triangle. Here is the description of from Project Euler:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.&lt;/p&gt;

  &lt;p style="text-align: center; font-family: consolas,courier new,monospace"&gt;&lt;strong&gt;3&lt;/strong&gt; 

    &lt;br /&gt;&lt;strong&gt;7&lt;/strong&gt; 4 

    &lt;br /&gt;2 &lt;strong&gt;4&lt;/strong&gt; 6 

    &lt;br /&gt;8 5 &lt;strong&gt;9&lt;/strong&gt; 3&lt;/p&gt;

  &lt;p&gt;That is, 3 + 7 + 4 + 9 = 23.&lt;/p&gt;

  &lt;p&gt;Find the maximum total from top to bottom in &lt;a href="http://projecteuler.net/project/triangle.txt"&gt;triangle.txt&lt;/a&gt; (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.&lt;/p&gt;

  &lt;p&gt;&lt;b&gt;NOTE:&lt;/b&gt; This is a much more difficult version of &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=18"&gt;Problem 18&lt;/a&gt;. It is not possible to try every route to solve this problem, as there are 2&lt;sup&gt;99&lt;/sup&gt; altogether! If you could check one trillion (10&lt;sup&gt;12&lt;/sup&gt;) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it. ;o)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So to test our solution against this much larger triangle, all we need is a method to load the triangle from disk.&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;class Problem067
  include TriangleSolver

  def load_file
    File.open('triangle.txt') do |file|
      file.readlines.map { |line| line.split(' ').map { |num| num.to_i } }
    end
  end
end&lt;/pre&gt;

&lt;p&gt;And now when we run problem 67, we can see that indeed, our memoization technique is a success. We can find the maximal sum of a triangle with 100 rows in less than one second. I dare say that is a lot better than twenty billion years!&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_018.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_018_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; for problem 18 can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. You can also see the &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/triangle_solver.rb" target="_blank"&gt;source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/specs/triangle_solver_spec.rb" target="_blank"&gt;specs&lt;/a&gt; for the &lt;code&gt;TriangleSolver&lt;/code&gt; as well as &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_067.rb" target="_blank"&gt;problem 67&lt;/a&gt;. Next time, how many Sundays fell on the first of the month during the twentieth century?&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-1362399885913828455?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XXmCkpfwmlCXz81pXdZ_mP0BKBs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XXmCkpfwmlCXz81pXdZ_mP0BKBs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XXmCkpfwmlCXz81pXdZ_mP0BKBs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XXmCkpfwmlCXz81pXdZ_mP0BKBs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/vSp4W24yO9Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/1362399885913828455/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=1362399885913828455" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/1362399885913828455?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/1362399885913828455?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/vSp4W24yO9Y/project-euler-problems-18-67.html" title="Project Euler-Problems 18 &amp;amp; 67" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problems-18-67.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck8CRH44fyp7ImA9WhZSFE0.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-7230076144504549040</id><published>2011-03-28T23:35:00.001-04:00</published><updated>2011-03-29T08:14:25.037-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-29T08:14:25.037-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 17</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 17" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=17" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.&lt;/p&gt;    &lt;p&gt;If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? &lt;/p&gt;    &lt;p&gt;&lt;b&gt;NOTE:&lt;/b&gt; Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of &amp;quot;and&amp;quot; when writing out numbers is in compliance with British usage.&lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;&lt;font color="#0000ff"&gt;&lt;font size="1"&gt;&lt;strong&gt;Disclaimer: &lt;/strong&gt;This problem is one of several that went through several iterations, and I’m still not completely happy with it.However, it works, and produces a result in less than one second, so good enough I guess.&lt;/font&gt;&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;This problem is fairly straight-forward. Simply iterate through all the numbers from one to one thousand and count up the letters. The only real trick is how you figure out how many letters are in each word. So for this problem, lets do something a little different and look at what the end result will be:&lt;/p&gt;  &lt;pre class="brush: ruby"&gt;(1..1000).reduce(0) { |sum, n| sum + letters_for(n) }&lt;/pre&gt;

&lt;p&gt;Pretty simple stuff. All we need to do now is define the &lt;code&gt;letters_for&lt;/code&gt; method and we're done. &lt;/p&gt;

&lt;p&gt;There are only a few actual words used in generating the text representations of the number from one to one thousand. Namely, each of the numbers from one to twenty has its own name. From there each group of ten numbers is just a new word by itself or hyphenated with one of the numbers from one to nine. This keeps going until we get to one hundred, which introduces another new word. And finally, one thousand introduces one more word.&lt;/p&gt;

&lt;p&gt;Now that we know we only have a few actual words we need to keep track of, I just stuffed all of those lengths into a hash for easy lookup.&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;class Problem017
  LETTERS = {
    1 =&amp;gt; 3, 2 =&amp;gt; 3, 3 =&amp;gt; 5, 4 =&amp;gt; 4, 5 =&amp;gt; 4, 6 =&amp;gt; 3, 7 =&amp;gt; 5, 8 =&amp;gt; 5, 9 =&amp;gt; 4, 10 =&amp;gt; 3,
    11 =&amp;gt; 6, 12 =&amp;gt; 6, 13 =&amp;gt; 8, 14 =&amp;gt; 8, 15 =&amp;gt; 7, 16 =&amp;gt; 7, 17 =&amp;gt; 9, 18 =&amp;gt; 8, 19 =&amp;gt; 8,
    20 =&amp;gt; 6, 30 =&amp;gt; 6, 40 =&amp;gt; 5, 50 =&amp;gt; 5, 60 =&amp;gt; 5, 70 =&amp;gt; 7, 80 =&amp;gt; 6, 90 =&amp;gt; 6, 
    100 =&amp;gt; 7, 1000 =&amp;gt; 8, 0 =&amp;gt; 0
  }
end&lt;/pre&gt;

&lt;p&gt;The last piece is to implement our &lt;code&gt;letters_for&lt;/code&gt; method by just adding up the pieces of the text by looking them up in the hash. This is the part I am not very happy with and may change in the future. It just seems very procedural and not very &lt;a title="Ruby - A programmer&amp;#39;s best friend" href="http://ruby-lang.org" target="_blank"&gt;Ruby&lt;/a&gt;-esque. &lt;/p&gt;

&lt;pre class="brush: ruby"&gt;class Problem017
  def letters_for(n)
    sum = 0
    sum += LETTERS[n / 1000] + LETTERS[1000] and n %= 1000 if n &amp;gt;= 1000
    sum += LETTERS[n / 100] + LETTERS[100] and n %= 100 if n &amp;gt;= 100
    sum += AND if sum &amp;gt; 0 and n &amp;gt; 0
    sum += LETTERS[n - n%10] + LETTERS[n%10] and n = 0 if n &amp;gt;= 20
    sum += LETTERS[n]
  end
end&lt;/pre&gt;

&lt;p&gt;As you can see, we just add however many “thousands” there are, plus the word “thousand”. Then we do the same for “hundred”. Then there is a small piece that adds the “and” between the hundreds and the tens/ones if both are present.&lt;sup&gt;1&lt;/sup&gt;&amp;#160; Finally, we add in a possibly hyphenated word for the numbers twenty to ninety-nine, or if the number is less then twenty, we add the proper word. &lt;/p&gt;

&lt;p&gt;Notice that I’ve included zero as having no letters here. This is so that the addition for the hyphenated words works out correctly. For instance:&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;# The last two lines for 30
sum += 30 + 0 and n = 0 
sum += 0

# The last two lines for 31
sum += 30 + 1 and n = 0
sum += 0&lt;/pre&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_017.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_017_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, finding the maximum sum travelling from the top of the triangle to the base.&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt;This part is a little weird since in the United States, it is incorrect to include the and, but in certain European countries it is required. As the problem states it is looking for the British version, we need to add this in.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-7230076144504549040?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zyGQznwAyUzbKT_94gvV_wVqTag/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zyGQznwAyUzbKT_94gvV_wVqTag/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zyGQznwAyUzbKT_94gvV_wVqTag/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zyGQznwAyUzbKT_94gvV_wVqTag/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/SrWj4P2-8PY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/7230076144504549040/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=7230076144504549040" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/7230076144504549040?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/7230076144504549040?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/SrWj4P2-8PY/project-euler-problem-17.html" title="Project Euler-Problem 17" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problem-17.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ak8AQH8_eip7ImA9WhZTEEo.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-233732085101761946</id><published>2011-03-14T01:00:00.001-04:00</published><updated>2011-03-14T01:00:41.142-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-14T01:00:41.142-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 16</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 16" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=16" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;2&lt;sup&gt;15&lt;/sup&gt; = 32,768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.&lt;/p&gt;    &lt;p&gt;What is the sum of the digits of the number 2&lt;sup&gt;1000&lt;/sup&gt;? &lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;This is another one of those problems that &lt;a title="Ruby - A programmer&amp;#39;s best friend" href="http://ruby-lang.org" target="_blank"&gt;Ruby&lt;/a&gt; makes &lt;em&gt;so&lt;/em&gt; easy by having a built-in arbitrary precision integer. Also, remember that back in &lt;a title="Project Euler-Problem 8" href="http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-8.html"&gt;problem 8&lt;/a&gt; we created a method on &lt;code&gt;Integer&lt;/code&gt; that will return an array with the digits of an integer. Putting these two elements together, we come to the very simple solution:&lt;/p&gt;  &lt;pre class="brush: ruby"&gt;(2**1000).digits.reduce(:+)&lt;/pre&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_016.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_016_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, how many letters would be needed to write all the numbers in words from 1 to 1000?&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-233732085101761946?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/SxTJsIWYOYom9t1lI9LqpmzrJe4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SxTJsIWYOYom9t1lI9LqpmzrJe4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/SxTJsIWYOYom9t1lI9LqpmzrJe4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SxTJsIWYOYom9t1lI9LqpmzrJe4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/pi51zj5NsEk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/233732085101761946/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=233732085101761946" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/233732085101761946?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/233732085101761946?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/pi51zj5NsEk/project-euler-problem-16.html" title="Project Euler-Problem 16" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problem-16.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUANQHo-eip7ImA9WhZTEEo.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-3717665969000279608</id><published>2011-03-13T12:15:00.001-04:00</published><updated>2011-03-14T00:43:11.452-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-14T00:43:11.452-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 15</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 15" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=15" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Starting in the top left corner of a 2x2 grid, there are 6 routes (without backtracking) to the bottom right corner.&lt;/p&gt;    &lt;p style="text-align: center"&gt;&lt;img alt="" src="http://projecteuler.net/project/images/p_015.gif" /&gt;&lt;/p&gt;    &lt;p&gt;How many routes are there through a 20x20 grid?&lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;In order find the number of paths that can be taken, we must first observe that we are going to move right 20 times and down 20 times in any valid solution. So our initial answer might be that we simply have to find how many ways we can rearrange 20 rights and 20 downs. Well, the number of possible orderings of 40 elements is 40!. So we might write something like:&lt;/p&gt;  &lt;pre class="brush: ruby"&gt;class Problem015
  class ::Integer
    def fact
      (1..self).reduce(1, :*)
    end
  end
end

40.fact&lt;/pre&gt;

&lt;p&gt;There is a problem with this solution, however. What happens if we swap a move down with a move down? We get the exact same ordering of moves. Now, since there are 20 moves down, there are 20! possible ways you can swap a down move with another down move. Similarly there are 20! ways we can swap a right move with another right move. So we need to remove these possibilities from the total number of orderings. So we get:&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;40.fact / (20.fact * 20.fact)&lt;/pre&gt;

&lt;p&gt;Now we can generalize this a little bit for any grid &lt;var&gt;m&lt;/var&gt;x&lt;var&gt;n&lt;/var&gt;.&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;class Problem015
  def paths_for_grid(m,n)
    (m+n).fact / (m.fact * n.fact)
  end
end

Problem015.new.paths_for_grid(20,20)&lt;/pre&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_012.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_012_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, what is the sum of the digits of the number 2&lt;sup&gt;1000&lt;/sup&gt;?&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-3717665969000279608?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/7T4SxqrhWb68fQwncviHBTJ6DAc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7T4SxqrhWb68fQwncviHBTJ6DAc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/7T4SxqrhWb68fQwncviHBTJ6DAc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7T4SxqrhWb68fQwncviHBTJ6DAc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/u5CW4iQOG_g" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/3717665969000279608/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=3717665969000279608" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/3717665969000279608?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/3717665969000279608?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/u5CW4iQOG_g/project-euler-problem-15.html" title="Project Euler-Problem 15" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problem-15.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A08BSXc5eCp7ImA9Wx9aGEw.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-1990573440967646570</id><published>2011-03-10T22:40:00.001-05:00</published><updated>2011-03-11T00:04:18.920-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-11T00:04:18.920-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 14</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 14" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=14" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The following iterative sequence is defined for the set of positive integers:&lt;/p&gt;    &lt;blockquote&gt;&lt;var&gt;n&lt;/var&gt; → &lt;var&gt;n&lt;/var&gt;/2 (&lt;var&gt;n&lt;/var&gt; is even)       &lt;br /&gt;&lt;var&gt;n&lt;/var&gt; → 3&lt;var&gt;n&lt;/var&gt; + 1 (&lt;var&gt;n&lt;/var&gt; is odd)&lt;/blockquote&gt;    &lt;p&gt;Using the rule above and starting with 13, we generate the following sequence:&lt;/p&gt;    &lt;p style="text-align: center"&gt;13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1&lt;/p&gt;    &lt;p&gt;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.&lt;/p&gt;    &lt;p&gt;Which starting number, under one million, produces the longest chain?&lt;/p&gt;    &lt;p&gt;&lt;b&gt;NOTE:&lt;/b&gt; Once the chain starts the terms are allowed to go above one million.&lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;The relatively straight-forward solution to this problem is to simply create an &lt;code&gt;Enumerator&lt;/code&gt; that will generate the appropriate sequence from a given starting point. We can then generate all of the sequences that start with numbers up to one million and find the longest one.&lt;/p&gt;  &lt;pre class="brush: ruby"&gt;class Problem014
  def sequence(start)
    Enumerator.new do |yielder|
      n = start
      (yielder &amp;lt;&amp;lt; n) &amp;amp;&amp;amp; (n = n.even ? n / 2 : 3*n + 1) while n &amp;gt; 1
      yielder &amp;lt;&amp;lt; 1
    end
  end

  def chain_lengths
    Enumerator.new do |yielder|
      start
      yielder &amp;lt;&amp;lt; [start, sequence(start).count] &amp;amp;&amp;amp; start += 1 while start &amp;lt; 1_000_000
    end
  end
end

Problem014.new.chain_lengths.max_by { |start,length| length }[0]&lt;/pre&gt;

&lt;p&gt;There is one glaring problem with this solution though. We are computing sequences hundreds of times over. Take for example a sequence starting with the number 26:&lt;/p&gt;

&lt;p style="text-align: center"&gt;26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1&lt;/p&gt;

&lt;p&gt;Notice that this contains the entire sequence that starts with 13. Therefore, the length of the sequence is just the length of the sequence for 13 plus one. Similarly, the length of sequence 13 is just the length of sequence 40 plus one. And so on and so forth. So with a little &lt;a title="Wikipedia - Memoization" href="http://en.wikipedia.org/wiki/Memoization" target="_blank"&gt;memoization&lt;/a&gt;, we can actually reuse the sequence lengths we have already computed.&lt;/p&gt;

&lt;pre class="brush: ruby"&gt;class Problem014
  def initialize
    @known_lengths = { 1 =&amp;gt; 1 }
  end

  def sequence(n)
    n.even? ? n / 2 : 3*n + 1
  end

  def chain_length(start)
    return @known_lengths[start] if @known_lengths.include? start
    @known_lengths[start] = chain_length(sequence(start)) + 1
  end

  def chain_lengths
    Enumerator.new do |yielder|
      start = 2
      yielder &amp;lt;&amp;lt; [start, chain_length(start)] &amp;amp;&amp;amp; start += 1 while start &amp;lt; 1_000_000
    end
  end
end&lt;/pre&gt;

&lt;p&gt;Notice that now the &lt;code&gt;sequence&lt;/code&gt; method only returns the next value in the chain. That is all that is necessary for us to look up and see if we already know that length. The &lt;code&gt;chain_length&lt;/code&gt; method will then return a pre-computed length if we already know it, or the &lt;code&gt;chain_length&lt;/code&gt; of the next value in the chain plus one. In addition, we will also store this length in case it is needed again for another sequence. Does all of this complication actually help? Well on my system I was able to go from 83s for the original algorithm down to 12s for the memoized version. Well worth the effort I think.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_014.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_014_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner?&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-1990573440967646570?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/GWFaykecjISWFZ_7ZYGnD_Fi8b8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/GWFaykecjISWFZ_7ZYGnD_Fi8b8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/GWFaykecjISWFZ_7ZYGnD_Fi8b8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/GWFaykecjISWFZ_7ZYGnD_Fi8b8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/P2icNh2aRGA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/1990573440967646570/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=1990573440967646570" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/1990573440967646570?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/1990573440967646570?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/P2icNh2aRGA/project-euler-problem-14.html" title="Project Euler-Problem 14" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problem-14.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkQDRHk7eyp7ImA9Wx9aF0Q.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-7573552389830918124</id><published>2011-03-10T15:52:00.001-05:00</published><updated>2011-03-10T15:52:55.703-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-10T15:52:55.703-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 13</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 13" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=13" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.&lt;/p&gt;    &lt;pre style="text-align: center"&gt;37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
62176457141856560629502157223196586755079324193331
64906352462741904929101432445813822663347944758178
92575867718337217661963751590579239728245598838407
58203565325359399008402633568948830189458628227828
80181199384826282014278194139940567587151170094390
35398664372827112653829987240784473053190104293586
86515506006295864861532075273371959191420517255829
71693888707715466499115593487603532921714970056938
54370070576826684624621495650076471787294438377604
53282654108756828443191190634694037855217779295145
36123272525000296071075082563815656710885258350721
45876576172410976447339110607218265236877223636045
17423706905851860660448207621209813287860733969412
81142660418086830619328460811191061556940512689692
51934325451728388641918047049293215058642563049483
62467221648435076201727918039944693004732956340691
15732444386908125794514089057706229429197107928209
55037687525678773091862540744969844508330393682126
18336384825330154686196124348767681297534375946515
80386287592878490201521685554828717201219257766954
78182833757993103614740356856449095527097864797581
16726320100436897842553539920931837441497806860984
48403098129077791799088218795327364475675590848030
87086987551392711854517078544161852424320693150332
59959406895756536782107074926966537676326235447210
69793950679652694742597709739166693763042633987085
41052684708299085211399427365734116182760315001271
65378607361501080857009149939512557028198746004375
35829035317434717326932123578154982629742552737307
94953759765105305946966067683156574377167401875275
88902802571733229619176668713819931811048770190271
25267680276078003013678680992525463401061632866526
36270218540497705585629946580636237993140746255962
24074486908231174977792365466257246923322810917141
91430288197103288597806669760892938638285025333403
34413065578016127815921815005561868836468420090470
23053081172816430487623791969842487255036638784583
11487696932154902810424020138335124462181441773470
63783299490636259666498587618221225225512486764533
67720186971698544312419572409913959008952310058822
95548255300263520781532296796249481641953868218774
76085327132285723110424803456124867697064507995236
37774242535411291684276865538926205024910326572967
23701913275725675285653248258265463092207058596522
29798860272258331913126375147341994889534765745501
18495701454879288984856827726077713721403798879715
38298203783031473527721580348144513491373226651381
34829543829199918180278916522431027392251122869539
40957953066405232632538044100059654939159879593635
29746152185502371307642255121183693803580388584903
41698116222072977186158236678424689157993532961922
62467957194401269043877107275048102390895523597457
23189706772547915061505504953922979530901129967519
86188088225875314529584099251203829009407770775672
11306739708304724483816533873502340845647058077308
82959174767140363198008187129011875491310547126581
97623331044818386269515456334926366572897563400500
42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
32238195734329339946437501907836945765883352399886
75506164965184775180738168837861091527357929701337
62177842752192623401942399639168044983993173312731
32924185707147349566916674687634660915035914677504
99518671430235219628894890102423325116913619626622
73267460800591547471830798392868535206946944540724
76841822524674417161514036427982273348055556214818
97142617910342598647204516893989422179826088076852
87783646182799346313767754307809363333018982642090
10848802521674670883215120185883543223812876952786
71329612474782464538636993009049310363619763878039
62184073572399794223406235393808339651327408011116
66627891981488087797941876876144230030984490851411
60661826293682836764744779239180335110989069790714
85786944089552990653640447425576083659976645795096
66024396409905389607120198219976047599490197230297
64913982680032973156037120041377903785566085089252
16730939319872750275468906903707539413042652315011
94809377245048795150954100921645863754710598436791
78639167021187492431995700641917969777599028300699
15368713711936614952811305876380278410754449733078
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;a name='more'&gt;&lt;/a&gt;

&lt;h4&gt;Solution&lt;/h4&gt;

&lt;p&gt;This problem is actually exceptionally easy in Ruby because of the &lt;code&gt;Bignum&lt;/code&gt; built-in type. Because &lt;code&gt;Bignum&lt;/code&gt; allows arithmetic on arbitrary precision integers, we can simply add the numbers together and pull off the first ten digits:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:61626a0e-cd84-4f28-b758-1af2172120de" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: ruby;"&gt;class Problem013
  
  NUMBERS = [37107287533902102798797998220837590246510135740250,
             46376937677490009712648124896970078050417018260538,
             74324986199524741059474233309513058123726617309629,
             91942213363574161572522430563301811072406154908250,
             ...
             77158542502016545090413245809786882778948721859617,
             72107838435069186155435662884062257473692284509516,
             20849603980134001723930671666823555245252804609722,
             53503534226472524250874054075591789781264330331690]
end

Problem013::NUMBERS.reduce(:+).to_s.slice(0..9).to_i&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_013.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_013_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, find the longest sequence using a starting number under one million..&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-7573552389830918124?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/vQ04N8eb6FVylC5_9w0WfjTc-iU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vQ04N8eb6FVylC5_9w0WfjTc-iU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/vQ04N8eb6FVylC5_9w0WfjTc-iU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vQ04N8eb6FVylC5_9w0WfjTc-iU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/I6fYmYm1sfY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/7573552389830918124/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=7573552389830918124" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/7573552389830918124?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/7573552389830918124?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/I6fYmYm1sfY/project-euler-problem-13.html" title="Project Euler-Problem 13" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problem-13.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEUBR3gzcCp7ImA9Wx9aF0s.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-4910923449653392016</id><published>2011-03-09T18:08:00.001-05:00</published><updated>2011-03-10T09:10:56.688-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-10T09:10:56.688-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 12</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt;  &lt;p&gt;From &lt;a title="Problem 12" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=12" target="_blank"&gt;Project Euler&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The sequence of triangle numbers is generated by adding the natural numbers. So the 7&lt;sup&gt;th&lt;/sup&gt; triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:&lt;/p&gt;    &lt;div style="text-align: center"&gt;1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...&lt;/div&gt;    &lt;p&gt;Let us list the factors of the first seven triangle numbers:&lt;/p&gt;    &lt;blockquote&gt;&lt;b&gt;1&lt;/b&gt;: 1       &lt;br /&gt;&lt;b&gt;3&lt;/b&gt;: 1,3       &lt;br /&gt;&lt;b&gt;6&lt;/b&gt;: 1,2,3,6       &lt;br /&gt;&lt;b&gt;10&lt;/b&gt;: 1,2,5,10       &lt;br /&gt;&lt;b&gt;15&lt;/b&gt;: 1,3,5,15       &lt;br /&gt;&lt;b&gt;21&lt;/b&gt;: 1,3,7,21       &lt;br /&gt;&lt;b&gt;28&lt;/b&gt;: 1,2,4,7,14,28       &lt;br /&gt;&lt;/blockquote&gt;    &lt;p&gt;We can see that 28 is the first triangle number to have over five divisors.&lt;/p&gt;    &lt;p&gt;What is the value of the first triangle number to have over five hundred divisors?&lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;There are two basic parts of this problem that we need to be concerned with. The first is generating the sequence of triangle numbers, and the second is counting how many divisors a given number has.&lt;/p&gt;  &lt;p&gt;The first part is relatively trivial. We will just create an &lt;code&gt;Enumerable&lt;/code&gt; that returns the proper sequence of numbers:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:9049d4fc-af56-4722-82ad-e0e6f4a6602a" class="wlWriterSmartContent"&gt;   &lt;pre class="brush: ruby;"&gt;class Problem012
  include Enumerable

  def each
    counter = 1; result = 1
    while true
      yield result
      result += (counter += 1)
    end
  end
end&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The next part of the problem is to count the number of divisors for a given number. Note that we don't actually need to compute the divisors, just count how many there are. The first observation that we can make is that the number of divisors will always be even except for the case of a perfect square. Why? Because if the number &lt;var&gt;n&lt;/var&gt; is evenly divisible by a number &lt;var&gt;d&lt;/var&gt; (and hence it is a divisor), then it is also divisible by &lt;var&gt;n&lt;/var&gt;/&lt;var&gt;d&lt;/var&gt;. So in finding divisor &lt;var&gt;d&lt;/var&gt;, we have also found &lt;var&gt;n&lt;/var&gt;/&lt;var&gt;d&lt;/var&gt;. For perfect squares, however, this rule works until you reach the square root of &lt;var&gt;n&lt;/var&gt;. In that case &lt;var&gt;d&lt;/var&gt; = &lt;var&gt;n&lt;/var&gt;/&lt;var&gt;d&lt;/var&gt;, so there is only one divisor. In addition, we also know that every number greater than one will have at least two divisors - one and itself. Given all of this, we come up with:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:9049d4fc-af56-4722-82ad-e0e6f4a6602a" class="wlWriterSmartContent"&gt;
  &lt;pre class="brush: ruby;"&gt;class Integer
  def divisor_count
    return 1 if self == 1
    limit = Math.sqrt(self)
    (2..limit).inject(limit == limit.floor ? 1 : 2) do |count,d|
      count + (self % d == 0 ? 2 : 0)
    end
  end
end&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We are now in a position to find the first triangle number with greater than 500 divisors:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:9049d4fc-af56-4722-82ad-e0e6f4a6602a" class="wlWriterSmartContent"&gt;
  &lt;pre class="brush: ruby;"&gt;Problem012.new.find { |n| n.divisor_count &amp;gt; 500 }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_012.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_012_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, finding the first ten digits of the sum of one-hundred 50-digit numbers.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-4910923449653392016?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/1c90XslPK16vMZJ733nA0KBwYMo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1c90XslPK16vMZJ733nA0KBwYMo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/1c90XslPK16vMZJ733nA0KBwYMo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1c90XslPK16vMZJ733nA0KBwYMo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/rW2zTaA5XKs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/4910923449653392016/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=4910923449653392016" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/4910923449653392016?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/4910923449653392016?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/rW2zTaA5XKs/project-euler-problem-12.html" title="Project Euler-Problem 12" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problem-12.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEIGQ3g6fip7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-1967246479520591533</id><published>2011-03-08T08:36:00.002-05:00</published><updated>2011-03-09T18:15:22.616-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:15:22.616-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 11</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt; From &lt;a title="Problem 11" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=11" target="_blank"&gt;Project Euler&lt;/a&gt;:   &lt;blockquote&gt;   &lt;p&gt;In the 20x20 grid below, four numbers along a diagonal line have been marked in red. &lt;/p&gt;    &lt;p&gt;&lt;font face="Courier New"&gt;08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08        &lt;br /&gt;49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00         &lt;br /&gt;81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65         &lt;br /&gt;52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91         &lt;br /&gt;22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80         &lt;br /&gt;24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50         &lt;br /&gt;32 98 81 28 64 23 67 10 &lt;b&gt;26&lt;/b&gt; 38 40 67 59 54 70 66 18 38 64 70         &lt;br /&gt;67 26 20 68 02 62 12 20 95 &lt;b&gt;63&lt;/b&gt; 94 39 63 08 40 91 66 49 94 21         &lt;br /&gt;24 55 58 05 66 73 99 26 97 17 &lt;b&gt;78&lt;/b&gt; 78 96 83 14 88 34 89 63 72         &lt;br /&gt;21 36 23 09 75 00 76 44 20 45 35 &lt;b&gt;14&lt;/b&gt; 00 61 33 97 34 31 33 95         &lt;br /&gt;78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92         &lt;br /&gt;16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57         &lt;br /&gt;86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58         &lt;br /&gt;19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40         &lt;br /&gt;04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66         &lt;br /&gt;88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69         &lt;br /&gt;04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36         &lt;br /&gt;20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16         &lt;br /&gt;20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54         &lt;br /&gt;01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48&lt;/font&gt; &lt;/p&gt;    &lt;p&gt;The product of these numbers is 26 x 63 x 78 x 14 = 1,788,696. &lt;/p&gt;    &lt;p&gt;What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20x20 grid?&lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;This is another one of those problems that is just very easy to brute force. We simply create a two dimensional array that will represent our grid.&amp;#160; &lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d3fc6d01-0557-4914-bc16-1f9221a3fe7d" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: ruby;"&gt;class Problem011
  GRID = [[ 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8],
          [49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0],
          [81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65],
          [52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91],
          [22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80],
          [24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50],
          [32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70],
          [67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21],
          [24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72],
          [21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95],
          [78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92],
          [16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57],
          [86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58],
          [19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40],
          [ 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66],
          [88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69],
          [ 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36],
          [20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16],
          [20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54],
          [ 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48]]
end&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We then generate all of the vertical, horizontal, and diagonal lines containing four numbers from the grid. &lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:9049d4fc-af56-4722-82ad-e0e6f4a6602a" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: ruby;"&gt;class Problem011
  def get_lines
    result = []

    GRID.length.times do |i|
      (GRID.length - 3).times do |j|
        result &amp;lt;&amp;lt; [GRID[i][j + 0],
                   GRID[i][j + 1],
                   GRID[i][j + 2],
                   GRID[i][j + 3]]
        result &amp;lt;&amp;lt; [GRID[j + 0][i],
                   GRID[j + 1][i],
                   GRID[j + 2][i],
                   GRID[j + 3][i]]
      end
    end

    (GRID.length - 3).times do |i|
      (GRID.length - 3).times do |j|
        result &amp;lt;&amp;lt; [GRID[i + 0][j + 0],
                   GRID[i + 1][j + 1],
                   GRID[i + 2][j + 2],
                   GRID[i + 3][j + 3]]
        result &amp;lt;&amp;lt; [GRID[i + 0][GRID.length - j - 1],
                   GRID[i + 1][GRID.length - j - 2],
                   GRID[i + 2][GRID.length - j - 3],
                   GRID[i + 3][GRID.length - j - 4]]
      end
    end

    return result
  end
end&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally we find the products of each of those four number combinations and get the largest one. &lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:da32e5db-9501-4fce-9127-822a5f843160" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: ruby;"&gt;Problem011.new.get_lines.map { |ary| ary.reduce(1, :*) }.max&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_011.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_011_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, finding the first triangle number with over five hundred divisors.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-1967246479520591533?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Wm6DAjGwTL2LmmoGey-Jkro8CKU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Wm6DAjGwTL2LmmoGey-Jkro8CKU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Wm6DAjGwTL2LmmoGey-Jkro8CKU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Wm6DAjGwTL2LmmoGey-Jkro8CKU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/CDctn2jHvvM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/1967246479520591533/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=1967246479520591533" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/1967246479520591533?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/1967246479520591533?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/CDctn2jHvvM/project-euler-problem-11.html" title="Project Euler-Problem 11" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problem-11.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEIBQX4-cCp7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-3597818295043206135</id><published>2011-03-05T13:00:00.015-05:00</published><updated>2011-03-09T18:15:50.058-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:15:50.058-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 10</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt; From &lt;a title="Problem 10" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=10" target="_blank"&gt;Project Euler&lt;/a&gt;:   &lt;br /&gt;  &lt;blockquote&gt;The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.    &lt;br /&gt;Find the sum of all the primes below two million.&lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt; As you'll recall, back in &lt;a title="Project Euler-Problem 7" href="http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-7.html"&gt;problem 7&lt;/a&gt; we created a general purpose prime number generator. That is going to come in extremely handy for this problem. Why? Well, the solution is as easy as:   &lt;br /&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px"&gt;   &lt;pre class="brush: ruby;"&gt;PrimeGenerator.new.take_while { |p| p &amp;lt; 2_000_000 }.reduce(:+)&lt;/pre&gt;
And that is all there is to it. Now wasn't that simple?&lt;/div&gt;

&lt;p&gt;The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_010.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_010_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, finding the greatest product of four numbers on the same line in a 20x20 grid.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-3597818295043206135?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/vWZk6yRO6uv8vHeKt4zC7c4Xqg4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vWZk6yRO6uv8vHeKt4zC7c4Xqg4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/vWZk6yRO6uv8vHeKt4zC7c4Xqg4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vWZk6yRO6uv8vHeKt4zC7c4Xqg4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/VMQCSm_ToMM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/3597818295043206135/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=3597818295043206135" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/3597818295043206135?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/3597818295043206135?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/VMQCSm_ToMM/project-euler-problem-10.html" title="Project Euler-Problem 10" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problem-10.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEIMQH4-eCp7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-6089811807886489779</id><published>2011-03-05T00:01:00.105-05:00</published><updated>2011-03-09T18:16:21.050-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:16:21.050-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler-Problem 9</title><content type="html">&lt;h4&gt;Description&lt;/h4&gt; From &lt;a title="Problem 9" href="http://projecteuler.net/index.php?section=problems&amp;amp;id=9" target="_blank"&gt;Project Euler&lt;/a&gt;:  &lt;br /&gt;  &lt;blockquote&gt;A Pythagorean triplet is a set of three natural numbers, &lt;var&gt;a&lt;/var&gt; &amp;lt; &lt;var&gt;b&lt;/var&gt; &amp;lt; &lt;var&gt;c&lt;/var&gt;, for which,    &lt;br /&gt;    &lt;div style="text-align: center"&gt;&lt;var&gt;a&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; + &lt;var&gt;b&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; = &lt;var&gt;c&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/div&gt; For example, 3&lt;sup&gt;2&lt;/sup&gt; + 4&lt;sup&gt;2&lt;/sup&gt; = 9 + 16 = 25 = 5&lt;sup&gt;2&lt;/sup&gt;.    &lt;br /&gt;There exists exactly one Pythagorean triplet for which &lt;var&gt;a&lt;/var&gt; + &lt;var&gt;b&lt;/var&gt; + &lt;var&gt;c&lt;/var&gt; = 1000.    &lt;br /&gt;Find the product &lt;var&gt;abc&lt;/var&gt;.&lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h4&gt;Solution&lt;/h4&gt; Creating all of the Pythagorean triplets can be done in one of two ways. The first is to simply pick arbitrary values for &lt;var&gt;a&lt;/var&gt; and &lt;var&gt;b&lt;/var&gt; and then compute the value of &lt;var&gt;c&lt;/var&gt;. Using this tactic we can see that the following would hold true:  &lt;br /&gt;  &lt;blockquote&gt;if &lt;var&gt;a&lt;/var&gt; = 0 (which will maximize &lt;var&gt;b&lt;/var&gt; in this equation, then    &lt;br /&gt;&lt;var&gt;a&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; + &lt;var&gt;b&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; = &lt;var&gt;c&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt;    &lt;br /&gt;&lt;var&gt;b&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; = &lt;var&gt;c&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt;    &lt;br /&gt;&lt;var&gt;b&lt;/var&gt; = &lt;var&gt;c&lt;/var&gt;    &lt;br /&gt;    &lt;br /&gt;and    &lt;br /&gt;    &lt;br /&gt;&lt;var&gt;a&lt;/var&gt; + &lt;var&gt;b&lt;/var&gt; + &lt;var&gt;c&lt;/var&gt; = 1000    &lt;br /&gt;2&lt;var&gt;b&lt;/var&gt; = 1000    &lt;br /&gt;&lt;var&gt;b&lt;/var&gt; = 500&lt;/blockquote&gt;  &lt;p&gt;Since &lt;var&gt;a&lt;/var&gt; must be greater than 0, we can deduce that &lt;var&gt;b&lt;/var&gt; must be less than 500. This means we need to search all of the pairs such that 1 &amp;lt;= &lt;var&gt;a&lt;/var&gt; &amp;lt;= &lt;var&gt;b&lt;/var&gt; &amp;lt; 500. Thankfully, there is a much easier solution. The astute reader may have noticed that a large number of the triplets (the vast majority in fact) will generate values of &lt;var&gt;c&lt;/var&gt; that are irrational. Since we are only concerned with values of &lt;var&gt;c&lt;/var&gt; that are natural numbers, there is an easy way to cut down the problem space.&lt;/p&gt;  &lt;p&gt;The answer here is &lt;a href="http://en.wikipedia.org/wiki/Euclid%27s_formula" target="_blank"&gt;Euclid's formula&lt;/a&gt; for Pythagorean triples. This formula states that given an arbitrary pair of positive integers &lt;var&gt;m&lt;/var&gt; and &lt;var&gt;n&lt;/var&gt; such that &lt;var&gt;m&lt;/var&gt; &amp;gt; &lt;var&gt;n&lt;/var&gt;, a Pythagorean triple exists given by the equations:&lt;/p&gt;  &lt;blockquote&gt;&lt;var&gt;a&lt;/var&gt; = &lt;var&gt;m&lt;var&gt;&lt;sup&gt;2&lt;/sup&gt; - &lt;var&gt;n&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt;        &lt;br /&gt;&lt;var&gt;b&lt;/var&gt; = 2&lt;var&gt;mn&lt;/var&gt;        &lt;br /&gt;&lt;var&gt;c&lt;/var&gt; = &lt;var&gt;m&lt;var&gt;&lt;sup&gt;2&lt;/sup&gt; + &lt;var&gt;n&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; &lt;/var&gt;&lt;/var&gt;&lt;/var&gt;&lt;/var&gt;&lt;/blockquote&gt; Given these equations, we can easily work out what the range of &lt;var&gt;m&lt;/var&gt; and &lt;var&gt;n&lt;/var&gt; are that we need to search to find our answer.  &lt;br /&gt;  &lt;blockquote&gt;&lt;var&gt;a&lt;/var&gt; + &lt;var&gt;b&lt;/var&gt; + &lt;var&gt;c&lt;/var&gt; = 1000    &lt;br /&gt;&lt;var&gt;m&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; - &lt;var&gt;n&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; + 2&lt;var&gt;mn&lt;/var&gt; + &lt;var&gt;m&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; + &lt;var&gt;n&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; = 1000    &lt;br /&gt;2&lt;var&gt;m&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; + 2&lt;var&gt;mn&lt;/var&gt; = 1000    &lt;br /&gt;if &lt;var&gt;n&lt;/var&gt; = 0 (which will maximize &lt;var&gt;m&lt;/var&gt; in this equation), then    &lt;br /&gt;2&lt;var&gt;m&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; = 1000     &lt;br /&gt;&lt;var&gt;m&lt;/var&gt;&lt;sup&gt;2&lt;/sup&gt; = 500    &lt;br /&gt;&lt;var&gt;m&lt;/var&gt; = 22.360... &lt;/blockquote&gt; Since &lt;var&gt;m&lt;/var&gt; and &lt;var&gt;n&lt;/var&gt; must be positive integers, we know that the range we need to search is 1 &amp;lt;= &lt;var&gt;n&lt;/var&gt; &amp;lt; &lt;var&gt;m&lt;/var&gt; &amp;lt; 23. This is a &lt;i&gt;much&lt;/i&gt; smaller search space than our first attempt.  &lt;br /&gt;With all of this theory out of the way, lets take a look at some code.  &lt;br /&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px"&gt;   &lt;pre class="brush: ruby;"&gt;class Problem009
  def find_triplet
    max = Math.sqrt(500).floor
    2.upto(max) do |m|
      1.upto(m-1) do |n|
        a = m**2 - n**2
        b = 2*m*n
        c = m**2 + n**2
        return [a, b, c] if a + b + c == 1000
      end
    end
  end
end
Problem009.new.find_triplet.reduce(1, :*)&lt;/pre&gt;

  
The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_009.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_009_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, computing the sum of all prime numbers less than 2 million.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-6089811807886489779?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/TmTHX2anv28NJQuryCTRXv3UA2Q/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TmTHX2anv28NJQuryCTRXv3UA2Q/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/TmTHX2anv28NJQuryCTRXv3UA2Q/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TmTHX2anv28NJQuryCTRXv3UA2Q/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/RmixZnZD2jg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/6089811807886489779/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=6089811807886489779" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6089811807886489779?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6089811807886489779?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/RmixZnZD2jg/project-euler-problem-9.html" title="Project Euler-Problem 9" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/03/project-euler-problem-9.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEEEQ3s8eCp7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-5385666146409925139</id><published>2011-02-23T12:18:00.003-05:00</published><updated>2011-03-09T18:16:42.570-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:16:42.570-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler–Problem 8</title><content type="html">&lt;h4&gt;
Description&lt;/h4&gt;
From &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=8" target="_blank" title="Problem 8"&gt;Project Euler&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
Find the greatest product of five consecutive digits in the 1000-digit number.&lt;br /&gt;
&lt;blockquote&gt;
&lt;span style="font-family: Courier New;"&gt;73167176531330624919225119674426574742355349194934        &lt;br /&gt;96983520312774506326239578318016984801869478851843         &lt;br /&gt;85861560789112949495459501737958331952853208805511         &lt;br /&gt;12540698747158523863050715693290963295227443043557         &lt;br /&gt;66896648950445244523161731856403098711121722383113         &lt;br /&gt;62229893423380308135336276614282806444486645238749         &lt;br /&gt;30358907296290491560440772390713810515859307960866         &lt;br /&gt;70172427121883998797908792274921901699720888093776         &lt;br /&gt;65727333001053367881220235421809751254540594752243         &lt;br /&gt;52584907711670556013604839586446706324415722155397         &lt;br /&gt;53697817977846174064955149290862569321978468622482         &lt;br /&gt;83972241375657056057490261407972968652414535100474         &lt;br /&gt;82166370484403199890008895243450658541227588666881         &lt;br /&gt;16427171479924442928230863465674813919123162824586         &lt;br /&gt;17866458359124566529476545682848912883142607690042         &lt;br /&gt;24219022671055626321111109370544217506941658960408         &lt;br /&gt;07198403850962455444362981230987879927244284909188         &lt;br /&gt;84580156166097919133875499200524063689912560717606         &lt;br /&gt;05886116467109405077541002256983155200055935729725         &lt;br /&gt;71636269561882670428252483600823257530420752963450&lt;/span&gt;&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;h4&gt;
Solution&lt;/h4&gt;
This problem is another one that is very easy to brute force, especially since &lt;a href="http://ruby-lang.org/" target="_blank" title="Ruby - A programmer's best friend"&gt;Ruby&lt;/a&gt; has some nice built-in methods that make it a no brainer. One thing we will need is an easy way to take a number and break it up into it’s digits. For that a simple method added to the Integer class will do.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:a1547a80-2567-4d91-a131-b28352ac4f27" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Integer
 def digits
  self.to_s.split('').map { |s| s.to_i }
 end
end&lt;/pre&gt;
&lt;/div&gt;
This method doesn’t do much, it converts the number to a string, splits it into individual characters, then converts the characters back into integers. Again, simple, but this method will come in handy for this problem as well as several in the future.&lt;br /&gt;
The first thing we need to find is all the combinations of 5 consecutive digits. We can make use of the &lt;a href="http://ruby-doc.org/core/classes/Enumerable.html#M001515" target="_blank" title="Ruby Docs"&gt;Enumerable#each_cons&lt;/a&gt; method to accomplish this.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:07f557b4-adf4-4b9c-9e76-4f3179762294" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Problem008
 def partition(number)
  number.digits.each_cons(5)
 end
end&lt;/pre&gt;
&lt;/div&gt;
This will give us an enumerator that contains arrays of 5 consecutive digits. We can now map each of these arrays to the product of their values.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:5ac7e871-51eb-4f59-a8df-019399f0bd59" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Problem008
 def products(number)
  partition(number).map { |a| a.reduce(1, :*) }
 end
end&lt;/pre&gt;
&lt;/div&gt;
And the final step, just find the largest product.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:362a7e32-bbf3-4042-a739-c2fc64341a17" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;THOUSAND_DIGIT_NUM = # number from problem statement
products(THOUSAND_DIGIT_NUM).max

# This could have been written all in one line as
THOUSAND_DIGIT_NUM.digits.each_cons(5).map { |a| a.reduce(1, :*) }.max&lt;/pre&gt;
&lt;/div&gt;
The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_008.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_008_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, finding the only Pythagorean triplet, {a, b, c} for which a + b + c&amp;nbsp; = 1000.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-5385666146409925139?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/gPpPBodaJ1-WLEmN3erqBH0hSV8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gPpPBodaJ1-WLEmN3erqBH0hSV8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/gPpPBodaJ1-WLEmN3erqBH0hSV8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gPpPBodaJ1-WLEmN3erqBH0hSV8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/4IV16j2m30M" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/5385666146409925139/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=5385666146409925139" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/5385666146409925139?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/5385666146409925139?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/4IV16j2m30M/project-eulerproblem-8.html" title="Project Euler–Problem 8" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-8.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEEGQHw_eyp7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-4841319826755181695</id><published>2011-02-22T12:33:00.002-05:00</published><updated>2011-03-09T18:17:01.243-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:17:01.243-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler–Problem 7</title><content type="html">&lt;h4&gt;
Description&lt;/h4&gt;
From &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=7" target="_blank" title="Problem 7"&gt;Project Euler&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.&lt;br /&gt;
What is the 10001st prime number?&lt;/blockquote&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;h4&gt;
Solution&lt;/h4&gt;
Finding primes is an interesting problem. There are a number of ways that we can go about doing so, and it is something we will need to do several times throughout the &lt;a href="http://projecteuler.net/" target="_blank"&gt;Project Euler&lt;/a&gt; problems. So we will want to develop a solution that is not only accurate, but also fairly efficient.&lt;br /&gt;
A fairly efficient method for finding all of the prime numbers below a given value is the &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_eratosthenes" target="_blank"&gt;Sieve of Eratosthenes&lt;/a&gt;.&amp;nbsp; The way it works is simple:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Create&amp;nbsp; a list of consecutive integers from two to n: (2, 3, 4, …, &lt;i&gt;n&lt;/i&gt;) &lt;/li&gt;
&lt;li&gt;Start with &lt;i&gt;p&lt;/i&gt; = 2, the first prime number. &lt;/li&gt;
&lt;li&gt;Remove from the list all multiples of &lt;i&gt;p&lt;/i&gt; less than or equal to &lt;i&gt;n&lt;/i&gt;. &lt;/li&gt;
&lt;li&gt;Find the smallest number remaining on the list greater than &lt;i&gt;p&lt;/i&gt;. This is the next prime number. Set &lt;i&gt;p&lt;/i&gt; equal to this number. &lt;/li&gt;
&lt;li&gt;Repeat this process until &lt;i&gt;p&lt;sup&gt;2&lt;/sup&gt;&lt;/i&gt; is greater than &lt;i&gt;n&lt;/i&gt;. &lt;/li&gt;
&lt;li&gt;All remaining numbers on the list are prime. &lt;/li&gt;
&lt;/ol&gt;
This works extremely well if you know what your upper bound is going to be.&amp;nbsp; While we could take a guess and generate all the primes up to some arbitrarily large number, we would like a way of using this method to generate any arbitrary number of primes. This can still be done if we take into account the following facts:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;If we want an arbitrary limit, there is no &lt;i&gt;n&lt;/i&gt; value, so we cannot stop striking composite numbers as in step 5 above. &lt;/li&gt;
&lt;li&gt;Since we have no upper bound, striking all of the multiples of &lt;i&gt;p&lt;/i&gt; would be impossible (there are infinitely many). So we will only strike the next multiple and worry about the larger ones when we get to them. &lt;/li&gt;
&lt;li&gt;There is insufficient memory to store a list of all numbers (obviously), so we will ignore all primes that have already been yielded and all composites that have already been stricken. Thus we will only store a list of composites that will need to be stricken when we reach them. &lt;/li&gt;
&lt;/ol&gt;
This last observation is the real trick. Instead of keeping a list of prime candidates, we’ll keep&amp;nbsp; a list of known composites to strike. We’ll also need to know what primes that composite is a multiple of in order to know what the next multiples of each will be. For instance, 12 is a multiple of both 2 and 3. This means that it would be stricken by both in the original sieve algorithm. We would need to know this so that we can see that the next multiple of 2 will be 14 and the next multiple of 3 will be 15.&lt;br /&gt;
One final note before we start implementing our algorithm. When we reach a prime number &lt;i&gt;p&lt;/i&gt;, we do not need to strike the first &lt;i&gt;p-1&lt;/i&gt; multiples of &lt;i&gt;p.&lt;/i&gt; This is because those multiples will be a multiple of another prime as well. This can easily be seen with an example:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Given the prime number &lt;i&gt;p&lt;/i&gt; = 11 &lt;/li&gt;
&lt;li&gt;2&lt;i&gt;p&lt;/i&gt; = 22 which is a multiple of 2 &lt;/li&gt;
&lt;li&gt;3&lt;i&gt;p&lt;/i&gt; = 33 which is a multiple of 3 &lt;/li&gt;
&lt;li&gt;4&lt;i&gt;p&lt;/i&gt; = 2*2&lt;i&gt;p&lt;/i&gt; = 44 which is a multiple of 2 &lt;/li&gt;
&lt;li&gt;5&lt;i&gt;p&lt;/i&gt; = 55 which is a multiple of 5 &lt;/li&gt;
&lt;li&gt;6&lt;i&gt;p&lt;/i&gt; = 2*3&lt;i&gt;p&lt;/i&gt; = 66 which is a multiple of both 2 and 3 &lt;/li&gt;
&lt;li&gt;7&lt;i&gt;p&lt;/i&gt; = 77 which is a multiple of 7 &lt;/li&gt;
&lt;li&gt;8&lt;i&gt;p&lt;/i&gt; = 2*2*2&lt;i&gt;p&lt;/i&gt; = 88 which is a multiple of 2 &lt;/li&gt;
&lt;li&gt;9&lt;i&gt;p&lt;/i&gt; = 3*3&lt;i&gt;p&lt;/i&gt; = 99which is a multiple of 3 &lt;/li&gt;
&lt;li&gt;10&lt;i&gt;p&lt;/i&gt; = 5*2&lt;i&gt;p&lt;/i&gt; = 110 which is a multiple of both 2 and 5 &lt;/li&gt;
&lt;li&gt;11&lt;i&gt;p&lt;/i&gt; = 121 which is only a multiple of 11. &lt;/li&gt;
&lt;/ul&gt;
Therefore, when we find a prime &lt;i&gt;p&lt;/i&gt;, the first composite we need to concern ourselves with is &lt;i&gt;p&lt;sup&gt;2&lt;/sup&gt;&lt;/i&gt;.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d7369095-06e7-4917-8a2c-6ea48881357c" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class PrimeGenerator
 include Enumerable

 def each
  composites = {}
  n = 2
  while true
   if composites.has_key?(n)
    factors = composites.delete(n)
    factors.each { |factor| composites.merge!(n + factor =&amp;gt; [factor]) { |key,left,right| left + right } }
   else
    composites.merge!(n**2 =&amp;gt; [n]) { |key,left,right| left + right }
    yield n
   end
   n += 1
  end
 end
end&lt;/pre&gt;
&lt;/div&gt;
With this class in place, finding the 10,001st prime is as simple as:&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:b701bb07-227e-4d0a-a839-33ae2bfc6ec1" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;PrimeGenerator.new.take(10001).drop(10000).first&lt;/pre&gt;
&lt;/div&gt;
The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_007.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_007_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, discovering the largest product of five consecutive digits in a 1000-digit number.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-4841319826755181695?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/0l_s2xrDYhlqZ1oRdWuI_-ZDScE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0l_s2xrDYhlqZ1oRdWuI_-ZDScE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/0l_s2xrDYhlqZ1oRdWuI_-ZDScE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0l_s2xrDYhlqZ1oRdWuI_-ZDScE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/nbBpFcEjF3o" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/4841319826755181695/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=4841319826755181695" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/4841319826755181695?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/4841319826755181695?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/nbBpFcEjF3o/project-eulerproblem-7.html" title="Project Euler–Problem 7" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-7.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEEBQHk8eyp7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-7060636443996972118</id><published>2011-02-19T23:52:00.003-05:00</published><updated>2011-03-09T18:17:31.773-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:17:31.773-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler–Problem 6</title><content type="html">&lt;h4&gt;
Description&lt;/h4&gt;
From &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=6" target="_blank" title="Problem 6"&gt;Project Euler&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
The sum of the squares of the first ten natural numbers is,&lt;br /&gt;
1&lt;sup&gt;2&lt;/sup&gt; + 2&lt;sup&gt;2&lt;/sup&gt; + ... + 10&lt;sup&gt;2&lt;/sup&gt; = 385&lt;br /&gt;
The square of the sum of the first ten natural numbers is,&lt;br /&gt;
(1 + 2 + ... + 10)&lt;sup&gt;2&lt;/sup&gt; = 55&lt;sup&gt;2&lt;/sup&gt; = 3025&lt;br /&gt;
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.&lt;br /&gt;
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.&lt;/blockquote&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;h4&gt;
Solution&lt;/h4&gt;
The straight forward solution to this problem is just to loop through the numbers from 1 to 100 and compute both the square of the sum and the sum of the squares.&amp;nbsp; &lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:c12d783d-5ab5-40ef-8e2e-4a46ca9ca711" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Problem006
 def sum_of_squares(n)
  (1..n).map { |n| n**2 }.reduce(:+)
 end

 def square_of_sum(n)
  (1..n).reduce(:+) ** 2
 end

 def difference(n)
  sum_of_squares(n) - square_of_sum(n)
 end
end

Problem006.new.difference(100)&lt;/pre&gt;
&lt;/div&gt;
Of course, this iterative solution works fine on such a small range, but starts to break down as the range gets larger. Fortunately, there is a simple formula for computing the sum of numbers 1 to n. &lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;sum(n) = n x (n + 1) / 2&lt;/span&gt;&lt;br /&gt;
Using this information we can now calculate the square of the sum in constant time.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7cc26a66-bf3e-4b70-843a-ceeacb4a5c3f" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Problem006
 def square_of_sum(n)
  (n * (n + 1) / 2) ** 2
 end
end&lt;/pre&gt;
&lt;/div&gt;
Similarly, there is a formula for calculating the sum of squares from 1 to n. The derivation of which can be found &lt;a href="http://www.macalester.edu/%7Ebressoud/pub/CBN2.pdf" target="_blank" title="Calculus Before Newton and Leibniz: Part II"&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;sum&lt;sup&gt;2&lt;/sup&gt;(n) = (n x (n + 1) x (2n + 1)) / 6&lt;/span&gt;&lt;br /&gt;
So finally, we can optimize the sum of squares to also work in constant time. This gives us a fast solution regardless of the size of n.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:f751c707-98db-4760-9e5c-aa028b563068" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Problem006
 def sum_of_squares(n)
  ((2*n+1) * (n+1) * n) / 6
 end
end&lt;/pre&gt;
&lt;/div&gt;
The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_006.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_006_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, finding the 10,001st prime number.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-7060636443996972118?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/utml4lBP3I0Dwrrn1JS7g5_yNlg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/utml4lBP3I0Dwrrn1JS7g5_yNlg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/utml4lBP3I0Dwrrn1JS7g5_yNlg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/utml4lBP3I0Dwrrn1JS7g5_yNlg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/BfzXo0121dg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/7060636443996972118/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=7060636443996972118" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/7060636443996972118?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/7060636443996972118?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/BfzXo0121dg/project-eulerproblem-6.html" title="Project Euler–Problem 6" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-6.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEEDQHw8eCp7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-2492177206990103035</id><published>2011-02-18T08:33:00.003-05:00</published><updated>2011-03-09T18:17:51.270-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:17:51.270-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler–Problem 5</title><content type="html">&lt;h4&gt;
Description&lt;/h4&gt;
From &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=5" target="_blank" title="Problem 5"&gt;Project Euler&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.&lt;br /&gt;
What is the smallest positive number that is &lt;span style="border-bottom: 1px dotted;" title="divisible with no remainder"&gt;evenly divisible&lt;/span&gt; by all of the numbers from 1 to 20?&lt;/blockquote&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;h4&gt;
Solution&lt;/h4&gt;
Another name for the smallest number that is evenly divisible by two or more numbers is the least common multiple (LCM). In general, finding the least common multiple is an easy task if we make the following observation. All numbers greater than 1 can be expressed uniquely as the product of its prime factors.&amp;nbsp; &lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;10 =&amp;gt; 2, 5 =&amp;gt; 2 x 5&lt;/span&gt;&lt;br /&gt;
In addition, each prime factor may have an exponent greater than one.&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;90 =&amp;gt; 2, 3, 3, 5 =&amp;gt; 2&lt;sup&gt;1&lt;/sup&gt; x 3&lt;sup&gt;2&lt;/sup&gt; x 5&lt;sup&gt;1&lt;/sup&gt;&lt;/span&gt;&lt;br /&gt;
To find the least common multiple of two or more numbers, you take the prime factorization of each number, than multiply the highest exponent of each prime together. This gives the least common multiple. (For more information on how this works, see &lt;a href="http://en.wikipedia.org/wiki/Least_common_multiple" target="_blank" title="Least Common Multiple"&gt;this Wikipedia article&lt;/a&gt;).&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;8 =&amp;gt; 2, 2, 2&amp;nbsp;&amp;nbsp;&amp;nbsp; =&amp;gt; &lt;b&gt;&lt;span style="color: blue;"&gt;2&lt;sup&gt;3&lt;/sup&gt;&lt;/span&gt;&lt;/b&gt;       &lt;br /&gt;18 =&amp;gt; 2, 3, 3&amp;nbsp;&amp;nbsp;&amp;nbsp; =&amp;gt; 2&lt;sup&gt;1&lt;/sup&gt; x &lt;b&gt;&lt;span style="color: blue;"&gt;3&lt;sup&gt;2&lt;/sup&gt;           &lt;/span&gt;&lt;/b&gt;84 =&amp;gt; 2, 2, 3, 7 =&amp;gt; 2&lt;sup&gt;2&lt;/sup&gt; x 3&lt;sup&gt;1&lt;/sup&gt; x &lt;b&gt;&lt;span style="color: blue;"&gt;7&lt;sup&gt;1&lt;/sup&gt;&lt;/span&gt;&lt;/b&gt;       &lt;br /&gt;lcm(8, 9, 21) =&amp;gt; 2&lt;sup&gt;3&lt;/sup&gt; x 3&lt;sup&gt;2&lt;/sup&gt; x 7&lt;sup&gt;1&lt;/sup&gt; =&amp;gt; 504&lt;/span&gt;&lt;br /&gt;
We already created a method that can compute the prime factors for us in problem 3. However, now, we don’t want to simply list them, we want to list the factors exactly once with their associated exponent. To do this, we’ll change the prime_factors method to instead return a hash. The keys of the hash will be the prime factors, and the values will be the exponent.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:4341784b-3757-4b68-8f02-25d53d3df62b" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Integer
 def prime_factors
  prime = 2
  result = {}
  value = self
  while value &amp;gt;= prime ** 2
   if value % prime == 0
    result.merge!(prime =&amp;gt; 0) if not result.has_key?(prime)
    result[prime] += 1
    value /= prime
   else
    prime += 1
   end
  end
  result.merge!(value =&amp;gt; 0) if not result.has_key?(value)
  result[value] += 1
  return result
 end
end&lt;/pre&gt;
&lt;/div&gt;
Note that this does force us to change the solution to problem 3: &lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:6de7027e-70ea-45f5-9e65-c2eda4504dd0" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;600_851_475_143.prime_factors.keys.max&lt;/pre&gt;
&lt;/div&gt;
Only a minor change, we are now calling keys on prime_factors before calling max.&lt;br /&gt;
Now the method for computing the least common multiple becomes trivial:&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:a14c4619-07d0-41ca-94e3-8f9f96b2270c" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Problem005
 def lcm(*numbers)
  factors = {}
  numbers.each do |n|
   factors.merge!(n.prime_factors) do |key,x,y|
    x &amp;gt; y : x : y
   end
  end
  factors.inject(1) { |product,pair| product * (pair[0] ** pair[1]) }
 end
end
Problem005.new.lcm 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20&lt;/pre&gt;
&lt;/div&gt;
The only thing to note here is that we are overriding the merge! methods logic. Instead of overwriting the current value of the hash with the new one, we always add the larger number to the hash. Thus, { 5 =&amp;gt; 2}.merge( 5 =&amp;gt; 1) would result in { 5 =&amp;gt; 2 } instead of { 5 =&amp;gt; 1 }.&lt;br /&gt;
The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_005.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_005_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, the difference between the sum of squares and the square of sums.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-2492177206990103035?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/kDoI2ea5PDxnr9lBv1Eo1sx2J_g/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kDoI2ea5PDxnr9lBv1Eo1sx2J_g/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/kDoI2ea5PDxnr9lBv1Eo1sx2J_g/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kDoI2ea5PDxnr9lBv1Eo1sx2J_g/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/ZtXfQ449ftA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/2492177206990103035/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=2492177206990103035" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/2492177206990103035?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/2492177206990103035?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/ZtXfQ449ftA/project-eulerproblem-5.html" title="Project Euler–Problem 5" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-5.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEENQ3o5fSp7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-6477003704168425427</id><published>2011-02-17T11:06:00.003-05:00</published><updated>2011-03-09T18:18:12.425-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:18:12.425-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler–Problem 4</title><content type="html">&lt;h4&gt;
Description&lt;/h4&gt;
From &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=4" target="_blank" title="Problem 4"&gt;Project Euler&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.&lt;br /&gt;
Find the largest palindrome made from the product of two 3-digit numbers.&lt;/blockquote&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;h4&gt;
Solution&lt;/h4&gt;
There are two parts to the solution to problem 4. The first part is to generate all the multiples of 3-digit numbers. This is relatively trivial. All we need to do multiple all the combinations of numbers from 100 to 999. The only obvious optimization here is to recognize that 100 * 999 is the same as 999 * 100. So we really only have to compute one of the two.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7f623c0d-2d76-4089-a4cd-e6e8d1bdf439" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Problem004
 def multiples(range)
  Enumerator.new do |yielder|
   range.each do |i|
    (range.first..i).each do |j|
     yielder &amp;lt;&amp;lt; i * j
    end
   end
  end
 end
end&lt;/pre&gt;
&lt;/div&gt;
Here I’ve used an Enumerator. It simply loops through all the numbers in a range and multiplies those values by all the numbers in the range less than that number. Each time, it yields these values.&lt;br /&gt;
The second part of the problem is to determine whether a number is a palindrome or not. This too is straight forward. All we need to do is compare whether the number is equal to its reverse. Since numbers do not have a reverse method, we’ll have to convert it to a string first.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:fd099908-1e9a-46bc-ab33-1ab8ce49f04f" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Integer
 def palindrome?
  text = self.to_s
  text == text.reverse
 end
end&lt;/pre&gt;
&lt;/div&gt;
With all of this in place, finding the largest palindrome is as easy as:&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:ff168682-cfca-4767-b20d-320c7576a48d" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: text;"&gt;Problem004.new.multiples(100..999).select { |x| x.palindrome? }.max&lt;/pre&gt;
&lt;/div&gt;
The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_004.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_004_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, finding the smallest number divisible by each of the number 1 through 20.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-6477003704168425427?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/fEPgcwmL9-ZL5YICcpNdTgLfu44/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fEPgcwmL9-ZL5YICcpNdTgLfu44/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/fEPgcwmL9-ZL5YICcpNdTgLfu44/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fEPgcwmL9-ZL5YICcpNdTgLfu44/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/jJwFHbeSnh4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/6477003704168425427/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=6477003704168425427" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6477003704168425427?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6477003704168425427?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/jJwFHbeSnh4/project-eulerproblem-4.html" title="Project Euler–Problem 4" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-4.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEAGQ3w4fSp7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-6262633230309168687</id><published>2011-02-16T19:46:00.003-05:00</published><updated>2011-03-09T18:18:42.235-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:18:42.235-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler–Problem 3</title><content type="html">&lt;h4&gt;
Description&lt;/h4&gt;
From &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=3" target="_blank" title="Problem 3"&gt;Project Euler&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
The prime factors of 13,195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600,851,475,143?&lt;/blockquote&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;h4&gt;
Solution&lt;/h4&gt;
The solution to this problem actually involves solving another problem that we’ll need later. Namely: computing the prime factors of a number. For this we’ll actual extend the Integer class to include a &lt;span style="font-family: Consolas;"&gt;prime_factors&lt;/span&gt; method. This method will return an array that contains all of the prime factors for the current number. The solution will then be to simply find the largest value in the array.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:2542e62f-037c-4161-b4a9-fd8309e917d5" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Integer
 def prime_factors
  prime = 2
  result = []
  value = self
  while value &amp;gt;= prime ** 2
   if value % prime == 0
    result &amp;lt;&amp;lt; prime
    value /= prime
   else
    prime += 1
   end
  end
  result &amp;lt;&amp;lt; value
 end
end
600_851_475_143.prime_factors.max&lt;/pre&gt;
&lt;/div&gt;
There are a number of optimizations that we can eventually make to this method, but we’ll hold off until we actually need the pieces necessary to improve it. One such piece will come in problem five.&lt;br /&gt;
The &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_003.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_003_spec.rb" target="_blank"&gt;specifications&lt;/a&gt; can be seen on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, finding the largest palindrome made from the product of two 3-digit numbers.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-6262633230309168687?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/SEax_OX7CsNyuVOklan_yqSWV-M/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SEax_OX7CsNyuVOklan_yqSWV-M/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/SEax_OX7CsNyuVOklan_yqSWV-M/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/SEax_OX7CsNyuVOklan_yqSWV-M/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/p0UfFEfmpQk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/6262633230309168687/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=6262633230309168687" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6262633230309168687?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6262633230309168687?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/p0UfFEfmpQk/project-eulerproblem-3.html" title="Project Euler–Problem 3" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-3.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEACRXg6eyp7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-6909343816069336370</id><published>2011-02-16T14:37:00.003-05:00</published><updated>2011-03-09T18:19:24.613-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:19:24.613-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler–Problem 2</title><content type="html">&lt;h4&gt;
Description&lt;/h4&gt;
From &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=2" target="_blank" title="Problem 2"&gt;Project Euler&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:    &lt;br /&gt;
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …     &lt;br /&gt;
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even valued terms.&lt;/blockquote&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;h4&gt;
Solution&lt;/h4&gt;
The solution to this problem again has a trivial solution and a slight optimization. The trivial solution simply involves creating a method to calculate the Fibonacci sequence up to 4 million and a second method to add the even terms together.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:4e0db333-96c4-45a1-8aaa-8d53d6cf4ea0" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: vb;"&gt;class Problem002
 include Enumerable

 def each
  last = 1; current = 2
  yield last; yield current
  while true
   last, current = current, last + current
   yield current
  end
 end

 def sum_to(maximum)
  take_while { |n| n &amp;lt; maximum }.select { |n| n.even? }.inject { |sum,n| sum + n}
 end
end

Problem002.new.sum_to(4_000_000)&lt;/pre&gt;
&lt;/div&gt;
Notice that here I’ve used a mix-in to make the problem itself an enumerable collection of Fibonacci terms. However, we have a slight optimization we can make by observing two things. &lt;br /&gt;
First is that we don’t actually need to yield all of the terms of the Fibonacci sequence, only the even terms. Notice that the only way to get an even term is if the previous two terms are both even or both odd. If we start with 1 and 2 our next term will be odd because an odd plus an even is odd. The next term will be odd as well because we now have an even followed by an odd. The third term will be even since an odd plus an odd is even. For the fourth term we are back to an odd followed by an even. So we see that if we start with the second term (2), we only need to yield every third term in the sequence&lt;sup&gt;.&lt;/sup&gt;&lt;br /&gt;
Our second observation is regarding calculating the terms themselves. we can see that each term is specified by the previous two terms in the sequence:&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:ee52529d-b624-416a-b52c-975bcce330b6" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: text;"&gt;n = (n-1) + (n-2)
n-1 = (n-2) + (n-3)&lt;/pre&gt;
&lt;/div&gt;
However, this means that we can also express each term by any previous two terms in the sequence. Reversing this, it means we can specify any future term by the current two terms. So we can come up with the following:&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:b020386d-6506-4075-a053-dbdd51ff6cfd" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: text;"&gt;Given n and m (the term before n)
n+1 = n + m
n+2 = (n+1) + n = (n + m) + n = 2n + m
n+3 = (n+2) + (n+1) = (2n + m) + (n + m) = 3n + 2m&lt;/pre&gt;
&lt;/div&gt;
As you can see, term n+1 doesn’t ever need to be computed since we can express n+2 and n+3 in terms of n and m.&amp;nbsp; This saves us computed 1/3 of all the values.&amp;nbsp; So our new more optimal solution looks like this:&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:5eea9d8e-bf7a-48f9-a6fa-6afa4bcaf0b7" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;class Problem002
 include Enumerable

 def each
  current = 2
  last = 1
  while true
   yield current
   last, current = 2*current + last, 3*current + 2*last
  end
 end

 def sum_to(maximum)
  take_while { |n| n &amp;lt; maximum }.inject { |sum,n| sum + n }
 end
end
Problem002.new.sum_to(4_000_000)&lt;/pre&gt;
&lt;/div&gt;
As always, the source is available on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;, both the &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_002.rb" target="_blank"&gt;full source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_002_spec.rb" target="_blank"&gt;specifications&lt;/a&gt;. Next time, finding the largest prime factor of a composite number.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-6909343816069336370?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ayG6DEZi7qj5wRL2GkyWLSdBzSE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ayG6DEZi7qj5wRL2GkyWLSdBzSE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ayG6DEZi7qj5wRL2GkyWLSdBzSE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ayG6DEZi7qj5wRL2GkyWLSdBzSE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/CLfUkXG_RMs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/6909343816069336370/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=6909343816069336370" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6909343816069336370?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/6909343816069336370?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/CLfUkXG_RMs/project-eulerproblem-2.html" title="Project Euler–Problem 2" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-2.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEANR3syfip7ImA9Wx9aF0w.&quot;"><id>tag:blogger.com,1999:blog-7139493952533550388.post-3262685051404775524</id><published>2011-02-02T10:18:00.003-05:00</published><updated>2011-03-09T18:19:56.596-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-09T18:19:56.596-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler–Problem 1</title><content type="html">In my constant effort to learn more, I’ve run into&amp;nbsp; a number of road blocks with my progression with &lt;a href="http://github.com/hsteinhilber/yabe" target="_blank" title="Yet-Another-Blog-Engine on GitHub"&gt;YABE&lt;/a&gt;. Why? Because I’m trying to write my blog engine in Ruby on Rails, while I have little real experience with &lt;a href="http://ruby-lang.org/" target="_blank" title="Ruby - A programmer's best friend"&gt;Ruby&lt;/a&gt;, &lt;a href="http://rubyonrails.org/" target="_blank" title="Rails - Web development that doesn't hurt"&gt;Rails&lt;/a&gt;, &lt;a href="http://rspec.info/" target="_blank"&gt;RSpec&lt;/a&gt;, &lt;a href="http://rubygems.org/" target="_blank" title="RubyGems - your community gem host"&gt;Gems&lt;/a&gt;, etc. etc. So I am trying to rectify this by at least getting the basics of the “Ruby way” down. My solution? Well simple really. I am going to go through and start implementing the problems from &lt;a href="http://projecteuler.net/" target="_blank"&gt;Project Euler&lt;/a&gt; in Ruby and testing my solutions using RSpec. I’ve already completed a few of the problems, which you can see &lt;a href="http://github.com/hsteinhilber/project_euler" target="_blank" title="hsteinhilber's project_euler @ github.com"&gt;here&lt;/a&gt;.&amp;nbsp; &lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;h4&gt;
Basic Structure&lt;/h4&gt;
The project has a very simple structure. There is a batch file in the \bin folder that allows you to run a simple console with all of the problems I have completed. Selecting a problem will give the result and how many seconds it took to generate that result. Every problem will have a corresponding specification, and possibly a number of utility classes (which will have their own specifications in turn).&lt;br /&gt;
&lt;h4&gt;
Problem #1&lt;/h4&gt;
From the &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=1" target="_blank"&gt;Project Euler website&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.&lt;br /&gt;
Find the sum of all the multiples of 3 or 5 below 1000.&lt;/blockquote&gt;
The trivial solution for this problem is actually quite easy. Simply loop through all of the number from 1 to 1000 (exclusive) and add all of the numbers that are divisible by 3 or 5. It might look something like this:&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:11829e85-e33b-4c10-abe2-f1c06ac63452" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: ruby;"&gt;def sum
 (3...1000).inject do |sum,value|
  if value % 3 == 0 or value % 5 == 0
   sum + value
  else
   sum
  end
 end
end&lt;/pre&gt;
&lt;/div&gt;
Of course, I wasn’t satisfied with this solution, as there are two divisions going on for every value tested. And we are testing every value conceivable, even ones we can easily see will not be used. What if instead we were to only iterate through the multiples of 3 and 5, skipping all the other integers. We could then eliminate the division and the extra iterations.&lt;br /&gt;
How do we iterate over only the multiples of 3 and 5 though? Well, the first step is to notice that the multiples are spaced in a cyclical pattern. Take a look at this:&lt;br /&gt;
&lt;table border="0" cellpadding="2" cellspacing="0" style="width: 400px;"&gt;&lt;tbody&gt;
&lt;tr&gt;
      &lt;td valign="top" width="50"&gt;3&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;5&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;6&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;9&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;10&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;12&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;15&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;18&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td valign="top" width="50"&gt;18&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;20&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;21&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;24&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;25&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;27&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;30&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;33&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td valign="top" width="50"&gt;33&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;35&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;36&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;39&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;40&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;42&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;45&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;48&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td valign="top" width="50"&gt;&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;+2&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;+1&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;+3&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;+1&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;+2&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;+3&lt;/td&gt;

      &lt;td valign="top" width="50"&gt;+3&lt;/td&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
The cycle repeats itself every 6 values. So we can simply use this sequence to iterate over all of the multiples and just add all of the values less than 1000 together! Here is were it gets fun, we can use an Enumerable to handle the iteration of these values, and then use the standard Enumerable mixin methods to solve the problem, like so:&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:c61d1118-0553-4a72-8b77-d2c15ef1ac8e" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;
&lt;pre class="brush: sql;"&gt;class Problem001
 include Enumerable
 ADD_VALUES = [2,1,3,1,2,3,3]  
 def each    
  n = 3    
  i = 0    
  while (true) do      
   yield n      
   n += ADD_VALUES[i]      
   i = (i + 1) % ADD_VALUES.length    
  end  
 end
 def sum(maximum)
  inject { |sum,value| break if value &amp;gt; maximum; sum + value; }
 end
end&lt;/pre&gt;
&lt;/div&gt;
You can see the full &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/lib/problem_001.rb" target="_blank"&gt;source&lt;/a&gt; and &lt;a href="https://github.com/hsteinhilber/project_euler/blob/master/spec/problem_001_spec.rb" target="_blank"&gt;specification&lt;/a&gt; on &lt;a href="http://github.com/" target="_blank"&gt;github&lt;/a&gt;. Next time, we’ll take a look at problem #2: summing the even values of a Fibonacci sequence less than four million.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7139493952533550388-3262685051404775524?l=hsteinhilber.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/HxG0PTAHwfp2uP3rpVqBBB9oMwk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HxG0PTAHwfp2uP3rpVqBBB9oMwk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/HxG0PTAHwfp2uP3rpVqBBB9oMwk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HxG0PTAHwfp2uP3rpVqBBB9oMwk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/RandomCodePatterns/~4/_OXs3sQw7vA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://hsteinhilber.blogspot.com/feeds/3262685051404775524/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7139493952533550388&amp;postID=3262685051404775524" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/3262685051404775524?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7139493952533550388/posts/default/3262685051404775524?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/RandomCodePatterns/~3/_OXs3sQw7vA/project-eulerproblem-1.html" title="Project Euler–Problem 1" /><author><name>Harry Steinhilber, Jr.</name><uri>http://www.blogger.com/profile/14221523082420258958</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="21" height="32" src="http://bp0.blogger.com/_oJpbJ8BTJIY/SI7bNptCfSI/AAAAAAAAAAQ/xi9YxqK--sE/S220/My+Photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://hsteinhilber.blogspot.com/2011/02/project-eulerproblem-1.html</feedburner:origLink></entry></feed>

