Start learning free

LESSON 219 OF 285 · WHILE LOOPS - CHALLENGES

Sum Until Limit in Go: A Loop That Stops at a Total

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

    GO · WHILE LOOPS - CHALLENGES

    Sum Until Limit in Go, explained

    Problem overview

    The function sumUntilLimit receives a positive integer limit. Add the numbers 1, 2, 3, and so on, in this order, and stop as soon as the total is at least limit. Return how many numbers you added.

    The total may overshoot the limit: the number that makes it reach or pass limit is the last one added, and it counts. With limit = 10, 1 + 2 + 3 + 4 is exactly 10, so the answer is 4. With limit = 11, the total after 4 numbers is 10, still below 11, so 5 is added too, the total becomes 15, and the answer is 5. With limit = 1, the first number already reaches the limit, so the answer is 1.

    Approach

    You can't know in advance how many numbers it will take, so the repeat is driven by a condition: keep adding while the total is below the limit. Go has no while; a for loop with only a condition does the same thing: for total < limit.

    Two variables carry the state. total is the sum so far, and count is how many numbers have been added. Because the numbers go 1, 2, 3 in order, count is also the last number added: after adding 1, 2 and 3, it is 3. So the next number to add is always count + 1, and the loop can use count as both the counter and the number.

    For limit = 11 the values after each pass are:

    Passcounttotal
    111
    223
    336
    4410
    5515

    After pass 5 the total, 15, is no longer below 11, so the loop ends and the function returns 5.

    Step by step

    1. Create total with :=, starting at 0. Nothing has been added yet.
    2. Create count with :=, starting at 0. No numbers have been added.
    3. Loop while total < limit. The loop stops exactly when the total is at least limit, which is when adding must stop.
    4. Add 1 to count with ++. It is now the next number: 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 next one in the sequence.
    6. Return count after the loop.

    The solution in Go

    func sumUntilLimit(limit int) int {
        total := 0
        count := 0
        for total < limit {
            count++
            total += count
        }
        return count
    }
    

    Go details:

    • The condition is checked before every pass. That is why the pass that reaches the limit still runs fully: the check happens at the top, and a total that is still below limit lets the next pass start.
    • The type is int. It is 64 bits, so the total, which never goes far past limit, can't overflow for limits up to a billion.

    Complexity

    After k passes the total is about k squared over 2, so the loop needs roughly the square root of 2 * limit passes. For limit = 1000000000 it runs 44721 times. The space is constant.

    Edge cases and common mistakes

    • Reaching the limit exactly. limit = 10 stops at 4 numbers, because the condition total < limit is false when the total equals the limit. Writing total <= limit would add a fifth number.
    • Overshooting. For 11, the answer counts the number that carried the total past the limit.
    • Swapping the two lines in the loop. If total += count runs before count++, the first number added is 0 and every number is one too small.
    • Returning total. The question asks how many numbers were added, not their sum.

    Testing

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