Skip to content

Instantly share code, notes, and snippets.

@antonio-catalano
Created April 8, 2018 00:40
Show Gist options
  • Save antonio-catalano/deecdff452aae4d399b27a484e0b8461 to your computer and use it in GitHub Desktop.
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
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