There’s a renowned story that Gauss, mathematician extraordinaire, had actually a lazy teacher. The so-called educator wanted to store the children busy for this reason he could take a nap; that asked the course to add the numbers 1 come 100.
You are watching: Sum of all numbers from 1 to 1000
Gauss approached v his answer: 5050. So soon? The teacher suspected a cheat, yet no. Manual enhancement was for suckers, and also Gauss found a formula to sidestep the problem:
Let’s re-publishing a couple of explanations of this an outcome and really recognize it intuitively. For these examples we’ll add 1 to 10, and then see how it uses for 1 come 100 (or 1 to any number).
Technique 1: Pair Numbers
Pairing numbers is a common approach to this problem. Rather of composing all the numbers in a solitary column, stop wrap the number around, choose this:
1 2 3 4 510 9 8 7 6An amazing pattern emerges: the sum of each tower is 11. Together the height row increases, the bottom row decreases, for this reason the sum continues to be the same.
Because 1 is paired with 10 (our n), we deserve to say the each column has actually (n+1). And also how countless pairs perform we have? Well, we have actually 2 equal rows, us must have n/2 pairs.
which is the formula above.
Wait — what around an odd number of items?
Ah, ns glad you lugged it up. What if we are adding up the numbers 1 come 9? us don’t have an even number of items to pair up. Plenty of explanations will certainly just offer the explanation above and leaving it in ~ that. Ns won’t.
Let’s include the number 1 come 9, but instead of beginning from 1, let’s count from 0 instead:
0 1 2 3 49 8 7 6 5By counting native 0, we acquire an “extra item” (10 in total) therefore we can have one even variety of rows. However, our formula will certainly look a little bit different.
Notice that each column has actually a amount of n (not n+1, like before), since 0 and 9 space grouped. And instead the having exactly n items in 2 rows (for n/2 bag total), we have actually n + 1 items in 2 rows (for (n + 1)/2 pairs total). If friend plug this numbers in you get:
which is the very same formula together before. It constantly bugged me that the same formula worked for both odd and also even number – won’t you gain a fraction? Yep, you get the same formula, but for various reasons.
Technique 2: Use two Rows
The above method works, however you handle odd and also even numbers differently. Isn’t there a much better way? Yes.
Instead that looping the number around, let’s compose them in two rows:
1 2 3 4 5 6 7 8 9 1010 9 8 7 6 5 4 3 2 1Notice the we have actually 10 pairs, and also each pair adds approximately 10+1.
The total of every the numbers over is
But we only want the amount of one row, no both. For this reason we division the formula over by 2 and get:
Now this is cool (as cool together rows the numbers can be). It works for an odd or even number of items the same!
Technique 3: make a Rectangle
I newly stumbled upon another explanation, a fresh approach to the old pairing explanation. Various explanations work better for different people, and I tend to prefer this one better.
Instead of composing out numbers, pretend we have actually beans. We desire to include 1 bean to 2 bean to 3 beans… every the means up to 5 beans.
xx xx x xx x x xx x x x xSure, we can go to 10 or 100 beans, but with 5 you acquire the idea. Just how do us count the variety of beans in ours pyramid?
Well, the amount is clearly 1 + 2 + 3 + 4 + 5. Yet let’s look at it a various way. Stop say we winter our pyramid (I’ll usage “o” because that the mirrored beans), and also then topple the over:
x o x o o o o ox x o o x x o o o ox x x o o o => x x x o o ox x x x o o o o x x x x o ox x x x x o o o o o x x x x x oCool, huh? In case you’re wondering even if it is it “really” lines up, the does. Take it a look in ~ the bottom heat of the continuous pyramid, with 5′x (and 1 o). The following row that the pyramid has 1 less x (4 total) and also 1 much more o (2 total) to fill the gap. Just like the pairing, one next is increasing, and the other is decreasing.
Now for the explanation: How countless beans do we have total? Well, that’s simply the area that the rectangle.
We have actually n rows (we didn’t change the number of rows in the pyramid), and also our repertoire is (n + 1) devices wide, because 1 “o” is combine up through all the “x”s.
Notice the this time, us don’t care around n being odd or also – the total area formula works out just fine. If n is odd, we’ll have actually an even variety of items (n+1) in every row.
But the course, we don’t want the full area (the number of x’s and also o’s), we just want the number of x’s. Since we doubled the x’s to gain the o’s, the x’s through themselves room just half of the complete area:
And we’re ago to our original formula. Again, the number of x’s in the pyramid = 1 + 2 + 3 + 4 + 5, or the amount from 1 come n.
Technique 4: median it out
We all understand that
average = sum / variety of items
which we deserve to rewrite to
sum = average * variety of items
So let’s number out the sum. If we have actually 100 number (1…100), then we clearly have 100 items. That was easy.
To gain the average, notification that the numbers are all equally distributed. Because that every huge number, yes sir a little number ~ above the other end. Let’s look in ~ a tiny set:
1 2 3The average is 2. 2 is currently in the middle, and 1 and also 3 “cancel out” for this reason their typical is 2.
For an even number of items
1 2 3 4the mean is in between 2 and 3 – that 2.5. Also though we have a fractional average, this is yes — due to the fact that we have actually an even number of items, when we multiply the mean by the count that ugly portion will disappear.
Notice in both cases, 1 is ~ above one side of the average and N is equally far away top top the other. So, we can say the median of the entire set is actually simply the median of 1 and n: (1 + n)/2.
Putting this into our formula
And voila! We have actually a fourth way of thinking around our formula.
So why is this useful?
1) adding up numbers quickly can be beneficial for estimation. An alert that the formula increases to this:
Let’s to speak you want to include the numbers from 1 to 1000: suppose you acquire 1 extr visitor come your site each day – exactly how many full visitors will you have actually after 1000 days? due to the fact that thousand squared = 1 million, we acquire million / 2 + 1000/2 = 500,500.
2) This concept of adding numbers 1 to N mirrors up in various other places, prefer figuring out the probability for the date of birth paradox. Having actually a firm grasp of this formula will help your expertise in numerous areas.
3) most importantly, this example shows there are countless ways to understand a formula. Possibly you like the pairing method, probably you prefer the rectangle technique, or maybe there’s another explanation that functions for you. Don’t offer up once you don’t know — try to find one more explanation that works. Happy math.
By the way, over there are more details about the background of this story and the an approach Gauss may have used.
Instead that 1 come n, how around 5 come n?
Start through the continual formula (1 + 2 + 3 + … + n = n * (n + 1) / 2) and subtract off the component you don’t desire (1 + 2 + 3 + 4 = 4 * (4 + 1) / 2 = 10).
Sum because that 5 + 6 + 7 + 8 + … n =
Sum indigenous a to n =
See more: How Much Is Magic Johnson Autograph Worth, Magic Johnson
How around even numbers, favor 2 + 4 + 6 + 8 + … + n?
Just dual the continual formula. To include evens native 2 to 50, discover 1 + 2 + 3 + 4 … + 25 and dual it:
Sum the 2 + 4 + 6 + … + n = 2 * (1 + 2 + 3 + … + n/2) = 2 * n/2 * (n/2 + 1) / 2 = n/2 * (n/2 + 1)So, to gain the evens native 2 come 50 you’d execute 25 * (25 + 1) = 650
How about odd numbers, like 1 + 3 + 5 + 7 + … + n?
That’s the exact same as the also formula, other than each number is 1 less than its counterpart (we have 1 instead of 2, 3 rather of 4, and also so on). We gain the following biggest even number (n + 1) and take turn off the extra (n + 1)/2 “-1″ items:
Sum of 1 + 3 + 5 + 7 + … + n = <(n + 1)/2 * ((n + 1)/2 + 1)> – <(n + 1) / 2>To include 1 + 3 + 5 + … 13, acquire the next biggest also (n + 1 = 14) and do
<14/2 * (14/2 + 1)> – 7 = 7 * 8 – 7 = 56 – 7 = 49Combinations: evens and offset
Let’s to speak you want the evens native 50 + 52 + 54 + 56 + … 100. Find all the evens
2 + 4 + 6 + … + 100 = 50 * 51and subtract turn off the people you nothing want
2 + 4 + 6 + … 48 = 24 * 25So, the sum from 50 + 52 + … 100 = (50 * 51) – (24 * 25) = 1950
Phew! expect this helps.
Ruby nerds: friend can inspect this using
<...Array(51).keys()>.map(x => x + 50).filter(x => x % 2 == 0).reduce((x, y) => x + y)1950// Note: There room 51 number from 50-100, inclusive. Fencepost!