Start learning free

LESSON 220 OF 292 · WHILE LOOPS - CHALLENGES

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

    SWIFT · WHILE LOOPS - CHALLENGES

    Sum Until Limit in Swift, explained

    Problem overview

    The function sumUntilLimit receives a positive integer limit. It adds the numbers 1, 2, 3 and so on, in that order, and stops as soon as the total is at least limit. It returns how many numbers it added. The last number counts even if it pushes the total past the limit.

    For limit = 10, the sum 1 + 2 + 3 + 4 reaches exactly 10, so the answer is 4. For limit = 11, the total after four numbers is 10, which is still below 11, so 5 is added as well. The total becomes 15 and the answer is 5. For limit = 1, the first number already reaches the limit, so the answer is 1.

    Approach

    Keep two variables: total, the sum so far, and count, how many numbers have been added. Because the numbers go 1, 2, 3 in order, count is also the last number added. So each turn does two things: move count to the next number, then add it to total.

    The loop runs while total < limit. As soon as total reaches or passes limit, the condition is false and the loop stops, which is exactly when the adding must stop. Here is the run for limit = 11:

    counttotal
    11
    23
    36
    410
    515

    After the fifth row, 15 is not less than 11, so the loop ends and the function returns 5.

    Step by step

    1. Create var total = 0. It is a var because it grows inside the loop.
    2. Create var count = 0. No number has been added yet.
    3. Loop while total < limit.
    4. Add 1 to count with += 1. It is now the next number to add: 1 on the first pass, 2 on the second.
    5. Add count to total. The order matters: count goes up first, so the number added is the right one.
    6. Return count after the loop.

    The solution in Swift

    class Solution {
        func sumUntilLimit(_ limit: Int) -> Int {
            var total = 0
            var count = 0
            while total < limit {
                count += 1
                total += count
            }
            return count
        }
    }
    

    Swift details worth knowing:

    • No ++. Swift has no increment operator, so counters grow with += 1.
    • Parameters are constants. The loop never changes limit, so no copy is needed here. The two variables hold the state that changes.
    • The limit is read each time. while total < limit is checked before every pass, including the first. With a positive limit, the first check is always true, because total starts at 0.
    • Size. Int is 64 bits, and the total never goes much past the limit, so there is no overflow.

    Edge cases and common mistakes

    • Reaching the limit exactly. limit = 10 stops when total is 10. The loop condition uses <, so a total equal to the limit ends the loop. Using <= would add one more number and return 5.
    • Small limits. limit = 1 gives 1, limit = 2 gives 2 (1 + 2 = 3) and limit = 3 gives 2 (1 + 2 = 3).
    • Adding before incrementing. If total += count came first, the first pass would add 0, and the numbers added would trail one behind.
    • Returning total. The question asks how many numbers were added, not their sum.
    • Large limits. limit = 1000000000 needs 44721 passes, which is quick.

    Testing

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