The Collatz Conjecture

MediumAcc. 87.4%
+30 XP 12

The 3n + 1 Problem

Pick a number. If it's even, divide by 2. If it's odd, multiply by 3 and add 1. Repeat. Mathematicians believe every number eventually reaches 1, no matter how high it climbs!

The Assignment

Your function receives a parameter named start.

  1. Initialize a variable steps at 0.
  2. Write a while loop that runs as long as start is not 1.
  3. In each step:
    • If start is even, set start = start / 2.
    • If start is odd, set start = (start * 3) + 1.
    • Increment your steps tracker.
  4. After the loop, print the total number of steps.

01EXAMPLE 1

Inputstart = 6
Output8

Explanation: 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 (8 steps).

Constraints

  • Use a while loop.
  • Ensure the loop stops perfectly at 1.
MathLoops
JavaScript
Loading...
3 Hidden

Input Arguments

start6

Expected Output

8

Click RUN to test your solution