How to calculate sum of the integers from $m$ to $n$.

10.9k Views Asked by At

How to calculate the sum of the integers from $m$ to $n$?

Is this correct?
$$ \frac{n (n+1)}{2} - \frac{m (m+1)}{2}$$

3

There are 3 best solutions below

2
On BEST ANSWER

Assuming $m\le n$, you are rather subtracting the sum from $0$ to $m-1$ to the sum from $0$ to $n$, so $\frac{n(n+1)}{2}-\frac{m(m-1)}{2}=\frac{(m-n+1)(m+n)}2$. This "incidentally" works when $m<0$ or $n\le0$ as well.

0
On

This is a special of the sum of consecutive terms of an arithmetic progression $(a_n)$: such a sum is equal to the arithmetic mean of the first and the last terms, multiplied by the number of terms: $$a_m+a_{m+1}+\dots+a_n=\frac{a_m+a_n}2(n-m+1).$$ In the present case, you obtain $$m+(m+1)+\dots+n=\frac{m+n}2 (n-m+1).$$

0
On

Your error was an indexing one.

$\frac {n (n+1)}2-\frac {m (m+1)}2 =(1+2+...+n)-(1+2+3+....+m)=(m+1)+...+n$ which is shifted from what you actually want.

You want:

$m+...+n=(1+.....+n)-(1+... (m-1))=\frac {n (n+1)}2-\frac {(m-1)m}2$.