Skip to content

Instantly share code, notes, and snippets.

@zed
Created December 20, 2016 20:37
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 zed/1b147fdeb9d66a599f3b60925fa3d43e to your computer and use it in GitHub Desktop.
Save zed/1b147fdeb9d66a599f3b60925fa3d43e to your computer and use it in GitHub Desktop.
Collatz conjecture
#!/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