<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Project Euler: Problem 2 in Ruby</title>
	<atom:link href="http://joezack.com/index.php/2009/01/11/project-euler-problem-2-in-ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://joezack.com/index.php/2009/01/11/project-euler-problem-2-in-ruby/</link>
	<description>Code Musings and Such</description>
	<lastBuildDate>Fri, 23 Apr 2010 20:48:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: me</title>
		<link>http://joezack.com/index.php/2009/01/11/project-euler-problem-2-in-ruby/comment-page-1/#comment-1051</link>
		<dc:creator>me</dc:creator>
		<pubDate>Thu, 31 Dec 2009 03:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://joezack.com/?p=480#comment-1051</guid>
		<description>@Dan
I missed this comment somehow Dan, great info thanks very much!</description>
		<content:encoded><![CDATA[<p>@Dan<br />
I missed this comment somehow Dan, great info thanks very much!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shah</title>
		<link>http://joezack.com/index.php/2009/01/11/project-euler-problem-2-in-ruby/comment-page-1/#comment-1018</link>
		<dc:creator>shah</dc:creator>
		<pubDate>Sun, 18 Oct 2009 16:06:39 +0000</pubDate>
		<guid isPermaLink="false">http://joezack.com/?p=480#comment-1018</guid>
		<description>1 1 2 3 5 8 13 21 plz solve the problem using for loop with java script.</description>
		<content:encoded><![CDATA[<p>1 1 2 3 5 8 13 21 plz solve the problem using for loop with java script.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Kubb</title>
		<link>http://joezack.com/index.php/2009/01/11/project-euler-problem-2-in-ruby/comment-page-1/#comment-251</link>
		<dc:creator>Dan Kubb</dc:creator>
		<pubDate>Sat, 14 Mar 2009 06:15:38 +0000</pubDate>
		<guid isPermaLink="false">http://joezack.com/?p=480#comment-251</guid>
		<description>How I&#039;ve been approaching Project Euler is I try to solve every problem at least twice: from the programmer&#039;s point of view, and then from the mathemetician&#039;s.  I&#039;m not a mathematician at all, but I&#039;m using the project to self-teach myself concepts that I&#039;d probably learned once and forgotten.

Since I&#039;m more of a programmer my first attempt at this problem was:

sum      = 0
previous = 1
current  = 2

while current &lt;= 4_000_000
  sum += current if current.even?  # Fixnum#even? available in Ruby 1.8.7
  previous, current = current, current + previous
end

Which is pretty much like Olathe&#039;s approach (except you&#039;ll note his code is for a slightly different question).  The bad thing about this is that every 3rd time through the loop is when it actually adds to the sum (since every 3rd number in the fibonacci sequence is even).

Some of the first few comments in that thread outline a mathematical approach that gives you an approximate answer which you round to get the right one.

I did some digging and found out there&#039;s a mathematical formula for this exact case already called Binet&#039;s fomula (strangely enough no one mentions the formula name, but the&#039;s a cryptic -- to me -- explanation on page 1 of the problem 2 thread). I wrote a solution for in Ruby using this formula:

sq5 = Math.sqrt(5)
phi = (1 + sq5) / 2.0

sum = 0
n   = 3

while sum &lt;= 4_000_000
  sum += (phi ** n - (1 - phi) ** n) / sq5
  n += 3
end

sum = sum.to_i

This loops the minimal number of times and scales much better than the first solution as the limit increases.</description>
		<content:encoded><![CDATA[<p>How I&#8217;ve been approaching Project Euler is I try to solve every problem at least twice: from the programmer&#8217;s point of view, and then from the mathemetician&#8217;s.  I&#8217;m not a mathematician at all, but I&#8217;m using the project to self-teach myself concepts that I&#8217;d probably learned once and forgotten.</p>
<p>Since I&#8217;m more of a programmer my first attempt at this problem was:</p>
<p>sum      = 0<br />
previous = 1<br />
current  = 2</p>
<p>while current &lt;= 4_000_000<br />
  sum += current if current.even?  # Fixnum#even? available in Ruby 1.8.7<br />
  previous, current = current, current + previous<br />
end</p>
<p>Which is pretty much like Olathe&#8217;s approach (except you&#8217;ll note his code is for a slightly different question).  The bad thing about this is that every 3rd time through the loop is when it actually adds to the sum (since every 3rd number in the fibonacci sequence is even).</p>
<p>Some of the first few comments in that thread outline a mathematical approach that gives you an approximate answer which you round to get the right one.</p>
<p>I did some digging and found out there&#8217;s a mathematical formula for this exact case already called Binet&#8217;s fomula (strangely enough no one mentions the formula name, but the&#8217;s a cryptic &#8212; to me &#8212; explanation on page 1 of the problem 2 thread). I wrote a solution for in Ruby using this formula:</p>
<p>sq5 = Math.sqrt(5)<br />
phi = (1 + sq5) / 2.0</p>
<p>sum = 0<br />
n   = 3</p>
<p>while sum &lt;= 4_000_000<br />
  sum += (phi ** n &#8211; (1 &#8211; phi) ** n) / sq5<br />
  n += 3<br />
end</p>
<p>sum = sum.to_i</p>
<p>This loops the minimal number of times and scales much better than the first solution as the limit increases.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
