#C10233. Magical Sequence Length
Magical Sequence Length
Magical Sequence Length
Given a positive integer \(N\), define a "magical sequence" (often known as the Collatz sequence) as follows:
- If \(N = 1\), stop.
- If \(N\) is even, then the next number is \(\frac{N}{2}\).
- If \(N\) is odd and greater than 1, then the next number is \(3N + 1\).
Your task is to compute the length of this sequence starting from the given \(N\) until you reach 1. Formally, if we denote the sequence as \(a_1 = N, a_2, a_3, \dots, a_k = 1\), you need to determine \(k\).
Note: The recurrence can be written in \(\LaTeX\) as:
\(a_{i+1} = \begin{cases}\frac{a_i}{2} & \text{if } a_i \text{ is even}\\ 3\,a_i + 1 & \text{if } a_i \text{ is odd}\end{cases}\)
inputFormat
The input consists of a single integer \(N\) provided via standard input. \(N\) is a positive integer (e.g., 1 \(\leq N \leq 10^9\) as a guideline).
outputFormat
Output the length of the magical sequence starting from \(N\) as an integer to standard output.
## sample6
9