In how many ways can a number be expressed as a sum of consecutive numbers?

Here's one more way to calculate this, from my answer to this question on codegolf.SE:*

An integer $n$ is expressible as the sum of $m$ consecutive positive integers if and only if either:

  • $m$ is odd and $\frac nm$ is an integer, or
  • $m$ is even and $\frac nm + \frac12$ is an integer,

and $\frac nm \ge \frac m2$ (or else some of the integers in the sum would be zero or negative).

These conditions follow from the fact that the sum of an arithmetically increasing sequence of $m$ numbers equals $m$ times the mean of the numbers.

The last condition can be rewritten as $m \le \sqrt{2n}$. Thus, it's sufficient to iterate over all integers $m$ from $1$ to $\lfloor \sqrt{2n} \rfloor$ and check whether $\frac nm + \frac m2 + \frac12$ is an integer.


*) The entire Q&A thread has since been deleted; here's an archive.org copy for anyone curious.


A sum of consecutive numbers is a difference of triangular numbers. The paper below gives a solution for the case of nonconsecutive triangular numbers.

Nyblom, M. A. On the representation of the integers as a difference of nonconsecutive triangular numbers. Fibonacci Quart. 39 (2001), no. 3, 256–263.

The main result is that the number of distinct representations of a nonzero integer $m$ as a difference of nonconsecutive triangular numbers is given by $d−1$, where $d$ is the number of odd divisors of $m$.


Fix $k$. Is there a way that a number $N$ can be written in more than one way as a sum of $k$ consecutive number? Certainly not because $$ a+(a+1)+\cdots+(a+k-1)\neq b+(b+1)+\cdots+(b+k-1) $$ if $a\neq b$. On the other hand $N$ is the sum of $k$ consecutive number if and only if $N$ is the form $$ N=\frac12\left[(n+k)(n+k+1)-n(n+1)\right] $$ for some $n$. Does that help?