#K70202. Collatz Sequence Length
Collatz Sequence Length
Collatz Sequence Length
Given a positive integer \(N\), compute the length of the Collatz sequence starting from \(N\) and ending at 1. The sequence is defined as follows:
\(a_0 = N\) and for \(n \ge 0\),
- If \(a_n\) is even, then \(a_{n+1} = \frac{a_n}{2}\).
- If \(a_n\) is odd and \(a_n \neq 1\), then \(a_{n+1} = 3a_n + 1\).
Your task is to output the length of this sequence including the number 1.
For example, if \(N = 6\), the sequence is: 6, 3, 10, 5, 16, 8, 4, 2, 1, so the answer is 9.
inputFormat
The input consists of a single positive integer \(N\) read from standard input.
outputFormat
Output a single integer representing the length of the Collatz sequence starting from \(N\) and ending at 1, printed to standard output.
## sample6
9