adding up
the maths you need
what it is
Add the whole numbers from 1 to and you get . Stop one short, as in 1 + 2 + 3 for 4, and you get the comparisons bubble sort makes on a row of numbers back to front.
Take a row back to front, each number smaller than the one before: 5, 4, 3, 2, 1. Bubble sort walks along it, comparing each pair of neighbours and swapping them when the bigger comes first. Walk 1 makes 4 comparisons and carries the 5 to the end. Each walk after it stops one pair sooner. That is 4 + 3 + 2 + 1 = 10 comparisons.
The comparisons 4, 3, 2, 1 form a run: whole numbers next to each other. Its sum is 10, and turned round, 1 + 2 + 3 + 4 has the same sum. means every whole number from 1 to , added. The table adds the runs up to 6.
| the run | half of that | the run's sum | ||
|---|---|---|---|---|
| 1 | 1 | 2 | 1 | 1 |
| 2 | 1 + 2 | 6 | 3 | 3 |
| 3 | 1 + 2 + 3 | 12 | 6 | 6 |
| 4 | 1 + 2 + 3 + 4 | 20 | 10 | 10 |
| 5 | 1 + 2 + 3 + 4 + 5 | 30 | 15 | 15 |
| 6 | 1 + 2 + 3 + 4 + 5 + 6 | 42 | 21 | 21 |
One formula gives the sum at once: , half of times one more than . Two staircases show why.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
guess it, thenthe move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
guess it, thenthe move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: count row by row
the line:A square 5 wide, its diagonal edged in heavy ink. The squares on or below it are yellow, 1, 2, 3, 4 and 5 by row, 15 in all. row on or below the diagonal on or above it on it 1 1 5 1 2 2 4 1 3 3 3 1 4 4 2 1 5 5 1 1 in all 15 15 5 The square 5 wide, counted row by row. why it is allowed: Counting rows from the top, row 1 has 1 square on or below it, row 2 has 2, and row has . Let be their total, the sum of question 1 again.
the move: double both sides
the line:Yellow and red, 15, lie on or below the diagonal; blue and red, 15, on or above it. The 5 red are in both: 15 + 15 is 30, the 25 squares and the 5 red again. why it is allowed: The plan: two lots of cover the square with the diagonal squares counted twice, as the picture shows. Lines 2 and 3 build that from line 1, and line 4 counts it. Doubling both sides keeps them equal.
guess it, thenthe move: take n from both sides
the line:why it is allowed: Taking the same number from both sides of line 2 keeps them equal.
guess it, thenthe move: count the whole square
the line:why it is allowed: Flip the square over along its diagonal: each square below it trades places with one above. So squares lie on or above it too, as the grid at line 1 shows. The right side of line 3 counts both lots, less the counted twice: every square once, rows of , which is , read squared. For 5, 15 + 15 is 30, and 30 less 5 is 25.
guess it, thenthe move: add n to both sides
the line:why it is allowed: Adding the same number to both sides of line 4 keeps them equal. For 5, twice 15 is 30, which is 25 + 5.
guess it, thenthe move: halve both sides
the line:why it is allowed: Halving both sides of line 5 keeps them equal.
guess it, thenthe move: write it as n times one more
the line:why it is allowed: is lots of and lots of 1, which is times one more than . The same formula as question 1.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: name the growth
the line:why it is allowed: The sum to is the sum to with added. Let be how much the formula grows over that step: the formula at , less the formula at , which puts in place of each . At 4, that is 10 less 6, which is 4.
the move: tidy the bracket
the line:why it is allowed: In line 1, is .
guess it, thenthe move: put both over 2
the line:why it is allowed: Half of one number, less half of another, is half of their difference. At 4, 10 less 6 is 4, and 20 less 12, halved, is 4 too.
guess it, thenthe move: multiply out both brackets
the line:why it is allowed: lots of make . lots of are lots of , less one , so . At 4, 20, and 16 less 4, 12.
guess it, thenthe move: take away part by part
the line:why it is allowed: Taking away takes away and gives back . At 4, taking 12 from 20 is taking 16 and giving back 4, which leaves 8.
guess it, thenthe move: collect like parts
the line:why it is allowed: In line 5, less is nothing, and and make .
guess it, thenthe move: halve
the line:- the formula,
The formula from 1 to 6, steeper at each step. The table gives each climb: 2, 3, 4, 5 and 6. the sum to the formula grows by 1 1 1 2 3 3 2 3 6 6 3 4 10 10 4 5 15 15 5 6 21 21 6 The sum and the formula side by side, each growing by the number added. why it is allowed: Half of is . The plot and the table check it from 2 to 6.
guess it, thenthe move: read it
the line:why it is allowed: At 1 the sum is 1, and 1 times 2, halved, is 1 too. Each step puts onto the sum, and by line 7 the formula grows by too. Equal at the start and growing alike, they stay equal for every . This is an argument from a start and a step, called proof by induction.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: name the sum
the line:why it is allowed: Let be the sum from 1 to , for from 2; at 1 there is nothing to add. Here is how many numbers bubble sort's row holds. For 4, is 1 + 2 + 3.
the move: use question 1 again
the line:why it is allowed: Question 1 holds for any last number from 1. Put in place of each in line 4 of question 1.
guess it, thenthe move: tidy the bracket
the line:Bubble sort on 4, 3, 2, 1 makes 3 + 2 + 1 = 6 comparisons, and 3 times 4, halved, is 6. the sum to , halved bubble sort on back to front 2 1 1 1 3 3 3 3 4 6 6 6 5 10 10 10 6 15 15 15 The sum one short, its formula and bubble sort's comparisons agree for rows of 2 to 6 numbers. why it is allowed: In line 2, is . For 4, 3 times 4, halved, is 6.
guess it, thenthe move: read it
the line:why it is allowed: Bubble sort stops after a walk with no swap. On numbers back to front, each walk carries the biggest unsorted number to the end and leaves the rest back to front. So every walk swaps, and bubble sort never stops before walk . Walk 1 compares all pairs of neighbours, and each walk after it one fewer, down to 1. That is this sum, added from the top down, as the picture at line 3 shows for 4.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: name the sum
the line:why it is allowed: Let be the sum of question 1.
the move: use question 1
the line:why it is allowed: By line 4 of question 1.
guess it, thenthe move: multiply out the bracket
the line:why it is allowed: lots of make , and lots of 1 make .
guess it, thenthe move: halve each part
the line:The sum to 10 as a staircase in a square 10 wide, 55 of its 100 squares. The dashed line halves the square, 50, and the staircase pokes above it by 10 half squares, 5. half of the sum 1 0.5 1 1 2 2 3 4 4 8 10 16 10 50 55 100 20 200 210 400 40 800 820 1,600 The sum always lies between half of and . why it is allowed: Half of a total is half of each part, added. For 4, 8 and 2 make 10.
guess it, thenthe move: a fact about n
the line:why it is allowed: is at least 1, so times is at least times 1. For 4, 4 is at most 16.
guess it, thenthe move: halve both sides
the line:why it is allowed: Halving both sides of line 5 keeps 'at most' the right way round, since one half is above zero. For 4, 2 is at most 8.
guess it, thenthe move: put half of n squared for half of n
the line:why it is allowed: By line 6, half of is at most half of . Put half of in its place in line 4. The right side gets no smaller, so is at most it. For 4, 8 + 2 is at most 8 + 8.
guess it, thenthe move: add the halves
the line:why it is allowed: In line 7, two halves of make . For 4, 10 is at most 16.
guess it, thenthe move: read it as a ceiling
the line:why it is allowed: By line 8, is at most 1 times from 1 on, and 1 is a fixed number above zero. So a fixed multiple of is a ceiling of : is in big O of . At 10, 55 is at most 100.
guess it, thenthe move: leave out the half of n
the line:why it is allowed: Line 4 adds half of , which is above zero, to half of . So is at least half of . For 4, 10 is at least 8.
guess it, thenthe move: read it as a floor
the line:- the sum,
- the ceiling of line 8,
- the floor of line 10,
The sum between its ceiling and its floor, for 1 to 10. At 10 they are 55, 100 and 50, so the sum is about half of . why it is allowed: By line 10, is at least one half times from 1 on, and one half is a fixed number above zero. So a fixed multiple of is a floor of too: is in big omega of .
guess it, thenthe move: a ceiling and a floor together
the line:why it is allowed: By lines 9 and 11, is in and in , so it is in . It grows like . By line 4, doubling makes half of 4 times as big and half of only twice as big. So the sum grows a little under 4 times: 55 at 10, 210 at 20, 820 at 40.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
a worked example
Add up 1 to 12 three ways.
- Two staircases: 12 columns of 13 squares hold 156, so one staircase, the sum, is half of that, 78.
- The square: a square 12 wide holds 144 squares. Twice the sum covers it with its 12 diagonal squares counted twice: 144 + 12 is 156. So the sum is 78.
- One at a time: the formula at 11, 11 times 12, halved, is 66, the sum to 11. Adding 12 gives 78, and the formula at 12, 12 times 13, halved, is 78 too.
the trap
check yourself
What is 1 + 2 + ... + 60?
the answer
60 times 61 is 3,660, and half of that is 1,830, by question 1.
How many comparisons does bubble sort make on 20 numbers back to front?
the answer
19 + 18 + ... + 1, the sum one short for 20. That is 19 times 20, halved, 190, by question 4.
What is 21 + 22 + ... + 30? It is the run from 1 to 30 without its first 20 numbers.
the answer
1 to 30 is 30 times 31, halved, 465. 1 to 20 is 20 times 21, halved, 210. So 465 less 210 leaves 255.