#C13786. Collatz Sequence Generator

    ID: 43362 Type: Default 1000ms 256MiB

Collatz Sequence Generator

Collatz Sequence Generator

In this problem, you are asked to generate the Collatz sequence for a given starting integer. The sequence is defined by the following recurrence:

For a given integer ( n ):

  • If ( n ) is even, the next number is ( \frac{n}{2} ).
  • If ( n ) is odd, the next number is ( 3n+1 ).

The process terminates when the sequence reaches 1 for the first time. For example, if the input is 6, the generated sequence is:

6, 3, 10, 5, 16, 8, 4, 2, 1

Your task is to read a single non-negative integer from standard input, generate the corresponding Collatz sequence, and print all numbers in the sequence separated by a space to standard output.

inputFormat

A single line containing one non-negative integer ( n ) (with ( n \ge 1 )).

outputFormat

A single line containing the generated sequence. The numbers should be output separated by a single space. There should be no extra spaces at the beginning or end of the line.## sample

6
6 3 10 5 16 8 4 2 1

</p>