The Collatz-Conjecture
The Problem¶
This is a famous and not yet fully solved problem in mathmatics. Here are the rules:
- Start with any positive integer number x
- If x is even, assign x ← x/2
- If x is odd, assign x ← 3x + 1
Eventually your sequence will end up in the loop 4 → 2 → 1 → 4 … (The unsolved part is that there is no proof yet that this always happens.)
Now you!¶
Implement a loop that generates and prints the sequence for any given positive starting number. You can either set the starting value in a variable at the beginning of your program or read it from a user input. The loop should stop if x reaches one of the three numbers (4, 2, or 1) that are known to form an endless cycle.
Example Output:
Hint
To check if a number is even, the modulo-operator (written as %
in Python) might be very helpful.
It calculates the remainder of a whole-number division.
For example 13 // 3 == 4
and since 4 * 3 == 12
that leaves a reminder of 1
to get to the original 13
.
Therefore, 13 % 3 == 1
.