euler/ruby/euler014.rb

17 lines
285 B
Ruby
Raw Normal View History

2014-04-25 23:59:14 +00:00
def collatz(num)
2014-11-20 22:20:39 +00:00
[].tap do |list|
until num == 1
if num.even?
list << (num = num / 2)
else
list << (num = (3 * num) + 1)
end
2014-04-25 23:59:14 +00:00
end
2014-11-20 22:20:39 +00:00
list << 1
2014-04-25 23:59:14 +00:00
end
end
2014-11-20 22:20:39 +00:00
def solution
(1..999999).map { |i| collatz(i).count }.each_with_index.max[1] + 1
2014-04-25 23:59:14 +00:00
end