Created
December 27, 2009 11:08
-
-
Save banyan/264244 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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