Complete square of summation of odd numbers

number theory

Suppose we have an integer(which may be odd or even), let us say for example $x=20$, also we have an odd number which is also known (let us say for example $y=39$).
Is there any formula that tell us how many consecutive odd numbers starting from $y$ that if we add them together with $x$, then we have a result of perfect square.
In our case, our perfect square will be : $perfect=100$, and it is $perfect=20+39+41$.
so the answer will be $2$ odd numbers (include the starting one).
any suggestions ?

Best Answer

As Ross Millikan's answer states, you're looking for an $n \ge 1$ such that, for some integer $a$, you have

$$\begin{equation}\begin{aligned} n(y+n-1)+x & = a^2 \\ n^2 + (y - 1)n + x & = a^2 \\ n^2 + (y - 1)n + x - a^2 & = 0 \end{aligned}\end{equation}\tag{1}\label{eq1A}$$

This is a quadratic equation in $n$. To get positive integral values for $n$ requires, at a minimum, the discriminant is a perfect square, say of $b$. This gives

$$\begin{equation}\begin{aligned} (y-1)^2 - 4(x - a^2) & = b^2 \\ (y-1)^2 - 4x + 4a^2 & = b^2 \\ (y-1)^2 - 4x & = b^2 - (2a)^2 \\ (y-1)^2 - 4x & = (b - 2a)(b + 2a) \end{aligned}\end{equation}\tag{2}\label{eq2A}$$

Call the left hand side value $c$, i.e.,

$$c = (y-1)^2 - 4x \tag{3}\label{eq3A}$$

Thus, you have for factors $f_1$ and $f_2$ of $c$ that

$$b - 2a = f_1 \tag{4}\label{eq4A}$$

$$b + 2a = f_2 \tag{5}\label{eq5A}$$

where $f_1 f_2 = c$. Solving these equations gives that \eqref{eq5A} minus \eqref{eq4A} has $4a = f_2 - f_1 \implies a = \frac{f_2 - f_1}{4}$. Also, adding the equations gives $2b = f_1 + f_2 \implies b = \frac{f_1 + f_2}{2}$. In particular, this shows that $f_2 - f_1$ must be a multiple of $4$, so you only need to check those factors.

The full quadratic formula equation for $n$ from \eqref{eq1A}, and using \eqref{eq2A}, is

$$n = \frac{1 - y \pm |b|}{2} \tag{6}\label{eq6A}$$

Since you are looking for positive solutions, you want to use only $n = \frac{1 - y + |b|}{2}$, and just the smallest one which is positive. Thus, you want to have the least value of $|b|$ which is greater than $y - 1$ and with the same parity.

This technique is most useful when $c$ in \eqref{eq3A} is relatively small, but with $y$ and $x$ potentially being quite large, since factoring can be a quite expensive operation for large values. For cases where $c$ is quite large, I don't know if there is any other way easier than perhaps just checking the next values until you get a perfect square.

Related Question