Start learning free

LESSON 215 OF 287 · WHILE LOOPS - CHALLENGES

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

    PYTHON · WHILE LOOPS - CHALLENGES

    Sum Until Limit in Python, 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 total may go past the limit: the number that makes it reach or pass the limit counts too.

    For limit = 10, 1 + 2 + 3 + 4 is exactly 10, so the answer is 4. For limit = 11, the total after 4 numbers is still 10, so 5 is added as well, the total becomes 15, and the answer is 5. For limit = 1 the first number is enough, and the answer is 1.

    Approach

    Two values change as the loop runs: the total so far, and how many numbers have been added. Because the numbers go 1, 2, 3, in order, the count is also the last number added: after adding 1, 2 and 3, the count is 3.

    That lets one variable do two jobs. Each pass raises count by 1 to get the next number, then adds count to total. The loop keeps going while total < limit, so it stops on the first pass where the total reaches the limit, and the number added on that pass is included in the count.

    Step by step

    1. Start total at 0. Nothing has been added yet.
    2. Start count at 0. It counts the numbers added so far.
    3. Loop while total < limit. This is the exact opposite of the stopping rule "at least the limit".
    4. Add 1 to count. It becomes the next number to add: 1 on the first pass, 2 on the second.
    5. Add count to total. The order matters: count went up first, so the number added is the right one.
    6. Return count after the loop.

    On limit = 11:

    Passcount (number added)total
    111
    223
    336
    4410
    5515

    After pass 4 the total, 10, is still below 11, so pass 5 runs. After it the total is 15, the condition fails, and the function returns 5.

    The solution in Python

    class Solution:
        def sumUntilLimit(self, limit: int) -> int:
            total = 0
            count = 0
            while total < limit:
                count += 1
                total += count
            return count
    

    Details worth knowing:

    • The loop checks first. A while loop tests its condition before every pass, including the first. With total at 0 and a limit of at least 1, the first test is always true.
    • Why < and not <=. The task stops when the total is at least limit. The loop continues while the opposite holds: the total is below it. With total <= limit, the loop would add one more number when the total equals the limit. For 10 it would return 5, not 4.
    • Don't confuse the two variables. The function returns count, the number of terms, not total. For a limit of 11 the total is 15 but the answer is 5.
    • Large limits are fast enough. For limit = 1000000000 the loop runs 44721 times, which is quick.

    Edge cases and common mistakes

    • Limit 1. One pass: count is 1, total is 1, and the loop ends. The answer is 1.
    • Limit 2 and 3. Both give 2, because 1 + 2 is 3, which reaches both limits.
    • Swapping the two body lines. Adding count to total before raising count adds 0 on the first pass and every number one pass late, so the answer comes out wrong.

    Testing

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