Skip to content

Instantly share code, notes, and snippets.

@alsotang
Created October 24, 2012 15:42
Show Gist options
  • Save alsotang/3946838 to your computer and use it in GitHub Desktop.
Save alsotang/3946838 to your computer and use it in GitHub Desktop.
euler14
collatz = Hash.new do |h, k|
if k == 1
1
elsif k.even?
h[k] = h[k/2]+1
else
h[k] = h[3*k+1] + 1
end
end
num = (1 ... 1_000_000).map do |i|
[i, collatz[i]]
end
.max_by {|item| item[1]}
puts num
# 837799
# 525
# real 0m5.746s
# user 0m5.152s
# sys 0m0.216s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment