Start learning free

LESSON 222 OF 293 · WHILE LOOPS - CHALLENGES

Sum Until Limit in Rust: A while Loop with Two Counters

Practice Read the full explanation

Given a positive integer limit, add up the numbers 1, 2, 3, … in this order, and stop as soon as the total is at least limit. Return how many numbers you added.

The total may go past limit: the number that makes it reach or pass limit is the last one added, and it counts.

 

Example 1:

Input: limit = 10
Output: 4
Explanation: 1 + 2 + 3 + 4 = 10 reaches the limit exactly, so 4 numbers were added.

Example 2:

Input: limit = 11
Output: 5
Explanation: 1 + 2 + 3 + 4 = 10 is still less than 11, so 5 is added too. The total becomes 15, and 5 numbers were added.

Example 3:

Input: limit = 1
Output: 1
Explanation: The first number, 1, already reaches the limit.

 

Constraints:

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

    RUST · WHILE LOOPS - CHALLENGES

    Sum Until Limit in Rust, explained

    Problem overview

    The function sum_until_limit takes a positive limit, at most 1_000_000_000. It adds the numbers 1, 2, 3, and so on in order, and stops as soon as the total is at least limit. It returns how many numbers it added. The total may overshoot the limit, and the number that crosses the line counts.

    For limit = 10 the sum 1 + 2 + 3 + 4 is exactly 10, so the answer is 4. For limit = 11 that sum is still short, so 5 is added too, the total becomes 15, and the answer is 5.

    Approach

    You need two numbers at once: the running total, and how many numbers were added. The second one is also the last number added, because the numbers go 1, 2, 3, and so on. So count doubles as the next value to add.

    The loop runs while total < limit. In each pass, increase count and add it to total. For limit = 11 the state after each pass is:

    passcounttotal
    111
    223
    336
    4410
    5515

    After pass 4 the total is 10, still below 11, so the loop continues. After pass 5 it is 15, and the loop ends with count equal to 5. With limit = 10 the loop stops after pass 4, because 10 < 10 is false.

    Step by step

    1. Create total at 0. It is mutable and holds the sum so far.
    2. Create count at 0. It is mutable too, and counts the numbers added.
    3. Loop while total < limit. The loop stops exactly when the total reaches or passes the limit.
    4. Add 1 to count first. It is now the next number to add.
    5. Add count to total. The order matters: the increase comes first, so the first number added is 1, not 0.
    6. Return count as the last expression.

    The solution in Rust

    impl Solution {
        fn sum_until_limit(limit: i32) -> i32 {
            let mut total = 0;
            let mut count = 0;
            while total < limit {
                count += 1;
                total += count;
            }
            count
        }
    }
    
    • Inferred types. Both variables start as 0. Comparing total with limit makes it an i32, and returning count does the same for it.
    • Compound assignment. count += 1 and total += count change the variables in place. Rust has no ++.
    • Overflow. The total ends below limit plus the last number added. For limit = 1_000_000_000 the answer is 44721 and the final total is about a billion, well under i32::MAX. Near i32::MAX the total could pass it, and in an optimized build it would wrap around, which is why the lesson's limit stops at 1_000_000_000.

    How many passes

    The total after k numbers grows roughly like k squared over 2, so the loop runs about the square root of 2 * limit times. That is 44721 passes for a limit of a billion, which is quick. Counting up one number at a time is a good fit here because the stopping point depends on the running total, not on a fixed number of repeats.

    Edge cases and common mistakes

    • Smallest limit. limit = 1 gives 1: the first number already reaches it.
    • Exact hit against overshoot. limit = 10 stops at a total of exactly 10, and limit = 11 overshoots to 15. Both counts include the last number added.
    • Using <=. With total <= limit the loop would add one more number after reaching the limit exactly, and limit = 10 would give 5.
    • Adding before incrementing. If total += count comes before count += 1, the first number added is 0 and the answer is off by one in the sum.

    Testing

    • limit = 10 gives 4.
    • limit = 11 gives 5.
    • limit = 1 gives 1.
    • limit = 2 gives 2.
    • limit = 3 gives 2.
    • limit = 1000000000 gives 44721.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free