CBOJ Fall Challenge '24 P2 - Collatz Conjecture


Submit solution

Points: 5
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

The Collatz Conjecture is a sequence defined as follows for a positive integer \(N\):

  1. If \(N\) is even, divide it by \(2\)
  2. If \(N\) is odd, multiply it by \(3\) and add \(1\).
  3. Repeat the process with the new value of \(N\), until \(N\) becomes \(1\).

How many steps are required to reach \(1\) from \(N\)?

Constraints

\(1 \le N \le 10^5\)

Input Specification

The first line of input contains one integer, \(N\).

Output Specification

Output the number of steps needed to reach \(1\).

Sample Input

6

Sample Output

8

Explanation

Starting from 6, we perform the following steps:

  1. \(6\) is even → \(6 / 2 = 3\)
  2. \(3\) is odd → \(3 \times 3 + 1 = 10\)
  3. \(10\) is even → \(10 / 2 = 5\)
  4. \(5\) is odd → \(5 \times 3 + 1 = 16\)
  5. \(16\) is even → \(16 / 2 = 8\)
  6. \(8\) is even → \(8 / 2 = 4\)
  7. \(4\) is even → \(4 / 2 = 2\)
  8. \(2\) is even → \(2 / 2 = 1\)

Comments

There are no comments at the moment.