Back
Close

The Collatz Conjecture

Statement

 Goal

The Collatz Conjecture, also infamously known as the 3N+1 conjecture, is renowned to be the most dangerous problem in mathematics.

The statement is simple: Given a positive integer, you will have to apply a set of rules to it to form a sequence.

If the number is even, divide it by 2.
If the number is odd, multiply it by 3 and add 1 to it.

If we look into the problem a bit deeper, we realize that if the sequence reaches numbers 4, 2, or 1, it is sent into an infinite loop. 4 / 2 = 2, 2 / 2 = 1, one is odd, so 1*3 + 1 = 4, and the loop continues.

The conjecture is: Any positive integer which creates a sequence like this will end in the 4→2→1→4 loop. The conjecture currently does not have a valid solution.

Given a positive integer, output the number of numbers in the sequence starting with the number before it reaches 1, starting the loop.
Input
Single Integer n.
Output
Number of numbers in the sequence before it reaches 1. The sequence starts with the positive integer n.

If the number is negative or 0, just output 1.
Constraints
n < 2147483648
Example
Input
10
Output
7

Game modes
Fastest, Shortest

Test cases
Regular Integer Test
Input
10
Output
7

Validator 1 Validator
Input
15
Output
18

Prime Numbers? Test
Input
23
Output
16

Validator 2 Validator
Input
57
Output
33

Big Number Test
Input
165
Output
112

Validator 3 Validator
Input
203
Output
40

Bigger Number Test
Input
1354
Output
53

Validator 4 Validator
Input
5436
Output
68

Titanic Number Test
Input
63728127
Output
950

Validator 5 Validator
Input
2147483646
Output
451

Negitive Numbers Test
Input
-13
Output
1

Validator 6 Validator
Input
-34720
Output
1

Zero for N Test
Input
0
Output
1

Validator 7 Validator
Input
30
Output
19

Solution language

Solution

Stub generator input