#K13616. Modified Collatz Sequence
Modified Collatz Sequence
Modified Collatz Sequence
Given a positive integer \(n\), you are required to generate a modified Collatz sequence. The process is as follows:
- Start with \(n\).
- Apply the following rule repeatedly until you reach 1:
- If \(n\) is even, then update \(n\) to \(\frac{n}{2}\).
- If \(n\) is odd, then update \(n\) to \(3n+1\).
- Once 1 is reached, append the reverse of all the numbers in the sequence except the last element (i.e. the 1) to the sequence.
For example, when \(n = 5\), the sequence computed in the forward process is: [5, 16, 8, 4, 2, 1]. After appending the reverse (excluding 1), the final sequence is: [5, 16, 8, 4, 2, 1, 2, 4, 8, 16, 5].
Your task is to implement this sequence generation. The input will be read from standard input (stdin) as a single integer, and the output (the complete sequence) should be printed to standard output (stdout) with all numbers separated by a space.
inputFormat
The input consists of a single integer \(n\) (\(n \ge 1\)) provided through standard input (stdin).
outputFormat
Output the modified Collatz sequence as a series of integers separated by a single space. The sequence should be printed to standard output (stdout).
## sample1
1