Skip to content

Instantly share code, notes, and snippets.

@oscardelben
Created March 17, 2009 11:22
Show Gist options
  • Save oscardelben/80474 to your computer and use it in GitHub Desktop.
Save oscardelben/80474 to your computer and use it in GitHub Desktop.
@table = {1 => 1}
result = [0,0]
def next_number(n)
if n.even?
n / 2
else
3 * n + 1
end
end
def sequence(n)
prev_value = @table[n]
if prev_value
prev_value
else
@table[n] = 1 + sequence( next_number(n) )
end
end
1_000_000.downto(1) do |n|
seq = sequence(n)
if seq > result[1]
result = [n, seq]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment