From Wikipedia:
Take any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process (which has been called “Half Or Triple Plus One”, or HOTPO[6]) indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
def collatz(n,itmax=10000):
it = 0
while not n==1 and it<itmax:
it += 1
if n % 2 == 0:
n = n//2
else:
n = 3*n+1
print(n)
print(str(it)+" steps")
collatz(4)
collatz(10)
## 2
## 1
## 2 steps
## 5
## 16
## 8
## 4
## 2
## 1
## 6 steps
Some questions to think about:
But be careful!