Skip to content

Instantly share code, notes, and snippets.

@ItsCrem
Created November 16, 2017 14:11
Show Gist options
  • Select an option

  • Save ItsCrem/fa844ae196c366175446c30f1d00b6aa to your computer and use it in GitHub Desktop.

Select an option

Save ItsCrem/fa844ae196c366175446c30f1d00b6aa to your computer and use it in GitHub Desktop.
CollatzSequence.py created by MaxCaminer - https://repl.it/@MaxCaminer/CollatzSequencepy
#Collatz Sequence
def collatz(number):
#If number is even then divide by 2 and then return number
if number % 2 == 0:
print(number // 2)
return number // 2
#If number is odd then times the number by 3 and add 1 then return number
elif number % 2 == 1:
result = 3 * number + 1
print(result)
return result
try:
n = input("Give me a number: ")
while n != 1:
n = collatz(int(n))
#Catching if input given is not an integer
except ValueError:
print('You must enter an integer. Please try again!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment