Collatz Conjecture
Consider the following operation on an arbitrary positive integer:
- If the number is even, divide it by two.
- If the number is odd, triple it and add one.
Keep applying the same operation on the result. The Collatz conjecture is: This process will eventually reach the number 1, regardless of which positive integer is chosen initially. (A nice video with more details: https://www.youtube.com/watch?v=094y1Z2wpJg)
Now, the task is to print the Collatz sequence for a given number n
.
Example: For n = 12
and the sequence will be 6, 3, 10, 5, 16, 8, 4, 2, 1.
Hint 1:
Since the number of steps is not known upfront, thewhile
loop is better suited for the task.