Start learning free

LESSON 219 OF 291 · WHILE LOOPS - CHALLENGES

Sum Until Limit in Scala: 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

    SCALA · WHILE LOOPS - CHALLENGES

    Sum Until Limit in Scala, explained

    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

    1. Create var total, starting at 0. It holds the sum so far.
    2. Create var count, starting at 0. It counts the numbers added so far.
    3. Write a while loop with the condition total < limit. The loop ends as soon as the total is at least the limit.
    4. Inside the loop, add 1 to count with count += 1. It is now the next number to add.
    5. Still inside, add count to total.
    6. After the loop, return count.

    For limit = 11:

    Turncounttotal
    111
    223
    336
    4410
    5515

    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. A val would be a compile error on the first +=.
    • The order of the two lines inside the loop matters. count goes up first, so the number added to total is 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 use count += 1.
    • Int overflow wraps silently, but it doesn't happen here: for limit = 1000000000 the 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 <= limit would 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
    
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free