52. The 3n + 1 Game

Acceptance — 0 accepted 0 submissions

Easy
LoopsMath
No companies listed
Here is a famous puzzle that mathematicians still haven't fully solved:
• If n is even, divide it by 2.
• If n is odd, make it 3n + 1.
Repeat until n becomes 1.

Given n, count how many steps it takes to reach 1.

Input Format
A single integer n.

Output Format
Print the number of steps.

Constraints
• 1 ≤ n ≤ 10^6
• Values in between can exceed 2^31, so use long long or long.

Example 1

Input
6
Output
8

Explanation 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1: 8 steps.

Example 2

Input
1
Output
0

Explanation Already 1, so 0 steps.

Example 3

Input
16
Output
4

Explanation 16 → 8 → 4 → 2 → 1: 4 steps.

Acceptance —
Accepted 0
Submissions 0
Auto-save
Saved Ln 1, Col 1
Submit a solution to see its status here.
Input
Expected Output
8
No results yet
Run your code to test it against the sample cases.
Problem List