Gauss formula to add number of sequence for arbitrary range
Emily Wong
Gauss formula to add numbers from $1-100$ is:
$$ \frac{n(n+1)}{2}$$
How can this be made applicable for arbitrary range, lets say $3-30$? Is there an easy way of doing that rather then linearly adding the numbers up?
$\endgroup$ 13 Answers
$\begingroup$The sum of the natural numbers from $a$ to $b$ inclusive is $$\frac{(a+b)(b-a+1)}{2}.$$
$\endgroup$ 2 $\begingroup$I don't like the accepted answer, because it does not contain any explanation. It is very similar to bad quality code, which people don't understand just copy-paste and hope that it will work without even testing it for edge cases. $$\frac{(a+b)(b-a+1)}{2}$$
The whole formula is based on the idea that you can count the average of the sequence from the first and last number, because if we have a regular gap between numbers, then the average should be right in the middle between the first and last numbers. By multiplying the average with the count of the numbers you will get the total of the sequence.
We can define our general sequence with constant gap size this way:$$general = [first + 0*gapSize, first+1*gapSize, ..., first+gapCount*gapSize]$$$$total(general) = first + (first+gapSize) + ... + (first+gapCount*gapSize)$$$$average(general) = total(general)/numberCount$$
For the sake of simplicity we can transform it into an index sequence:
$$index = (general-first)/gapSize = [0, 1, ..., gapCount]$$$$total(index) = 0 + 1 + ... + gapCount$$$$average(index) = total(index)/numberCount$$
If we check the formula of the totals we might notice that we need to subtract the first number as many times as many numbers we have to get a proper total. The division does not involve that kind of changes. Another thing we might notice that the count of numbers is one more than the count of gaps between numbers.
$$total(index) = (total(general) - first*numberCount)/gapSize$$$$numberCount = gapCount + 1$$
So all we need is showing that the average is right in the middle between the first and last value. For our index sequence that should be $gapCount/2$. If we transform the sequence further and subtract the $gapCount/2$, then we should get a sequence that has a total of zero. If that is true, then we really found the average of the index sequence.
$$zero = index - \frac{gapCount}{2}$$$$zero = [0- \frac{gapCount}{2}, 1- \frac{gapCount}{2}, ..., gapCount- \frac{gapCount}{2}]$$$$total(zero) = - \frac{gapCount}{2} + (1- \frac{gapCount}{2}) + ... + gapCount- \frac{gapCount}{2}$$$$total(zero) = - \frac{gapCount}{2} + (1- \frac{gapCount}{2}) + ... + \frac{gapCount}{2}$$
We can use the $g = gapCount$ abbreviation to expand it further. By doing that we should notice we have nice pairs that cancel out each other.
$$total(zero) = - \frac{g}{2} + (1- \frac{g}{2}) + (2- \frac{g}{2}) + ... + (\frac{g}{2}-2) (\frac{g}{2}-1) + \frac{g}{2}$$$$total(zero) = (- \frac{g}{2} + \frac{g}{2}) + (1- \frac{g}{2} + \frac{g}{2}-1) + (2- \frac{g}{2} + \frac{g}{2}-2)$$$$total(zero) = 0$$
We can have a little doubt about this pair approach for the case when the count of the numbers is not even. We can check that on a short example:
$$total([0]-0) = 0$$$$total([0,1,2]-2/2) = -1+0+1 = (-1+1) + 0 = 0$$$$total([0,1,2,3,4]-4/2) = -2+-1+0+1+2 = (-2+2) + (-1+1) + 0 = 0$$
So in the odd case the number in the middle will be the average and won't have a pair. We proved that indeed $gapCount/2$ is the average of an index sequence. We can use that info to count the total and average of our general sequence.
$$average(index) = \frac{gapCount}{2}$$$$average(index) = total(index)/numberCount$$$$total(index) = \frac{gapCount}{2} * numberCount$$$$total(index) = (total(general) - first*numberCount)/gapSize$$$$total(general) = first*numberCount + total(index)*gapSize$$$$total(general) = first*numberCount + \frac{gapCount}{2}*numberCount*gapSize$$$$total(general) = (first + \frac{gapCount}{2}*gapSize)*numberCount$$$$total(general) = average(general)*numberCount$$$$average(general) = first + \frac{gapCount}{2}*gapSize$$$$numberCount = gapCount + 1$$$$total(general) = (first + \frac{gapCount}{2}*gapSize)*(gapCount+1)$$
Since we like the last number of the sequence better than counting how many gaps we have in the sequence, we can use that too to describe the total and the average.
$$last = first + gapCount*gapSize$$$$gapCount = \frac{last-first}{gapSize}$$$$average(general) = first + \frac{gapCount}{2}*gapSize$$$$average(general) = first + \frac{\frac{last-first}{gapSize}}{2}*gapSize = \frac{first+last}{2}$$$$total(general) = average(general)*numberCount$$$$numberCount = gapCount + 1 = \frac{last-first}{gapSize} + 1$$$$total(general) = \frac{first+last}{2}*(\frac{last-first}{gapSize} + 1)$$
When the gap size is one, we can simplify with that too so we will end up with our original formula:
$$\frac{first+last}{2}*(last-first + 1)$$$$\frac{(a+b)(b-a+1)}{2}$$
I'd rather keep the gap size, so we can use it for sequences having a gap size different than one. Something like:
$$example = -2.56 + index * -0.44, numberCount = 5$$$$example = [-2.56,-3,-3.44,-3.88,-4.32]$$$$average(example) = \frac{-2.56-4.32}{2} = -3.44$$$$total(example) = -3.44*5 = -17.2$$
We can verify our formulas just to be sure:
$$numberCount = \frac{-4.32-(-2.56)}{-0.44} + 1 = \frac{4.32-2.56}{0.44} +1 = 4+1 = 5$$$$total(example) = -2.56 -3 -3.44 -3.88 -4.32 = -17.2$$
We can count the total for the example in the question too:
$$question = 3 + index, last=30$$$$question = [3,4,...,30]$$$$average(question) = \frac{3+30}{2} = 16.5$$$$numberCount = (\frac{30-3}{1} + 1) = 28$$$$total(question) = 16.5*28 = 462$$
$\endgroup$ $\begingroup$As a quick guideline and/or explanation of how to get the answer, consider adding the sequence to itself in reverse order:
$\begin{eqnarray} 2S_n & = & S_n + S_n \\ & = & a & + (a + d) & + \ldots & + (a + (n-1)d) & + (a + (n-1)d) & + \ldots & + (a + d) & + a \\ & = & a & + (a + d) & + \ldots & + (a + (n-1)d) \\ & + & (a + (n-1)d) & + (a + (n-2)d) & + \ldots & + a \\ & = & (2a + (n-1)d) & + (2a + (n-1)d) & + \ldots & + (2a + (n-1)d) \\ & = & n(2a + (n-1)d) \\ \therefore S_n & = & \frac{n}{2}(2a + (n-1)d)\end{eqnarray}$
which you can rearrange to give the alternative form.
$\endgroup$