I wrote the following code for the Collatz exercise: The Collatz Sequence
Any feedback is appreciated.
Write a function named
collatz()that has one parameter namednumber. Ifnumberis even, thencollatz()should printnumber // 2and return this value. Ifnumberis odd, thencollatz()should print and return3 * number + 1.
def collatz(n):
while n > 1:
if n % 2 == 0:
n = int(n // 2)
print (n)
elif n % 2 == 1:
n = int(3 * n + 1)
print (n)
n = int(input("Enter a number: "))
collatz (n)