<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users541</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/541" rel="self"/>
  <title>Jeff</title>
  <updated>Fri Apr 04 00:30:49 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code276</id>
    <published>2008-04-04T00:30:49-07:00</published>
    <updated>2008-07-24T07:50:30-07:00</updated>
    <title>[Ruby] Euler 14</title>
    <content type="html">&lt;p&gt;The problem description is here: &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=14" target="_blank"&gt;http://projecteuler.net/index.php?section=problems&amp;amp;id=14&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My interest was sparked by: &lt;a href="http://diditwith.net/2008/04/03/ApplesAndOranges.aspx" target="_blank"&gt;http://diditwith.net/2008/04/03/ApplesAndOranges.aspx&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;require 'benchmark' 

def euler_chain_size(start)
  length = 0 
  while start &amp;gt; 1 
    start = start%2 == 0 ? start/2 : 3*start+1 
    length += 1 
    if @chains.has_key?(start) 
      length += @chains[start] 
      break 
    end 
  end 
  length 
end 

@chains = {} 
max_chain_size = 0 
max_chain_start = 1 

Benchmark.bm do |x| 
  x.report do 
    1.upto(1000000) do |i| 
      chain_size = euler_chain_size(i) 
      @chains[i] = chain_size 
      if chain_size &amp;gt; max_chain_size 
        max_chain_size = chain_size 
        max_chain_start = i 
      end 
    end 
  end 
  puts &amp;quot;(#{max_chain_start}, #{max_chain_size})&amp;quot; 
end 

&lt;/pre&gt;</content>
    <author>
      <name>Jeff</name>
      <email>jeff@thequeue.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/276-euler-14" rel="alternate"/>
  </entry>
</feed>

