A problem about the sum of the squares of integers

What a lovely result to spot!

This result is equivalent to the fact that the set of numbers of the form $F(k)=k(k+1)(2k+1)$ for $1\le k\le 2^m$ takes every even residue of $2^m$ precisely twice. This can be proved inductively.

Suppose the result to be true for a given value of $m$ and let $1\le k\le 2^m$. Then $$F(k+2^m)\equiv F(k)+2^m \text{ (mod }2^{m+1})$$

In this way every residue for $2^m$ gives rise to a pair of distinct residues for $2^{m+1}$ each congruent to the original residue modulo $2^m$.


We observe $\sum_{k=1}^{2^{m+1}}k^2\equiv 0 \mod 2^m$ and $\sum_{k=1}^{2^{m+1}}k^2\equiv 2^m \mod 2^{m+1}$.

By induction we get that $a_1,\dots,a_{2^{m+2}}$ sorts into 4 elements for each residue class modulo $2^m$. With $a_{k+2^{m+1}}\equiv a_k+ a_{2^m+1}\equiv a_k+ 2^m$ it follows that the 4 elements modulo $2^m$ splits into two residue classes modulo $2^{m+1}$ each containing two elements.