Problems > The Weird Algorithm

The Weird Algorithm

Difficulty: 900 2000 ms 128 MB

math implementation

Print the whole Collatz trajectory of N (even → halve, odd → 3N+1) until it reaches 1, inclusive.

Input: one integer N.

Output: the full sequence on one line.

Constraints:

  • 1 ≤ N ≤ 106

Sample Tests

Input 1
3
Output 1
3 10 5 16 8 4 2 1
Input 2
1
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.