Halving steps
Inputs
n is a whole number from 0 to 100,000.
Your tasks
- Create a number
stepsthat starts at 0 and a copyrestofn. - Use a
whileloop that runs whilerestis more than 0. Each round, halverest(rounded down) and add 1 tosteps. - Complete
halvingStepsso it returnssteps. Return it, do not print it.
Example
With n = 20, halvingSteps returns 5.
Rules
- If
nis 0, return 0.
Test data
TEST DATA 1: n is 20
TEST DATA 2: n is 0
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.