#C7492. Collatz Sequence Length

    ID: 51369 Type: Default 1000ms 256MiB

Collatz Sequence Length

Collatz Sequence Length

Given a positive integer \( n \), repeatedly apply the following operations until \( n \) becomes 1:

  • If \( n \) is even, divide it by 2.
  • If \( n \) is odd, multiply it by 3 and add 1.

The task is to compute the length of the sequence, i.e. the total number of numbers in the sequence starting from \( n \) and ending with 1. This process is known as the Collatz sequence (or the 3n+1 problem).

Example: For \( n=6 \), the sequence is 6, 3, 10, 5, 16, 8, 4, 2, 1, so the answer is 9.

inputFormat

The input consists of a single integer ( n ) (where ( 1 \le n \le 10^6 )). Input is given via standard input (stdin).

outputFormat

Output a single integer representing the length of the Collatz sequence for the given integer ( n ). Output should be printed to standard output (stdout).## sample

6
9