Collatz steps
Inputs
n is a whole number from 1 to 500.
Your tasks
- Create a number
stepsthat starts at 0 and a numbercurrentset ton. - Use a
whileloop that runs whilecurrentis not 1. Ifcurrentis even, halve it. If it is odd, replace it with3 * current + 1. Add 1 tostepseach round. - Complete
collatzStepsso it returnssteps. Return it, do not print it.
Example
With n = 6, collatzSteps returns 8.
Rules
- If
nis already 1, return 0.
Test data
TEST DATA 1: n is 6
TEST DATA 2: n is 1
Practice focus
While loops and early exits, for when you don't know how many rounds you need. Binary search, the Euclidean algorithm and graph search are all while loops with a clear stopping rule.