All tasks
Mediumloopsmath

Collatz Steps

Time limit 2 s · Memory 64 MBAcceptance: —Solved by: 0

Statement

You are given an integer n. At every step: if n is even, divide it by 2; if odd, replace it with 3·n + 1. Print how many steps it takes to reach n = 1. For n = 1 the answer is 0.

Examples

Example 1
Input
6
Output
8
Example 2
Input
1
Output
0

Constraints

  • 1 ≤ n ≤ 10^6
Solve this task to unlock the editorial.

AI tutor

Stuck? Get a nudge — the tutor explains the idea, it never hands you the solution.