Created
April 8, 2018 00:40
-
-
Save antonio-catalano/deecdff452aae4d399b27a484e0b8461 to your computer and use it in GitHub Desktop.
It will print the collatz sequence given an initial positive integer 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(number): | |
if number % 2 == 0: | |
print(number // 2) | |
return (number // 2) | |
else: | |
print(3 * number + 1) | |
return (3 * number + 1) | |
while True: | |
intero= input('Write a positive integer or write "done" if you want to exit: ') | |
if intero == 'done': | |
break | |
else: | |
try: | |
intero = int(intero) | |
if intero > 0: | |
k = collatz(intero) | |
while True: | |
if k == 1: | |
break | |
else: | |
k = collatz(k) | |
else: | |
print('Collatz sequence takes only positive integers') | |
except: | |
print('Wrong input, write a positive integer') | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment