The Collatz Conjecture is an unsolved problem in Mathematics which lends itself nicely to exploration with Python.

The idea is simple:

Pick a positive whole number. If itβs odd, multiply it by 3 and add 1. If itβs even, divide it by 2. Now you have a new number. Apply the same rules to the new number. What happens as you keep repeating the process?

Try a few examples manually, and then have a go at completing the program below to explore the conjecture with Python.

Details: Complete the function named `collatz()`

. It has one parameter named `number`

. It should return `number // 2`

if `number is even`

and `3 * number + 1`

otherwise. Hint: You can use the modulo operator to check for evenness.

```
def collatz(number):
pass
n = int(input("Enter a positive whole number: "))
print(n)
while n != 1:
n = collatz(n)
print(n)
```

Once you’ve had a good try, you may want to look at my solution:

You might think that the number you start with affects the number you end up with. Perhaps some numbers eventually reach 1, while others go off to infinity. Collatz conjectured that if you start with a positive whole number and run this process long enough, all starting values will lead to 1.

Over the years, many mathematicians have been drawn Collatz conjecture. They have tested billions of examples without finding a single exception to Collatzβs prediction. However, it is not yet proven that ALL positive whole numbers will eventually reach `1`

.

## The Collatz Conjecture with a Negative Starting Number

You may be curious as to what happens if we apply the Collatz formulas to a negative initial number. Have a play with this idea now if it interests you. You can easily use the same program as above to explore this – maybe changing the input message to encourage negative input…

Generally speaking, if we apply the Collatz function on any integer, then it is conjectured that it ends up in one of five “cycles”:

`β¦β0β0β0ββ¦`

`β¦β1β4β2β1ββ¦`

`β¦ββ1ββ2ββ1ββ¦`

`β¦ββ5ββ14ββ7ββ20ββ10ββ5ββ¦`

–`β¦ββ17ββ50ββ25ββ74ββ37ββ110ββ55ββ164ββ82ββ41ββ122ββ61ββ182ββ91ββ272ββ136ββ68ββ34ββ17ββ¦`

This article has explored the Collatz Conjecture with Python. I hope you found it interesting. Happy computing!