Formulas
The sum of consecutive integers from n
to m
is
$\frac{ (n + m) (m - n + 1) }{ 2 } = \frac{ m^2 - n^2 + m + n }{ 2 }$
So we want to count
$\# \{ (n,m) \in \mathbb{N}^2: m^2 - n^2 + m + n = 2k \} = \text{CNS}(k)$
Isolating n
,
$n^2 - n - m^2 -m + 2k = 0$
$n = \frac{ 1 + \sqrt{4m^2 + 4m - 8k + 1} }{2}$
Since $n$ must be integer, this means $4m^2 + 4m - 8k + 1$ must be a perfect square not divisible by 4.
$4m^2 + 4m - 8k + 1 = (2m + 1)^2 - 8k$
$4m^2 + 4m - 8k + 1 = s^2$, with $s$ odd.
$m = \frac{ -1 + \sqrt{8k + s^2} }{2}$
Again, $8k + s^2$ must be a perfect square: $8k + s^2 = (s+r)^2$.
Also, $8k + s^2$ must not be divisible by 4, granted because $s$ odd.
We can consider $s$ to be positive because $-s$ and $s$ give the same solution, and $0$ is divisible by 4. We can also consider $r$ to be positive.
$8k + s^2 = s^2 + 2sr + r^2$
$8k = 2sr + r^2$
$s = \frac{ 4k }{r} - \frac{r}{2}$
We will have a solution for every $r$ such that
- $r$ is even.
- $r$ must divide $4*k$.
- $\frac{ 4k }{r} \geq \frac{r}{2} \iff r^2 \leq 8k \iff r \leq 2 \sqrt{2k}$
- For $s$ to be odd, either $r$ not divisible by 4 or $\frac{ 4k }{r}$ odd (e.g. $k$ odd).
- Case $r = 2$ gives a summation of a single term and is not counted.
Case $k = 45$
So, for $\text{CNS}(45)$, since $4 * 45 = 2^2 * 3^2 * 5$ and $\lfloor 2 \sqrt{2*45} \rfloor = 18$
- $r = 2 \implies s = 89 \implies m = 45 \implies n = 45$, ignored.
- $r = 2^2 = 4 \implies s = 43 \implies m = 23 \implies n = 22$
- $r = 2*3 = 6 \implies s = 27 \implies m = 16 \implies n = 14$
- $r = 2*5 = 10 \implies s = 13 \implies m = 11 \implies n = 7$
- $r = 2^2*3 = 12 \implies s = 9 \implies m = 10 \implies n = 5$
- $r = 2*3^2 = 18 \implies s = 1 \implies m = 9 \implies n = 1$
- The following would be $r = 2^2*5 = 20 > 18$. End.
That's why $\text{CNS}(45) = 5$.
Case $k = 945$
Since $4 * 945 = 2^2 * 3^3 * 5 * 7$ and $\lfloor 2 \sqrt{2*945} \rfloor = 86$,
- $r = 2 \implies s = 1889 \implies m = 945 \implies n = 945$, ignored.
- $r = 2^2 = 4 \implies s = 943 \implies m = 473 \implies n = 472$
- $r = 2*3 = 6 \implies s = 627 \implies m = 316 \implies n = 314$
- $r = 2*5 = 10 \implies s = 373 \implies m = 191 \implies n = 187$
- $r = 2^2*3 = 12 \implies s = 309 \implies m = 160 \implies n = 155$
- $r = 2*7 = 14 \implies s = 263 \implies m = 138 \implies n = 132$
- $r = 2*3^2 = 18 \implies s = 201 \implies m = 109 \implies n = 101$
- $r = 2^2*5 = 20 \implies s = 179 \implies m = 99 \implies n = 90$
- $r = 2^2*7 = 28 \implies s = 121 \implies m = 74 \implies n = 61$
- $r = 2*3*5 = 30 \implies s = 111 \implies m = 70 \implies n = 56$
- $r = 2^2*3^2 = 36 \implies s = 87 \implies m = 61 \implies n = 44$
- $r = 2*3*7 = 42 \implies s = 69 \implies m = 55 \implies n = 35$
- $r = 2*3^2*5 = 42 \implies s = 69 \implies m = 55 \implies n = 35$
- $r = 2^2*3*5 = 60 \implies s = 33 \implies m = 46 \implies n = 17$
- $r = 2*5*7 = 70 \implies s = 19 \implies m = 44 \implies n = 10$
- $r = 2^2*3*7 = 84 \implies s = 3 \implies m = 43 \implies n = 2$
- The following would be $r = 2*3^2*5 = 90 > 87$. End.
Therefore,
$\text{CNS}(945) = 15$.
Case $k = 947$
Since $4 * 947 = 2^2 * 947$ and $\lfloor 2 \sqrt{2*945} \rfloor = 87$,
- $r = 2 \implies s = 1893 \implies m = 947 \implies n = 947$, ignored.
- $r = 2^2 = 4 \implies s = 945 \implies m = 474 \implies n = 473$
- The following would be $r = 2*947 = 1894 > 87$. End.
Therefore,
$\text{CNS}(947) = 1$.
In general
Let $k = 2^{a_0} {p_1}^{a_1} {p_2}^{a_2} \dots {p_l}^{a_l}$, where $2, p_1, p_2, \dots, p_l$ are $l+1$ different prime numbers.
$\text{CNS}(k) = \# \left\{ 2^{b_0} {p_1}^{b_1} \dots {p_l}^{b_l} :
\begin{array}{c}
b_0 \in \{ 1, a_0+2\} \\
0 \leq b_i \leq a_i \quad \forall i=1,\dots,l \\
2^{b_0-1} {p_1}^{b_1} \dots {p_l}^{a_l} \leq \sqrt{2*k}
\end{array}
\right\}$
The last constraint is annoying. But if we remove it we will get the double of solutions, so we can just divide by 2.
Therefore, the closed formula is
$\text{CNS}(k) = \frac{ 2 (a_1+1) \dots (a_l+1)}{2} - 1 = (a_1+1) \dots (a_l+1) - 1$
The $-1$ at the end is because we ignore summations of only one term.
45 = 45
does not count as a 6th possibility? $\endgroup$