In how many ways can a number be expressed as a sum of consecutive numbers? [duplicate]
All the positive numbers can be expressed as a sum of one, two or more consecutive positive integers. For example $9$ can be expressed in three such ways, $2+3+4$, $4+5$ or simply $9$. In how many ways can a number be expressed as a sum of consecutive numbers?
In how many ways can this work for $65$?
Here, for $9$ answer is $3$, for $10$ answer is $3$, for $11$ answer is $2$.
Solution 1:
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.
Solution 2:
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$.
Solution 3:
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?
Solution 4:
Factorise the number and find the number of odd factors . Total number of odd factors (except 1) is the answer.
Express N in terms of prime factors
$N = a^p . b^q . c^r$
If a = 2 . Number of odd factors = (q+1)(r+1) - 1 . Note : 1 is subtracted because 1 cannot be answer as consecutive terms means greater than 1 term.
For eg.
$100 = 2^2 . 5^2 $
So Number of odd factors = (2+1) - 1 = 2 = Number of ways of writing 100 as sum of 2 or more consecutive integers .
They are
18, 19, 20, 21, 22
9,10,11,12,13,14,15,16
ANSWER:
Number of ways of writing N as sum of consecutive positive integers is Number of odd factors in that number (except 1).
Also see : http://mathblag.wordpress.com/2011/11/13/sums-of-consecutive-integers/