Skip to content

Instantly share code, notes, and snippets.

@rassoc
rassoc / competition.rb
Last active June 30, 2021 22:38
collatz
# 0.53 s, 31424 kb
$memo = {}
def collatz(n)
return $memo[n] if $memo[n]
$memo[n] = case
when n == 1 then 1
when n.even? then 1 + collatz(n.div 2)
else 1 + collatz(3 * n + 1)
end