FunctionRacerProblemsRace

Collatz Steps

BeginnerMathPython, JavaScript, TypeScript, Java, C++

Start with n. If the current number is even halve it, otherwise multiply it by 3 and add 1. Return how many steps it takes to reach 1 (0 if n is already 1).

Example 1

n 6
returns 8

6, 3, 10, 5, 16, 8, 4, 2, 1: eight steps.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Math problems