Created
October 24, 2012 15:42
-
-
Save alsotang/3946838 to your computer and use it in GitHub Desktop.
euler14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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