# How to get inverse of formula for sum of integers from 1 to n? [duplicate]

I know very well that the sum of integers from $1$ to $n$ is $\dfrac{n\times(n+1)}2$. What I'm interested in today, and cannot find a solution for, is performing the opposite operation.

Let $m = \dfrac{n^2 + n} 2$. Knowing the value of $m$, how do I figure out the value of $n$? I could easily program a solution but I'd much prefer an algebraic one.

• Funny that the answer to this question isn't really related to the original statement. – Simply Beautiful Art Dec 3 '16 at 19:30
• you may not always get an integer value for n. You get an integer value only when m itself is a triangular number ( google triangular number). – user25406 Dec 3 '16 at 22:40
• @SimpleArt: actually it sort of did, it did say "Get n from formula..." Anyway I clarified the OP's title now. – smci Dec 4 '16 at 12:15
• If you are doing this in, say, C: uint64_t n = sqrt(2.0 * m); for small enough values of m. – Sinan Ünür Dec 4 '16 at 14:36
• Duplicate: Gauss Sums: Reversing the $T(n) = \frac{n(n+1)}{2}$ formula. (Found using Approach0.xyz) – Workaholic Dec 4 '16 at 19:18

Simple algebra suffices: \begin{align} m &= \frac{n^2+n}{2} \\ 2m &= \left(n+\frac{1}{2}\right)^2 - \frac{1}{4} \\ \sqrt{2m + \frac{1}{4}} - \frac{1}{2}&= n \end{align}

and we are done.

The easiest and quickest way is to multiply $$m$$ by $$2$$, take the square root, and round that down to the nearest whole number.

For Example: $$m = 55$$. And, $$55*2 =110$$. We have $$\sqrt{110} = 10.4$$.... Round down to $$10$$. So, $$n=10$$

This works because we know $$2m = n\times (n+1)$$, so square root of that is between $$n$$ and $$n+1$$.

But if you want an algebraic solution, you can use the formula $$n = \frac{-1+\sqrt{1+8\times m}}{2}$$

Same example: $$m=55:$$ $$n= \frac{-1+\sqrt{1+8\times55}}{2} = \frac{-1+\sqrt{441}}{2} = \frac{-1+21}{2} = \frac{20}{2} =10$$

You have got that $m = \dfrac{n^2 + n} 2$ which will give you $2m=n(n+1)$.

You can make a quadratic equation $n^2+n-2m=0$. On solving the quadratic equation you get that $n=\frac{-1 \pm\sqrt{1+8m}}{2}$. Now solve this (as you know the $m$, you can easily find $n$) and eliminate the negative solution (As $n$ can not be negative).

• I downvoted before you edited to include the quadratic equation. Your first paragraph (i.e., "just look for two consecutive integers whose product is $2m$) is little more than a restatement of the problem. I removed the downvoted when you added the second paragraph. – Théophile Dec 3 '16 at 17:07