Problem: Let $a_1$, $a_2$, $a_3$, \dots be a sequence of integers,
with the property that every consecutive group of $a_i$'s
averages to a perfect square.
More precisely, for all positive integers $n$ and $k$, the quantity
 \[ \frac{a_n + a_{n+1} + \dots + a_{n+k-1}}{k} \]
is always the square of an integer.
Prove that the sequence must be constant
(all $a_i$ are equal to the same perfect square).

Solution: Let $\nu_p(n)$ denote the largest exponent of $p$ dividing $n$.
The problem follows from the following proposition.

\begin{proposition*}
  Let $(a_n)$ be a sequence of integers and let $p$ be a prime.
  Suppose that every consecutive group of $a_i$'s
  with length at most $p$ averages to a perfect square.
  Then $\nu_p(a_i)$ is independent of $i$.
\end{proposition*}

We proceed by induction on the smallest value of $\nu_p(a_i)$ as $i$ ranges
(which must be even, as each of the $a_i$ are themselves a square).
First we prove two claims.

\begin{claim*}
  If $j \equiv k \pmod p$ then $a_j \equiv a_k \pmod{p}$.
\end{claim*}
\begin{proof}
  Taking groups of length $p$ in our given,
  we find that $p \mid a_j +\dots + a_{j+p-1}$
  and $p \mid a_{j+1} + \dots + a_{j+p}$ for any $j$.
  So $a_j \equiv a_{j+p} \pmod p$ and the conclusion follows.
\end{proof}

\begin{claim*}
  If some $a_i$ is divisible by $p$ then all of them are.
\end{claim*}
\begin{proof}
  The case $p=2$ is trivial so assume $p \ge 3$.
  Without loss of generality (via shifting indices)
  assume that $a_1 \equiv 0 \pmod{p}$, and define
  \[ S_n = a_1 + a_2 + \dots + a_n \equiv a_2 + \dots + a_n \pmod{p}. \]
  Call an integer $k$ with $2 \le k < p$ a
  \textbf{pivot} if $1-k^{-1}$ is a quadratic nonresidue modulo $p$.

  We claim that for any pivot $k$, $S_k \equiv 0 \pmod{p}$.
  If not, then
  \[ \frac{a_1 + a_2 + \dots + a_k}{k}
    \text{ and } \frac{a_2 + \dots + a_k}{k-1} \]
  are both qudaratic residues.
  Division implies that $\frac{k-1}{k} = 1 - k\inv$ is a
  quadratic residue, contradiction.

  Next we claim that there is an integer $m$
  with $S_m \equiv S_{m+1} \equiv 0 \pmod{p}$,
  which implies $p \mid a_{m+1}$.
  If $2$ is a pivot, then we simply take $m=1$.
  Otherwise, there are $\frac12(p-1)$ pivots,
  one for each nonresidue (which includes neither $0$ nor $1$),
  and all pivots lie in $[3,p-1]$,
  so we can find an $m$ such that $m$ and $m+1$ are both pivots.

  Repeating this procedure starting with $a_{m+1}$
  shows that $a_{2m+1}, a_{3m+1}, \dots$ must all be divisible by $p$.
  Combined with the first claim and the fact that $m < p$,
  we find that all the $a_i$ are divisible by $p$.
\end{proof}

The second claim establishes the base case of our induction.
Now assume all $a_i$ are divisible by $p$ and hence $p^2$.
Then all the averages in our proposition (with length at most $p$)
are divisible by $p$ and hence $p^2$.
Thus the map $a_i \mapsto \frac{1}{p^2} a_i$ gives a new sequence
satisfying the proposition, and our inductive hypothesis completes the proof.

\begin{remark*}
  There is a subtle bug that arises
  if one omits the condition that $k \le p$ in the proposition.
  When $k = p^2$ the average $\frac{a_1 + \dots + a_{p^2}}{p^2}$
  is not necessarily divisible by $p$
  even if all the $a_i$ are.
  Hence it is not valid to divide through by $p$.
  This is why the condition $k \le p$ was added.
\end{remark*}