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 in one quick calculation, with no adding one number at a time.
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
The capital Greek letter sigma, written , means add things up. Under it, says where to start the counter, and the letter on top, , says where to stop. So means . Each value of the counter 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 in this formula is a whole number. If , the list is empty, and an empty total is 0.
- Multiplying and dividing in a formula
Letters side by side mean multiply, so means . A fraction bar means divide, so means the product divided by 2. Brackets come first. For , , the product is , and half of 20 is 10, which equals .
- Powers, like
means , read n squared. For it is 100, and for 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
Big O is a short way to describe how the work grows as the input grows. means the work grows roughly like times for large , and small constants and smaller terms are ignored. So a total of is still , 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 from 1 to , and the inner loop runs times on outer run number , the inner work adds up to in total.
What each symbol means
| Symbol | Meaning |
|---|---|
| The total of all the whole numbers from 1 up to , written out as . The three dots mean ‘and so on’. The result has no unit when the numbers count items, such as cans or steps. | |
| The counter, a whole number label for each term. It starts at 1 and takes each value from 1 up to in turn, so the first term has and the last has . It counts positions in the list, not amounts. | |
| The last number in the list, a whole number that is 0 or more. For example 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. | |
| The closed form, a quick formula for the same total. Multiply by the next number, , and then divide by 2. For , that is , and half of that is 5050. |
The formula piece by piece
The counter. On the first term , on the second , and on the last term . It is a label for the position of each term, and it moves one step at a time from 1 up to .
The total of every term: the first number plus the second, and so on up to . Written out, it is , with terms in all.
The number right after , one bigger than the last number in the list. For , . Pairing each number from the front of the list with one from the back gives this same total every time.
The product of 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 it is .
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.
In one plain sentence: adding the whole numbers from 1 up to gives the same answer as multiplying by the next number, , 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 . You get out the total of all the whole numbers from 1 to . The sum shows up whenever a task takes one step for the first item, two for the second, and so on up to . Adding the numbers one at a time takes 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 . The second and the second to last make , which is also . Every column adds to . There are columns, so both copies together add to .
Why divide by 2? The two copies together give , and each copy is the same total. So one copy is half of , which gives . The result is always a whole number. The product of two numbers in a row, such as and , is always even, so half of it is always whole.
Why it matters for loops. The result grows like . The later terms are large and the early terms are small, so the total is about half of . A loop over from 1 to with an inner loop that runs times does steps in all. That is about half of , so it is still in big O notation. The formula applies only when the inner count grows with .
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 cans. Make a copy of the triangle, turn it upside down and fit it against the original. Together they form a rectangle that is rows tall and cans wide. The rectangle holds cans, so one triangle holds half of them, which is the formula’s .
Worked example: Adding 1 to 100
Add the whole numbers from 1 to , so the total is . Then count the steps of a nested loop that reaches the same total, to see how this sum appears in code.
- Find the next number. The formula needs , the number right after . For , it is . This is also the total of each matching pair in the forward and backward lists: , and .
- Multiply. Multiply by , which gives . 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.
- Divide by 2. Half of the double count is the total we want, because the double count holds each pair twice. Dividing gives . So equals 5,050.
- 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.
- Read the nested loop. An outer loop runs from 1 to 100. For each value of , the inner loop runs times. The inner work therefore adds up to , which is the same 5,050 steps.
- Compare with . The square would be the step count if the inner loop ran 100 times every time. The real count, 5,050, is about half of that: . Since the share stays close to half, the work is still .
Answer: The sum of the whole numbers from 1 to 100 is 5,050. A nested loop whose inner body runs times for each up to 100 does 5,050 steps, which is about half of .
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Using for the sum from 1 to . That formula stops one term early, at . The sum up to uses , with , not .
- Forgetting the division by 2. The product counts both copies of the list, so it is twice the sum. For , leaving out the division gives 10,100 instead of 5,050.
- Using the triangle formula for a fixed inner loop. If the inner loop always runs times, the total is , not . The formula needs the inner count to grow with .
- Expecting an exact result. The sum is , so the lower term is not zero. Big O drops it, but the exact count keeps it.
Where you will meet it
- Counting comparisons in bubble sort, a simple sorting method. The first pass compares pairs, the next pass compares one fewer, and so on, which adds up to .
- Counting handshakes. In a group of people where each pair shakes hands once, there are handshakes, which is the sum of the whole numbers from 1 to .
- Triangular numbers, such as the number of cans in a stacked triangle. Each total of 1 up to is called a triangular number.
- Analyzing the cost of algorithms that check every pair of items in a list.
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 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 . The formula also works for , where .
How does this connect to big O?
The sum grows like , so it is . Big O drops the constant and the lower term , because it keeps only the part that grows fastest.
Related formulas
- Big O Notation: Big O notation is a way to say that one function never grows faster than another, once the input is big enough.
- Geometric Series: A geometric series adds up a list where each term is the one before it times a fixed number, and this formula finds the whole sum in one step.
- Master Theorem: The master theorem tells you how long a program takes when it cuts a problem into smaller pieces, solves each piece and joins the answers.