Collatz Conjecture

Step 1.

Define a function

def h(n: int) -> int:
    if n % 2 == 0:
        return n // 2
    else:
        return 3 * n + 1

Step 2.

Test the function.

h(10) == 5
True
h(5) == 16
True

Step 3.

Iterate a few times.

h(5)
16
h(16)
8
h(Out[5])
4
h(Out[6])
2

Step 4.

Define an iteration function that prints a result.

def iterate_from(n: int) -> None:
    print(n)
    while n != 1:
        n = h(n)
        print(n)
iterate_from(16)
16
8
4
2
1
iterate_from(15)
15
46
23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1