Problem list
Log in
#r03109 · Online Judge > GoldOJ

Collatz Sequence

Bronze III 2.0 s 256 MB

Given a natural number $n$, repeatedly apply: if $n$ is odd replace it with $3n+1$, if even replace it with $n/2$, until it reaches $1$. Print every value in order, including the starting number and the final $1$, one per line.

Input

One integer $n$ ($1 \le n \le 10^7$).

Output

Print the Collatz sequence, one number per line.

Example 1

Input
5
Output
5
16
8
4
2
1
Source
Online Judge > GoldOJ
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code