Start learning free

LESSON 221 OF 293 · WHILE LOOPS - CHALLENGES

Halving Steps in Rust: Counting Divisions by 2

Practice Read the full explanation

Given a positive integer n, return how many times you have to divide it by 2 until it becomes 1.

Every division is an integer division: the remainder is dropped, so 9 divided by 2 gives 4. Keep dividing the new value by 2 until it is 1, and count the divisions. If n is already 1, no division is needed.

 

Example 1:

Input: n = 8
Output: 3
Explanation: 8 → 4 → 2 → 1: three divisions by 2.

Example 2:

Input: n = 9
Output: 3
Explanation: 9 → 4 → 2 → 1. 9 divided by 2 gives 4, because the remainder 1 is dropped.

Example 3:

Input: n = 1
Output: 0
Explanation: n is already 1, so there is nothing to divide.

 

Constraints:

  • 1 <= n <= 109
STEP 1 OF 6
    Ctrl + Enter to check

    RUST · WHILE LOOPS - CHALLENGES

    Halving Steps in Rust, explained

    Problem overview

    The function halving_steps takes a positive n, at most 1_000_000_000, and returns how many times it must be divided by 2 to reach 1. Every division is an integer division, so the remainder is dropped and 9 divided by 2 is 4.

    For n = 8 the path is 8, 4, 2, 1: three divisions. For n = 9 it is 9, 4, 2, 1: also three. For n = 1 no division is needed, so the answer is 0.

    Approach

    This is a counting loop. While the number is bigger than 1, halve it and add 1 to a counter. When the loop stops, the number is 1, and the counter holds every division made on the way.

    For n = 9, the state after each pass is:

    passnum after the passsteps after the pass
    141
    222
    313

    The loop always ends, because num / 2 is smaller than num for every num above 1. The number of passes is about the base-2 logarithm of n: even the largest input, 1_000_000_000, needs only 29.

    Step by step

    1. Copy n into num with let mut. Only mut variables can change, and parameters aren't.
    2. Create steps at 0. It is mutable and counts the divisions.
    3. Loop while num > 1. For n = 1 the loop doesn't run and the answer stays 0.
    4. Replace num with num / 2. The integer division drops the remainder.
    5. Add 1 to steps with += 1: each pass is one division.
    6. Return steps as the last expression.

    The solution in Rust

    impl Solution {
        fn halving_steps(n: i32) -> i32 {
            let mut num = n;
            let mut steps = 0;
            while num > 1 {
                num = num / 2;
                steps += 1;
            }
            steps
        }
    }
    
    • Integer division. With both sides integers, / truncates toward zero. For the positive numbers here that means dropping the fraction: 9 / 2 is 4. With floating-point numbers, 9.0 / 2.0 would be 4.5 and the loop would behave differently.
    • Inferred type. steps starts as 0 and becomes an i32, because the function returns it.
    • Loop condition. num > 1 needs no parentheses in Rust, and the braces are required.

    A pattern in the answers

    Because each pass halves the number, every value from 8 to 15 needs three passes, and 16 needs four. The answer is the position of the highest set bit of n, which is why it grows so slowly: doubling the input adds one step. The loop doesn't need that insight, but it explains why even a billion needs only 29.

    Rounding down also explains why odd numbers need no extra work. Going from 9 to 4 loses the remainder, and the loop simply carries on from 4.

    Edge cases and common mistakes

    • Already 1. n = 1 gives 0.
    • Not a power of two. 9 and 8 both give 3. Every value from 8 to 15 gives 3, because each one halves down to 1 in the same number of passes.
    • The condition num > 0. Since 1 / 2 is 0, it would take one extra pass after reaching 1, and the answer would be one too high.
    • Dividing steps instead of num. The loop would never end, because num would stay above 1.

    Testing

    • n = 8 gives 3.
    • n = 9 gives 3.
    • n = 1 gives 0.
    • n = 2 gives 1.
    • n = 3 gives 1.
    • n = 1000000000 gives 29.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free