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.
- Initialize a variable
stepsat 0. - Write a
whileloop that runs as long asstartis not 1. - In each step:
- If
startis even, setstart = start / 2. - If
startis odd, setstart = (start * 3) + 1. - Increment your
stepstracker.
- If
- After the loop, print the total number of
steps.
01EXAMPLE 1
Input
start = 6Output
8Explanation: 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 (8 steps).
Constraints
- Use a while loop.
- Ensure the loop stops perfectly at 1.
MathLoops
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
start6
Expected Output
8
Click RUN to test your solution