#C10168. Collatz Sequence Steps
Collatz Sequence Steps
Collatz Sequence Steps
Given a positive integer \(X\), determine the number of steps required to reach 1 by repeatedly applying the following rules:
- If \(X\) is even, then update \(X\) to \(\frac{X}{2}\).
- If \(X\) is odd, then update \(X\) to \(3X+1\).
This process is widely recognized in mathematics as the Collatz Conjecture or the 3n+1 problem. For example, when \(X=3\), the process is: 3 \(\to\) 10 \(\to\) 5 \(\to\) 16 \(\to\) 8 \(\to\) 4 \(\to\) 2 \(\to\) 1, which takes 7 steps.
inputFormat
The input is read from stdin and has the following format:
T X1 X2 ... XT
Where:
T
is an integer denoting the number of test cases.- Each \(X_i\) is a positive integer representing the starting value for the \(i\)th test case.
outputFormat
For each test case, print a single line on stdout representing the number of steps required to reduce \(X\) to 1 by applying the rules:
- If \(X\) is even, then \(X \to \frac{X}{2}\).
- If \(X\) is odd, then \(X \to 3X+1\).
3
3
6
19
7
8
20
</p>