Created
May 5, 2012 10:08
-
-
Save antimon2/2601304 to your computer and use it in GitHub Desktop.
collatz.py
This file contains 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(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