#C10717. Collatz Sequence Analysis
Collatz Sequence Analysis
Collatz Sequence Analysis
Given a list of positive integers terminated by a zero, compute the length of the Collatz sequence for each number until the terminating zero is encountered. The Collatz sequence for a positive integer \( n \) is defined by the recurrence:
\( n_{i+1} = \begin{cases} \frac{n_i}{2}, & \text{if } n_i \text{ is even} \\ 3n_i+1, & \text{if } n_i \text{ is odd} \end{cases} \)
The sequence stops once the value 1 is reached. The length includes the starting number and the 1 at the end of the sequence.
Your task is to read a sequence of integers from standard input, compute the Collatz sequence length for each integer (until a 0 is encountered), and output the results.
inputFormat
The input consists of one or more integers separated by spaces and/or newlines. The sequence is terminated by the integer 0, which should not be processed.
outputFormat
Output the Collatz sequence lengths for each number (before the terminating zero) on a single line, separated by a space. If no numbers are provided before the terminal 0, output nothing.
## sample1
2
3
5
10
0
1 2 8 6 7