Created
August 5, 2012 18:34
-
-
Save yuuki/3266592 to your computer and use it in GitHub Desktop.
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
def collatz(i) | |
Enumerator.new do |y| | |
n = i | |
while n != 1 do | |
n = n.even? ? n/2 : 3*n + 1 | |
y << n | |
end | |
end | |
end | |
max = (1...1000000).each.map do |i| | |
{ input: i, count: collatz(i).count } | |
end.max {|i, j| i[:count] <=> j[:count] } | |
puts max[:input] | |
# 837799 | |
# time 105s CPU: Core i5 1.6GHz, MEM: 4GB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment