Created
December 20, 2016 20:37
-
-
Save zed/1b147fdeb9d66a599f3b60925fa3d43e to your computer and use it in GitHub Desktop.
Collatz conjecture
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
| #!/usr/bin/env python3 | |
| """Collatz conjecture.""" | |
| def collatz(n): | |
| assert n > 0 | |
| while n != 1: | |
| yield n | |
| if n & 1: # odd | |
| n = 3*n + 1 | |
| else: | |
| n >>= 1 | |
| yield 1 | |
| print(*collatz(127)) | |
| print(*collatz(1<<15)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment