Click name ↑ to return to homepage

Collatz conjecture: take any positive integer - no matter what value, if you follow this formula, you will always end up at number 1.

Formula: n. If even - halve it. If odd, x3 then add 1. Repeat.

def collatz(number):

```
if number == 0:
try:
print('enter a number:')
number=int(input())
print("your number is " + str(number))
except ValueError:
print("enter an integer")
if number == 1:
print("the number is now 1")
if number > 1:
#if number is even
if number%2 == 0:
print("this is an even number")
#print number//2
number = number//2
print("divided by two, is " + str(number))
#re-run on this new number
collatz(int(number))
else:
print("this is an odd number, but is not 1")
number = ((number*3)+1)
print("times 3, plus one, is " + str(number))
#re-run on this new number
collatz(int(number))
```