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:
| pass | count | total |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
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
- Create
totalat 0. It is mutable and holds the sum so far. - Create
countat 0. It is mutable too, and counts the numbers added. - Loop while
total < limit. The loop stops exactly when the total reaches or passes the limit. - Add 1 to
countfirst. It is now the next number to add. - Add
counttototal. The order matters: the increase comes first, so the first number added is 1, not 0. - Return
countas 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. Comparingtotalwithlimitmakes it ani32, and returningcountdoes the same for it. - Compound assignment.
count += 1andtotal += countchange the variables in place. Rust has no++. - Overflow. The total ends below
limitplus the last number added. Forlimit = 1_000_000_000the answer is 44721 and the final total is about a billion, well underi32::MAX. Neari32::MAXthe total could pass it, and in an optimized build it would wrap around, which is why the lesson's limit stops at1_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 = 1gives 1: the first number already reaches it. - Exact hit against overshoot.
limit = 10stops at a total of exactly 10, andlimit = 11overshoots to 15. Both counts include the last number added. - Using
<=. Withtotal <= limitthe loop would add one more number after reaching the limit exactly, andlimit = 10would give 5. - Adding before incrementing. If
total += countcomes beforecount += 1, the first number added is 0 and the answer is off by one in the sum.
Testing
limit = 10gives 4.limit = 11gives 5.limit = 1gives 1.limit = 2gives 2.limit = 3gives 2.limit = 1000000000gives 44721.
