Skip to content

Instantly share code, notes, and snippets.

@yuuki
Created August 5, 2012 18:34
Show Gist options
  • Save yuuki/3266592 to your computer and use it in GitHub Desktop.
Save yuuki/3266592 to your computer and use it in GitHub Desktop.
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