I am trying to create a python script for the collatz conjecture (if a number's odd, times it by three and add 1, and if it's even, divide by two). I can't figure out how to successfully increment a variable that shows how many iterations of the function and I think it has something to do with it being recursive. Here's what I've got so far:
def collatz(n):
list_terminate = [0, 1, 2, 4]
if n in list_terminate:
return n
else:
if n % 2 == 0:
print(n)
iterations += 1
return collatz(n // 2)
if n % 2 == 1:
iterations += 1
print(n)
return collatz((n * 3) + 1)