Skip to content

Instantly share code, notes, and snippets.

@antimon2
Created May 5, 2012 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antimon2/2601304 to your computer and use it in GitHub Desktop.
Save antimon2/2601304 to your computer and use it in GitHub Desktop.
collatz.py
def collatz(n):
if n >= 0:
yield n
while n > 1:
n = n / 2 if n % 2 == 0 else n * 3 + 1
yield n
# list(num for num in collatz(3))
# => [3, 10, 5, 16, 8, 4, 2, 1]
# len(list(num for num in collatz(27)))
# => 112
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment