Created
December 6, 2021 02:04
-
-
Save NimaBavari/e3a7a9dae54aa814c99e48881c1767e9 to your computer and use it in GitHub Desktop.
Collatz sequence generator given a starting number
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_gen(start): | |
yield start | |
while start != 1: | |
if start % 2 == 0: | |
start //= 2 | |
else: | |
start = 3 * start + 1 | |
yield start | |
for item in collatz_gen(27): | |
print(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment