Skip to content

Instantly share code, notes, and snippets.

@jsomers
Created February 8, 2011 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsomers/817263 to your computer and use it in GitHub Desktop.
Save jsomers/817263 to your computer and use it in GitHub Desktop.
A solution to Project Euler problem #14
index = {}
def nxt(n)
(n % 2 == 0 ? n / 2 : 3 * n + 1)
end
(2..1_000_000).each do |n|
c = n + 0
ct = 0
while n > 1
if index[n] then ct += index[n]; break end
n = nxt(n)
ct += 1
end
index[c] = ct
end
p index.sort {|a, b| b[1] <=> a[1]}.first[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment