L1 · FoundationsNumbers & loops~3 min · 5 tests

Sum of the First N Natural Numbers

Add 1 + 2 + … + n with a loop in O(n), then with Gauss's formula n(n+1)/2 in O(1). Your first time-complexity lesson, measured live in the browser.

The problem

Return 1 + 2 + … + n for a non-negative integer n. Try to do it without a loop.

Examples

  1. Example 1

    Input

    sum_to_n(10)

    Expected output

    55
  2. Example 2

    Input

    sum_to_n(1)

    Expected output

    1

+ 3 hidden tests on Submit.

Edge cases to ask about

  • n = 0
  • Large n

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · sum_to_n
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Loop and addO(n)O(1)
    bestGauss's formulaO(1)O(1)Pair 1 with n, 2 with n−1, …: n/2 pairs each summing to n+1.
    Walkthrough of the optimal approach (try it yourself first)

    Pair the numbers from both ends: 1 + n, 2 + (n−1), … each pair sums to n + 1 and there are n / 2 pairs, so the total is n(n + 1) / 2.

    Run both versions in the Complexity Lab: the loop's time grows in a straight line with n, the formula stays flat. That flat line is what O(1) means.

    Complexity: O(1) time, O(1) space. The formula costs the same three operations for any n; the loop grows linearly with n.

    Reveal the reference solution
    def sum_to_n(n):
        return n * (n + 1) // 2

    The brute force, for comparison

    def sum_to_n(n):
        total = 0
        for i in range(1, n + 1):
            total += i
        return total

    Follow-ups interviewers ask

    • Sum of squares 1² + … + n² in O(1).

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Sum of the First N Natural Numbers in Python?

    The optimal solution runs in O(1) time and O(1) auxiliary space. The formula costs the same three operations for any n; the loop grows linearly with n.

    What is the brute-force approach, and how do you optimise it?

    Loop and add: O(n) time, O(1) space. Gauss's formula: O(1) time, O(1) space. Pair 1 with n, 2 with n−1, …: n/2 pairs each summing to n+1.

    What follow-up questions do interviewers ask about Sum of the First N Natural Numbers?

    Sum of squares 1² + … + n² in O(1).