Project Euler: Problem14

class Collatz
  @@h = Hash::new

  def Collatz.collatz(n)
    if (@@h[n])
      @@h[n]
    elsif (n==1)
      1
    elsif (n % 2 == 0)
      tmp = collatz(n/2)
      @@h[n] = tmp
      1 + tmp
    else
      tmp = collatz(3*n+1)
      @@h[n] = tmp
      1 + tmp
    end
  end
  def Collatz.findlongest(n)
    max = 0
    maxlen = 0
    (1...n).each{|i|
      len = collatz(i)
      if (len > maxlen)
        max = i
        maxlen = len
      end
      p i if i % 1000 == 0
    }
    return max
  end
  def Collatz.printHash
    p @@h
  end
end

p Collatz.findlongest(1_000_000)