Back to Thomas's Home Page

Sum of consecutive squares equal to a square

This article only contains results with few proofs. All arguments can be made with basic number theory, with a little knowledge.

Formatted with MathJax.

The Question

For what values of $n$ does there exist $n$ consecutive perfect squares that add up to a perfect square?

This reduces to a Pell-like equation:

$$ \tag{1} x^2 - n y^2 = \frac{n(n-1)(n+1)}{3}$$

With the added parity condition:

$$2 \mid (y-n+1)$$

The value of $y$ corresponds to twice the midpoint of the sequence $m,m+1,...,m+(n-1)$ whose squares add up to a square. (If $y$ and $n$ are the same parity, then a solution to (1) corresponds to a sequence of $n$ consecutive perfect odd squares that add up to a square.)

When $n$ is a perfect square

When $n$ is a perfect square, there is a solution if and only if $gcd(n,6) = 1$.

Given $n$ a perfect square with $gcd(n,6)=1$, we can find a solution $y=(n^{2}-49)/24$ which results in the first term of our sequence being: $$m = (n+1)(n-25)/48$$

When $n$ is not a square

One of the nice properties of Pell-like equations is that if you have solutions to two Pell-like equations:

$$u^{2}-nv^{2} = A$$ $$w^{2}-nz^{2} = B$$

Then we can find a solution to:

$$s^{2}-nt^{2} = AB$$

This is convenient in our case because there is a very easy solution to:

$$u^{2}-nv^{2} = n(n-1)$$

Namely, $(u,v)=(n,1)$.

This means that if $\frac{n+1}{3}$ is an integer, and we can find a solution to:

$$w^{2}-nz^{2} = \frac{n+1}{3}$$

We'd have a solution to (1). Solving for n, we get:

$$n = \frac{3w^{2}-1}{3z^{2}+1}$$

Whenever you have a representation of $n$ in this form, you can find a solution to (1), and it is not hard to show that the solution also satisfies the parity condition. (In fact, $y=w$ in the solution.)

The case where $z=0$ gives us the simplest infinite set of non-square values of $n$ , namely, $n = 3w^{2}-1$ .

The smallest $n$ of this form which cannot be represented with $z=0$ is 23, with $(w,z)=(10,2)$.

Interestingly, when $z>0$ , we find: $$n' = 3z^{2}n$$ also has a solution to (1), but the solution only satisfies the parity condition if $w$ is odd (that is, when $n$ was even.) For example, with $n=2$, then $\frac{n+1}{3} = 1$, and we can take any solution to: $$w^{2}-2z^{2}=1$$

which has infinite solutions with $z>0$, and get a solution: $$n^\prime = 6z^{2}$$

A similar, but more complicated complement when z>0 is:

$$n^{\prime\prime} = 3(w\pm z)^{2} - n$$

But in these cases, the solution to (1) only satisfies the parity condition if $w$ is even (alternatively, if n is odd.)

For example:

$$n = 23 = \frac {3w^{2}-1}{3z^{2}+1}$$ $$w=10, z=2.$$ $$n^{\prime\prime} = 3(w+ z)^{2} - n = 409$$ or $$n^{\prime\prime} = 3(w- z)^{2} - n = 169$$ The latter is actually a perfect square, so we have already covered it.

Where do these complements come from?

In fact, we have trivial solutions to these two Pell-like equations: $$r^{2}-ns^{2} = -n$$ $$r^{2}-ns^{2} = 1-n$$

Namely:

$$(r,s)=(0,1)$$ $$(r,s)=(1,1)$$

respectively. So, once we have a solution to: $$w^{2}-nz^{2} = \frac{n+1}{3}$$

We can multiply by $-n$ on both sides to get a solution to the equation:

$$ \tag*{(*)} r^{2}-ns^{2} = -\frac{n(n+1)}{3}$$ $$(r,s)=(nz,w)$$

But if we rewrite (*) as a quadratic equation in $n$, we get: $$n^{2} - (3s^{2}-1)n + 3r^{2} = 0 $$

Since we have one integer solution to this monic integer quadratic equation, we know that there has to be another solution, and

$$n' = \frac{3r^2}{n} = 3z^2n$$

is that other solution. At this point you need to show that the resulting solution to (1) satifies the parity condition when $w$ is odd, but that's not hard.

Similarly, for $n^{\prime\prime}$ , we first find a solution to:

$$\tag*{(**)} r^{2}-ns^{2} = -\frac{(n-1)(n+1)}{3}$$ $$(r,s)=(w+nz,w+z)$$

and again rewrite it as a monic equation in $n$ and find the complement solution to that equation.

That's why there are two "complement" values - there's one dual for each of the equations.

There are values of $r$ and $s$ for which (*) or (**) have integer solutions, neither of which is of the form $\frac{3w^{2}-1}{3z^{2}+1}$.

For example, when $n=50$, there is no solution to:$$w^2-50z^2=17$$ (since $2$ is not a square mod $5$) but there is a solution to $$r^2-50s^2 = -50\cdot 17$$namely, $(r,s)=(40,7)$.

Solutions to the sub-equation $r^2-ns^2 = -\frac{n(n+1)}{3}$

If $$r^2-ns^2 = -\frac{n(n+1)}{3}$$ then $$n = \frac{-(3s^2-1)\pm\sqrt{(3s^2-1)^2-12r^2}}{2}$$ So we need $(3s^2-1)^2-3(2r)^2$ to be a perfect square. Since $\mathbb Z[\sqrt{3}]$ is a UFD, this means that $3s^2-1 + 2r\sqrt{3} = c(a+b\sqrt{3})^2$ for some $a,b,c$. Equating, we see that $3s^2-1 = c(a^2+3b^2)$ and $r=abc$.

So, if $$c=\frac{3d^2-1}{a^2+3b^2}$$ is an integer then letting $(r,s)=(abc,d)$, the equation: $$0=n^2-(3s^2-1)n + 3r^2 = n^2 - c(a^2+3b^2)n + 3a^2b^2c^2 = (n-a^2c)(n-3b^2c)$$ has solutions $n=a^2c$ and $n=3b^2c$. Letting $(a,b,c,d)=(5,2,2,5)$ yields $n=50$ and $n=24$, for example. A quick check can show that $b$ must be even and $a$ must be odd, which shows that $y=r+s$ satisfies the parity conditon for $n=a^2c$ all the time, while $y=r+s$ satisfies the parity condition for $n=3b^2c$ only when $d$ was odd.

I believe this accounts for all solutions to this sub-equation.

Solutions to sub-equation $r^2-ns^2 = -\frac{(n-1)(n+1)}{3}$

TODO

If $$r^2-ns^2 = -\frac{(n-1)(n+1)}{3}$$ then: $$n^2 - (3s^2)n + (3r^2-1) = 0$$

This equation has integer solutions if and only if $9s^4-12r^2 + 4$ is a perfect square. The case $s=r$ is trivially true, which corresponds to the already-covered case, $n=3s^2-1$ and the trivial case $n=1$.

More generally, solving: $$9s^4-12r^2 + 4 = D^2$$ we can subtract $9r^4-12r^2+4=(3r^2-2)^2$ from both sides and get: $$9(s-r)(s+r)(s^2+r^2) = (D-3r^2+2)(D+3r^2-2)$$

I'm not sure where to go from here.

There are examples where $n\equiv -1\pmod 3$ and there is no solution to $w^2-nz^2=(n+1)/3$. For example, $n=443$, there is no solution to: $$w^2-443z^2=444/3 = 4\cdot 37$$

On the other hand, $$386^2-443\cdot 22^2 = -\frac{442\cdot 444}{3}$$

Pure solutions

There are solutions that do not come from either of the above sub-equations. For example, $n=33$ can only arise from the first sub-equation, with $b=1$. So we get the equation: $$33=3\frac{3d^2-1}{a^2+3}$$ which amounts to solving $3d^2-11a^2=34$. But that would mean $a^2-d^2\equiv 2 \pmod 4$, which isn't possible - if $a-d$ is even then $a+d$ is even.

So, while the sub-equations are useful for finding lots of values of $n$, they are not enough.

Necessary Conditions

Necessary conditions when $n$ is not a square:

A. Factor $n=a^{2}b$, with $b$ square-free, then:
  1. If $2 \mid a$, then $2 \mid b$
  2. If $3 \mid a$, then $3 \mid b$
  3. $3$ is a square $\pmod{b}$. (Alternatively, the only primes that divide $b$ are $2$, $3$, and primes of the form $12k \pm 1$.)
  4. If $3 \mid b$, then $b \equiv 6 \pmod{9}$
B.
  • If $3 \mid n+1$, then $\frac{n+1}{3}$ is the sum of two perfect squares
  • If $3 \nmid n+1$, then $n+1$ is the sum of two perfect squares

    There are, however, values of $n$ which match all these conditions but do not have a solution. The smallest such $n$ is 842. (And yes, it is interesting that $842=29^{2}+1$ , and $841 = 20^{2}+21^{2}$ .)

    Note that the necessary conditions apply when $n$ is a perfect square. (B) is trivially true, since $n+1$ is never divisible by three and is obviously the sum of squares. (A.1) and (A.2) coincides with the restriction that $n$ be relatively prime to 6, and (A.3) and (A.4) are true because $b=1$ . So these necessary conditions are sufficient in the case where $n$ is a perfect square.

    General Arithmetic Sequences

    Extension question: For what $n$ can we find an arithmetic sequence of length $n$ such that the sum of the squares of the values is a perfect square?

    In other words, for what $n$ does there exist integers $m$ and $d>0$ such that: $$\sum_{i=0}^{n-1} (m+id)^2$$ is a perfect square.

    In this case, the Pell-like equation is:

    $$ \tag{2} x^{2}-ny^{2} = d^{2}\frac{(n-1)n(n+1)}{3}$$

    Where $d$ is the common difference of the arithmetic sequence.

    We also need $y$ to have the same parity as $d(n-1)$ , but the parity condition is less relevant, since if we have a solution to (2) without the parity condition, we can multiple both sides by $4$ and get a solution with the parity condition: $$(2x)^{2}-n(2y)^{2} = (2d)^{2}\frac{(n-1)n(n+1)}{3}$$

    When $n$ is a perfect square

    When $n$ is a perfect square, we can always find a solution to (2), and, in particular, we can find a solution with $d=gcd(n,6)$.

    For example, when $n=4$:

    $$6^{2} = (-1)^{2}+1^{2}+3^{2}+5^{2}$$

    With $n=9$:

    $$24^{2} = (-10)^{2} + (-7)^{2} + (-4)^{2} + (-1)^{2} + 2^{2} + 5^{2} + 8^{2} + 11^{2} + 14^{2}$$

    For a general square, $n$, you can start with: $$m = \frac{(3n+2)(\sqrt{n}-1)}{2} + 6$$ $$d = 6$$

    This general solution has the advantage that the first term, $m$, is positive for all squares $n$ .

    When $n$ is not a square

    Some of these necessary conditions will seem familiar:

    A'. Factor $n=a^{2}b$, with $b$ square-free, then:
    1. If $3 \nmid b(n+1)$, then $b \equiv 1 \pmod{3}$
    2. If $3 \mid b$, then $b \equiv 6 \pmod{9}$
    3. $3$ is a square $\pmod{b}$. (Alternatively, the only primes that divide $b$ are $2$, $3$, and primes of the form $12k \pm 1$.)
    B.
    1. If $3 \mid n+1$, then $\frac{n+1}{3}$ is the sum of two perfect squares
    2. If $3 \nmid n+1$, then $n+1$ is the sum of two perfect squares

    So the only change is that we've replaced (A.1) and (A.2) with (A'.1.)

    (A'.1) is trivially true if $3 \nmid a$, since then $n=a^2b \equiv b \pmod{3}$, so $n(n+1) \equiv b(n+1) \not\equiv 0 \pmod{3}$. But one of $n$, $n-1$, and $n+1$ must be divisible by $3$, so $n-1$ must be divisible by $3$, and hence $n \equiv 1 \pmod{3}$, and hence $b \equiv 1 \pmod{3}$. So we could rewrite (A'.1) as:

    If $3 \mid a$ then $b \not\equiv 2 \pmod{3}$.

    We'll see later that (A') and (B) are sufficient conditions.

    A necessary and sufficient condition that is easier to prove is that we be able to write:

    $$\tag{3} n = \frac{u^{2}-3w^{2}}{v^{2}+3w^{2}}$$

    for some integers $(u,v,w)$.

    This formula can be re-written as:

    $$\tag{3'} u^{2}-nv^{2} = 3(n+1)w^{2}$$

    Alternatively:

    $$\tag{3''} u^{2} = nv^{2}+3(n+1)w^{2}$$

    You can get from (2) to (3') and back by using the fact that you can write $n(n-1)$ trivially in the form $r^{2}-ns^{2}$ , namely with $(r,s)=(n,1)$. So you can multiply (2) by $9n(n-1)$ and re-order to get a solution for (3') with w=n(n-1)d.

    You get from (3') to (2) by again multiplying both sides of (3') by $n(n-1)$ to get a solution for (2) with d=3w.

    For $n$ in the form (3), we can find an $m$ explicitly when $d=6w$ as: $$m=(v-3w)n + u + 3w$$

    (This obviously doesn't work when w=0, so we have to find a different solution when $n$ is a perfect square.)

    We can add the condition that $gcd(v,w)=1$ because if they have a common prime factor, then that factor must divide the denominator of (3), and hence the numerator of (3) and hence $u$ as well, and therefore we can remove that factor.

    If we add one to both sides of (3), we get:

    $$n+1 = \frac{u^{2}+v^{2}}{v^{2}+3w^{2}}$$

    This can be seen as the "source" of the conditions in (B).

    Equation $(3^{\prime\prime})$ can be seen as looking for rational solutions $(X,Y)$ to the equation:

    $$nX^{2} + 3(n+1)Y^{2} = 1$$

    The Hasse-Minkowski theorem gives us the tool for determining when this equation has rational solutions, and the result coincides with (A') and (B), so tht (A') and (B) are sufficient.

    Alternatively, see: pp. 238-242 of Harvey Cohn: Advanced Number Theory.

    For specific pairs $(X,Y)$ , you get some of the classes of solutions we have seen. For example, with $(X,Y)=(1,0)$ , we get a solution when $n$ is a perfect square. With $(X,Y)=(0,1)$ , there is a solution when ${n=3z^2-1}$ for some $z$.

    The non-squares which satisfy these rules but not the original rules for consecutive integers start with:

    52  (m=1003,d=2)
    148 (m=-389, d=6)
    244 (m=8057, d=6)
    276 (m=-241, d=2)
    292 (u=104 v=5, w=2)
    388 (u=12784, v=649, w=2)
    528 (u=552, v=23, w=4)
    564 (u=444, v=7, w-10, m=--2231 d=10)
    592 (u=208 v=5, w=4)
    628 (u=484, v=19, w=2)
    708 (u=96,v=1, w=2)
    772 (u=71824, v= 2585, w=2)
    792 (m=-2296, d=6)
    832 (u=448, v=7, w=8)
    842 (u=939, v=30, w=7, m=1110, d=7) 
    852 (m=105,d=2)
    964 (u=112, v=1, w=2)
    976 (u=20464, v=655, w=4)
    996 (u=192, v=5, w=2)
    

    The case of $n=842$ was the example from the first section which satisfied (A) and (B) but for which no integer solution of (1) can be found. Turns out, it requires d=7.

    OEIS Links

    The Online Encyclopedia of Integer Sequences has a lot of sequences related to this sequence.

    A134419
    The set of $n$ for which there is a solution to (1), whether matching the parity condition or not.
    A001032
    Numbers $n$ for which there are consecutive positive integers whose squares add to a square. If you take out the positive requirement, you are only adding $n=25.$
    A001033
    Numbers $n$ for which there are consecutive positive odd integers whose squares add to a square. These correspond to solutions of (1) which do not satisfy the parity condition, excepting $n=4$, which fails on the positivity condition.
    A186699
    The values $n$ for which there is an arithmetic sequence of squares adding to a square. I added this one to the OEIS.
    Thomas Andrews (other@thomasoandrews.com.)
    Copyright 2011.