Skip to content

Instantly share code, notes, and snippets.

@Neemox
Forked from hovissimo/collatz.py
Last active April 29, 2016 22:24
Show Gist options
  • Save Neemox/4122615b6e38c83f4818079fc2c97f83 to your computer and use it in GitHub Desktop.
Save Neemox/4122615b6e38c83f4818079fc2c97f83 to your computer and use it in GitHub Desktop.
def get_number()
while True: #(!num) through a big error
try:
print('Please enter a number for the Collatz sequence:')
num = int(input()) # Prompt for a number
return num
break
except ValueError: # If we couldn't make it an int...
print("enter an integer") # ... complain
def collatz(n):
# return next number after n in the collatz sequence
if n % 2 == 0:
n = n // 2
print(n)
return n
else:
n = n * 3 + 1
print(n)
return n
def main():
number = get_number()
while number != 1:
number = collatz(number)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment