CBOJ Fall Challenge '24 P2 - Collatz Conjecture
The Collatz Conjecture is a sequence defined as follows for a positive integer \(N\):
- If \(N\) is even, divide it by \(2\)
- If \(N\) is odd, multiply it by \(3\) and add \(1\).
- Repeat the process with the new value of \(N\), until \(N\) becomes \(1\).
How many steps are required to reach \(1\) from \(N\)?
Constraints
\(1 \le N \le 10^5\)
Input Specification
The first line of input contains one integer, \(N\).
Output Specification
Output the number of steps needed to reach \(1\).
Sample Input
6
Sample Output
8
Explanation
Starting from 6, we perform the following steps:
- \(6\) is even → \(6 / 2 = 3\)
- \(3\) is odd → \(3 \times 3 + 1 = 10\)
- \(10\) is even → \(10 / 2 = 5\)
- \(5\) is odd → \(5 \times 3 + 1 = 16\)
- \(16\) is even → \(16 / 2 = 8\)
- \(8\) is even → \(8 / 2 = 4\)
- \(4\) is even → \(4 / 2 = 2\)
- \(2\) is even → \(2 / 2 = 1\)
Comments