Skip to content

Instantly share code, notes, and snippets.

@banyan
Created December 27, 2009 11:08
Show Gist options
  • Select an option

  • Save banyan/264244 to your computer and use it in GitHub Desktop.

Select an option

Save banyan/264244 to your computer and use it in GitHub Desktop.
def collatz(arg)
if arg.kind_of?(Integer)
arg = [arg]
elsif arg[-1] == 1
return [arg.size - 1, "[#{arg.join(', ')}]"]
end
if (arg[-1] % 2) == 0
collatz(arg << arg[-1] / 2)
else
collatz(arg << arg[-1] * 3 + 1)
end
end
p collatz(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment