less than 1 minute read

Given a positive integer n, find the length of its Collatz sequence. The Collatz sequence is generated sequentially where

  • n = n / 2 if n is even
  • n = 3 * n + 1 if n is odd
  • And the sequence ends if n = 1

https://binarysearch.com/problems/Collatz-Sequence

Examples

Example 1

Input

  • n = 11

Output

  • answer = 15

Explanation

The Collatz sequence is: [11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] and its length is 15.

Solution

Categories:

Updated:

Leave a comment