Formula Bro

Math and physics formulas, explained like you are new.


Sum of the First n Integers

In one sentence: This formula gives the total of the numbers 1, 2, 3 and so on up to nn in one quick calculation, with no adding one number at a time.

∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2}

Read it out loud: “The sum, written with the Greek capital letter sigma, of i, for every whole number i from 1 up to n, equals n times n plus 1, divided by 2. The letter i is a counter that takes the values 1, 2, 3, up to n, and n is the last number in the list.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

The sum sign ∑\sum

The capital Greek letter sigma, written ∑\sum, means add things up. Under it, i=1i = 1 says where to start the counter, and the letter on top, nn, says where to stop. So ∑i=13i\sum_{i=1}^{3} i means 1+2+3=61 + 2 + 3 = 6. Each value of the counter ii gives one term in the total.

Whole numbers and counting numbers

Whole numbers are 0, 1, 2, 3 and so on, with no fractions and no negative numbers. Counting numbers, also called natural numbers, start at 1. The letter nn in this formula is a whole number. If n=0n = 0, the list is empty, and an empty total is 0.

Multiplying and dividing in a formula

Letters side by side mean multiply, so n(n+1)n(n+1) means n×(n+1)n \times (n+1). A fraction bar means divide, so n(n+1)2\frac{n(n+1)}{2} means the product divided by 2. Brackets come first. For n=4n = 4, n+1=5n + 1 = 5, the product is 4×5=204 \times 5 = 20, and half of 20 is 10, which equals 1+2+3+41 + 2 + 3 + 4.

Powers, like n2n^{2}

n2n^{2} means n×nn \times n, read n squared. For n=10n = 10 it is 100, and for n=100n = 100 it is 10000. A power is repeated multiplication, and the small raised number is the exponent that says how many copies to multiply together.

Big O notation, like O(n2)O(n^{2})

Big O is a short way to describe how the work grows as the input grows. O(n2)O(n^{2}) means the work grows roughly like nn times nn for large nn, and small constants and smaller terms are ignored. So a total of n22+n2\frac{n^{2}}{2} + \frac{n}{2} is still O(n2)O(n^{2}), because the squared term grows fastest.

Loops and nested loops

A loop is a block of code that repeats again and again. A nested loop is a loop inside another loop. If an outer loop runs ii from 1 to nn, and the inner loop runs ii times on outer run number ii, the inner work adds up to 1+2+⋯+n1 + 2 + \cdots + n in total.

What each symbol means

SymbolMeaning

∑i=1ni\sum_{i=1}^{n} i

The total of all the whole numbers from 1 up to nn, written out as 1+2+3+⋯+n1 + 2 + 3 + \cdots + n. The three dots mean ‘and so on’. The result has no unit when the numbers count items, such as cans or steps.

ii

The counter, a whole number label for each term. It starts at 1 and takes each value from 1 up to nn in turn, so the first term has i=1i = 1 and the last has i=ni = n. It counts positions in the list, not amounts.

nn

The last number in the list, a whole number that is 0 or more. For example n=100n = 100 means the list runs from 1 to 100. It has no unit; it counts how many numbers are added, which is also the number of steps a plain loop would take.

n(n+1)2\frac{n(n+1)}{2}

The closed form, a quick formula for the same total. Multiply nn by the next number, n+1n + 1, and then divide by 2. For n=100n = 100, that is 100×101=10100100 \times 101 = 10100, and half of that is 5050.

The formula piece by piece

ii

The counter. On the first term i=1i = 1, on the second i=2i = 2, and on the last term i=ni = n. It is a label for the position of each term, and it moves one step at a time from 1 up to nn.

∑i=1ni\sum_{i=1}^{n} i

The total of every term: the first number plus the second, and so on up to nn. Written out, it is 1+2+3+⋯+n1 + 2 + 3 + \cdots + n, with nn terms in all.

n+1n + 1

The number right after nn, one bigger than the last number in the list. For n=100n = 100, n+1=101n + 1 = 101. Pairing each number from the front of the list with one from the back gives this same total every time.

n(n+1)n(n+1)

The product of nn and the next number. Writing the list forward and backward gives two copies of the list, and this product adds up both copies, so it is double the total. For n=100n = 100 it is 100×101=10100100 \times 101 = 10100.

n(n+1)2\frac{n(n+1)}{2}

The double count removed. Dividing by 2 leaves one copy of the list, which is the total we want. The division is needed because each pair of numbers was counted twice in the product.

∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2}

In one plain sentence: adding the whole numbers from 1 up to nn gives the same answer as multiplying nn by the next number, n+1n + 1, and then dividing the result by 2.

What the formula is doing, and why

What goes in and what comes out. You put in one whole number nn. You get out the total of all the whole numbers from 1 to nn. The sum shows up whenever a task takes one step for the first item, two for the second, and so on up to nn. Adding the numbers one at a time takes nn additions. The formula gives the same total with one multiplication and one division.

Why does it work? Write the list twice, once forward and once backward, and add the two copies column by column. The first number and the last make 1+n1 + n. The second and the second to last make 2+(n−1)2 + (n - 1), which is also n+1n + 1. Every column adds to n+1n + 1. There are nn columns, so both copies together add to n(n+1)n(n+1).

Why divide by 2? The two copies together give n(n+1)n(n+1), and each copy is the same total. So one copy is half of n(n+1)n(n+1), which gives n(n+1)2\frac{n(n+1)}{2}. The result is always a whole number. The product of two numbers in a row, such as nn and n+1n+1, is always even, so half of it is always whole.

Why it matters for loops. The result grows like n22\frac{n^{2}}{2}. The later terms are large and the early terms are small, so the total is about half of n2n^{2}. A loop over ii from 1 to nn with an inner loop that runs ii times does n(n+1)2\frac{n(n+1)}{2} steps in all. That is about half of n2n^{2}, so it is still O(n2)O(n^{2}) in big O notation. The formula applies only when the inner count grows with ii.

An everyday picture

Think of stacking cans in a triangle. The top row has 1 can, the next row has 2 cans, and the bottom row has nn cans. Make a copy of the triangle, turn it upside down and fit it against the original. Together they form a rectangle that is nn rows tall and n+1n + 1 cans wide. The rectangle holds n(n+1)n(n+1) cans, so one triangle holds half of them, which is the formula’s n(n+1)2\frac{n(n+1)}{2}.

Worked example: Adding 1 to 100

Add the whole numbers from 1 to n=100n = 100, so the total is 1+2+3+⋯+1001 + 2 + 3 + \cdots + 100. Then count the steps of a nested loop that reaches the same total, to see how this sum appears in code.

  1. Find the next number. The formula needs n+1n + 1, the number right after nn. For n=100n = 100, it is 101101. This is also the total of each matching pair in the forward and backward lists: 1+100=1011 + 100 = 101, and 2+99=1012 + 99 = 101.
  2. Multiply. Multiply nn by n+1n + 1, which gives 100×101=10100100 \times 101 = 10100. This product counts both copies of the list, so it is twice the total we want. The number 10,100 is the full double count.
  3. Divide by 2. Half of the double count is the total we want, because the double count holds each pair twice. Dividing gives 10100÷2=505010100 \div 2 = 5050. So 1+2+⋯+1001 + 2 + \cdots + 100 equals 5,050.
  4. Check it by adding one at a time. A computer can add the numbers from 1 to 100 one at a time, keeping a running total as it goes. It also reaches 5,050 at the end, so the slow method and the one-line formula agree.
  5. Read the nested loop. An outer loop runs ii from 1 to 100. For each value of ii, the inner loop runs ii times. The inner work therefore adds up to 1+2+⋯+1001 + 2 + \cdots + 100, which is the same 5,050 steps.
  6. Compare with n2n^{2}. The square 1002=10000100^{2} = 10000 would be the step count if the inner loop ran 100 times every time. The real count, 5,050, is about half of that: 505010000=0.505\frac{5050}{10000} = 0.505. Since the share stays close to half, the work is still O(n2)O(n^{2}).

Answer: The sum of the whole numbers from 1 to 100 is 5,050. A nested loop whose inner body runs ii times for each ii up to 100 does 5,050 steps, which is about half of 1002100^{2}.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

Why is there a divide by 2?

Every pair of numbers gets counted twice when you write the list forward and backward. The product n(n+1)n(n+1) holds both copies, so dividing by 2 leaves one copy, which is the total.

Does the formula work when $n = 0$?

Yes. An empty list adds to 0, and 0⋅12=0\frac{0 \cdot 1}{2} = 0. The formula also works for n=1n = 1, where 1⋅22=1\frac{1 \cdot 2}{2} = 1.

How does this connect to big O?

The sum grows like n22\frac{n^{2}}{2}, so it is O(n2)O(n^{2}). Big O drops the constant 12\frac{1}{2} and the lower term n2\frac{n}{2}, because it keeps only the part that grows fastest.

Sources

  1. Triangular number, Wikipedia
  2. Summation, Wikipedia

| All Computer Science formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: computer-science/sum-of-first-n-integers.md.