How do I test if a number $x$, is a sum of consecutive natural numbers?

2.5k Views Asked by At

How do I test if a number $x$, is a sum of consecutive natural numbers? For example my test is passed for $55$ as it is $1 + 2 + 3 + \ldots + 10$ but fails for $54$?

Edit: The numbers start with 1 always.

7

There are 7 best solutions below

0
On BEST ANSWER

The formular for the sum of the first $n$ natural numbers is $$\frac{n(n+1)}{2}$$ A number $x$ appears in this sequence, iff $$\frac{n(n+1)}{2}=x \Leftrightarrow n^2+n-2x=0$$ has an non-negative integer solution. Since, this is a quadratic equation, you can simply compute the positive solution: $$n=\frac{\sqrt{1+8x}-1}{2}$$ This is an integer number, iff $1+8x$ is a square and $2\mid \sqrt{1+8x}-1$. You can see, that the latter is always true, if the former is true. So the answer is:

$x$ occurs in this sequence, if $1+8x$ is a perfect square.

0
On

we know

1 +2 +3 +...... n = S = n(n+1)/2

solving we get a quadratic in n

n^2 +n - 2S = 0

if this equation has a positive integer as one of its roots then the condition us satisfied

0
On

The sum of the numbers from $1$ to $n$ is $\frac 12n(n+1)$, so given a number $m$ we need to check if it is of this form. So $m=\frac 12n(n+1), n^2+n-2m=0$ and we can use the quadratic formula, $n=\frac 12(-1+\sqrt{1+8m})$ where we took the $+$ sign to make $n$ positive. So to check if it is of this form, just see if $1+8m$ is a perfect square.

0
On

Let $n$ be the number in question. It's a sum of consecutive natural numbers starting at $1$ if and only if $2n$ is not a perfect square, and

$$\left\lfloor\sqrt{2n}\right\rfloor\cdot\left\lceil\sqrt{2n}\right\rceil=2n\;.$$

This is because $\sum\limits_{k=1}^mk=\frac{n(n+1)}2$, so $n=\left\lfloor\sqrt{2n}\right\rfloor$, and $n+1=\left\lceil\sqrt{2n}\right\rceil$.

0
On

$x=55$ for example.

First, multiply x by 2, you get $y=110$.

Then, find $z = floor(sqrt(y)) = 10$.

Finally, if $z*(z+1)==y$, you say $x$ is a sum of consecutive natural numbers.

This is because $|\sqrt{n*(n+1)}-n|<1$ for almost every $n$.

4
On

If you require that your sums include $1$ as a summand, then you're talking about triangular numbers. The sum of the natural numbers from $1$ to $n$ will be $$\frac{n(n+1)}2,$$ so determining if a number has this form amounts to solving a quadratic equation.


If you don't require that $1$ be among the summed natural numbers, then for natural numbers $k<n$ we can see that the sum of the natural numbers from $k+1$ to $n$ is $$\frac{n(n+1)}2-\frac{k(k+1)}2=\frac{n^2-k^2+n-k}2=\frac{(n+k)(n-k)+n-k}2=\frac{(n+k+1)(n-k)}2.$$ $54$ actually works as a number of the latter type, for example by putting $n=10$ and $k=1$ (there are other ways, too). In fact, every natural number except those of the form $2^m$ for some non-negative integer $m$ can be represented in this fashion where $k+1<n$.

[Thanks to André Nicolas for mentioning the result, and grudging thanks ;-) to Daniel Fischer for breaking the proof wide open immediately.]

It's clear that such powers of $2$ won't work when $k+1<n,$ for then $n-k>1$ and exactly one of $n+k+1,n-k$ is odd, necessarily greater than $1,$ so $$\frac{(n+k+1)(n-k)}2$$ is not a non-negative integer power of $2$.

On the other hand, suppose $m$ is a natural number that is not a non-negative integer power of $2,$ so that there is some odd prime $p$ dividing it, say $m=jp$. Now, putting $k=j-\frac{p+1}2$ and $n=j+\frac{p-1}2,$ we have that $n+k+1=2j$ and $n-k=p,$ so that $$\frac{(n+k+1)(n-k)}2=\frac{2jp}2=jp=m.$$ It is readily shown that $k+1<n$ and that $k,$ are natural numbers, at which point the proof is complete.

0
On

It doesn't fail for 54... 17+18+19 = 54.