# public michaelbernstein /Project-Euler-Solutions

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Added problem 14. Have some others to add when not lazy - but most co…

`…de is fairly trivial`
 ... ... `@@ -0,0 +1,64 @@` 1 `+class Collatz` 2 `+` 3 `+ def initialize` 4 `+ #Initial values.` 5 `+ @lookup_table = {"1" => 1, "2" => 2}` 6 `+ end` 7 `+` 8 `+ def calc(n)` 9 `+ if n % 2 == 0` 10 `+ n / 2` 11 `+ else` 12 `+ 3*n + 1` 13 `+ end` 14 `+ end` 15 `+` 16 `+ def number_of_chains(n)` 17 `+ if x = @lookup_table[n.to_s]` 18 `+ return x` 19 `+ end` 20 `+` 21 `+ y = n` 22 `+ ct = 0 ` 23 `+` 24 `+ while (y != 1)` 25 `+ if y % 2 == 0` 26 `+ y = y / 2` 27 `+ else` 28 `+ y = 3*y + 1` 29 `+ end` 30 `+` 31 `+ #Faster by a bit to eliminate that function call` 32 `+ #y = calc(y)` 33 `+ ` 34 `+ if z = @lookup_table[y.to_s]` 35 `+ ct = z + ct` 36 `+ y = 1` 37 `+ else` 38 `+ ct += 1` 39 `+ end` 40 `+ end` 41 `+` 42 `+ #This is to count itself in the chain #.` 43 `+ ct += 1 ` 44 `+ @lookup_table[n.to_s] = ct` 45 `+ ct` 46 `+ end` 47 `+end` 48 `+` 49 `+` 50 `+c = Collatz.new` 51 `+` 52 `+max = 0` 53 `+max_x = 0` 54 `+(1..999999).each do |x|` 55 `+ t = c.number_of_chains(x)` 56 `+ if t > max` 57 `+ max = t` 58 `+ max_x = x` 59 `+ end` 60 `+end` 61 `+` 62 `+` 63 `+puts "Largest num: #{max_x}, chains: #{max}"` 64 `+`