Problem overview
The function sumUntilLimit takes a positive Int, limit. You add the numbers 1, 2, 3 and so on, in that order, and stop as soon as the total is at least limit. The function returns how many numbers you added.
For limit = 10, the total goes 1, 3, 6, 10. It reaches 10 exactly after four numbers, so the answer is 4. For limit = 11 the total after four numbers is 10, still below 11, so 5 is added too and the total becomes 15: the answer is 5. The number that reaches or passes the limit counts. For limit = 1 the answer is 1.
Approach
You don't know in advance how many numbers it will take, which is the situation a while loop is made for. It repeats while a condition holds. Here the condition is "the total is still below the limit".
Two variables carry the state. total is the sum so far. count is how many numbers have been added, and because they are added in the order 1, 2, 3, it is also the last number added. That is why the loop only needs count: the next number to add is always count + 1.
Step by step
- Create
var total, starting at 0. It holds the sum so far. - Create
var count, starting at 0. It counts the numbers added so far. - Write a
whileloop with the conditiontotal < limit. The loop ends as soon as the total is at least the limit. - Inside the loop, add 1 to
countwithcount += 1. It is now the next number to add. - Still inside, add
counttototal. - After the loop, return
count.
For limit = 11:
| Turn | count | total |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
After turn 4 the total is 10, which is below 11, so the loop runs again. After turn 5 it is 15, the condition is false, and the loop ends with count equal to 5.
The solution in Scala
class Solution {
def sumUntilLimit(limit: Int): Int = {
var total = 0
var count = 0
while (total < limit) {
count += 1
total += count
}
return count
}
}
Details that matter in Scala:
- Both variables change inside the loop, so both are
var. Avalwould be a compile error on the first+=. - The order of the two lines inside the loop matters.
countgoes up first, so the number added tototalis the new one. Reversed, the loop would add 0 first, then 1, and the answer would be off by one. - Scala has no
++for numbers, so usecount += 1. Intoverflow wraps silently, but it doesn't happen here: forlimit = 1000000000the function returns 44721, and the total stays far below the 32-bit limit, because the last number added is small compared with the limit.
Edge cases and common mistakes
- The total passes the limit. It doesn't need to hit the limit exactly. With 11 the total ends at 15 and the last number still counts.
- Smallest limit. With
limit = 1, the first turn gives a total of 1, and the answer is 1. - Wrong comparison.
total <= limitwould add one number too many whenever the total hits the limit exactly: for 10 it would return 5. - Returning the total. The task asks how many numbers were added, so return
count.
Testing
sumUntilLimit(10) -> 4
sumUntilLimit(11) -> 5
sumUntilLimit(1) -> 1
sumUntilLimit(1000000000) -> 44721
