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
. Ifnumber
is even, thencollatz()
should printnumber // 2
and return this value. Ifnumber
is 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)