\documentclass[12pt,reqno]{article}

\usepackage[usenames]{color}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amscd}
\usepackage{graphicx}

\usepackage[colorlinks=true,
linkcolor=webgreen,
filecolor=webbrown,
citecolor=webgreen]{hyperref}

\definecolor{webgreen}{rgb}{0,.5,0}
\definecolor{webbrown}{rgb}{.6,0,0}

\usepackage{color}
\usepackage{fullpage}
\usepackage{float}

\usepackage{psfig}
\usepackage{graphics}
\usepackage{latexsym}
\usepackage{epsf}
\usepackage{breakurl}

\setlength{\textwidth}{6.5in}
\setlength{\oddsidemargin}{.1in}
\setlength{\evensidemargin}{.1in}
\setlength{\topmargin}{-.1in}
\setlength{\textheight}{8.4in}

\newcommand{\R}{\mathbb{R}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\DeclareMathOperator{\Var}{Var}
\newcommand{\seqnum}[1]{\href{https://oeis.org/#1}{\rm \underline{#1}}}

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{conjecture}[theorem]{Conjecture}

\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}


\usepackage{psfig}

\begin{document}

\begin{center}
\epsfxsize=4in
\leavevmode\epsffile{logo129.eps}
\end{center}


\hypersetup{pdftitle={An Equidistribution Result for Differences Associated with Square Pyramidal Numbers}, pdfauthor={Anji Dong, Katerina Saettone, Kendra Song, Alexandru Zaharescu}}

\begin{center}
\vskip 1cm{\LARGE\bf An Equidistribution Result for Differences \\[.1in] Associated with Square Pyramidal Numbers
}
\vskip 1cm
\large
Anji Dong, Katerina Saettone, Kendra Song\\
Department of Mathematics \\ 
University of Illinois Urbana-Champaign\\
Urbana, IL 61801\\ 
USA \\ 
\href{mailto:anjid2@illinois.edu}{\tt anjid2@illinois.edu}\\
\href{mailto:kas18@illinois.edu}{\tt kas18@illinois.edu} \\
\href{mailto:kendras4@illinois.edu}{\tt kendras4@illinois.edu}\\
\ \\
Alexandru Zaharescu\\
Department of Mathematics \\ 
University of Illinois Urbana-Champaign\\
Urbana, IL 61801\\ 
USA \\ 
and \\
Simion Stoilow Institute of Mathematics of the Romanian Academy\\
P.\ O.\ Box 1-764, RO-014700 Bucharest\\
Romania\\
\href{mailto:zaharesc@illinois.edu}{\tt zaharesc@illinois.edu}
\end{center}

\vskip .2 in

\begin{abstract}
    We provide an asymptotic formula for the average value of the sequence \seqnum{A351830}, whose terms are the absolute differences between square
pyramidal numbers and their nearest perfect squares.  Moreover, we supply asymptotic formulas for the $k$-th moment of this sequence of distances
to the nearest perfect squares, for any fixed positive integer $k$.
\end{abstract}

\section{Introduction}\label{introduction}

The classical cannonball problem asks when a square pyramidal number is a perfect square. Lucas \cite{question} posed this problem, and Watson     \cite{Watsoncannonball} proved that the only positive values of $n$ for which $1^2+2^2+\cdots+n^2$ is a square are  $n=1$ and $n=24$. 

There are many integer sequences associated with the cannonball problem; for example, see \seqnum{A001032}, \seqnum{A351830}, \seqnum{A350886}, \seqnum{A350887}, and \seqnum{A350888} in the On-Line Encyclopedia of Integer Sequences (OEIS) \cite{oeis}. In this paper, we establish additional properties of the sequence \seqnum{A351830}. This is given by
\begin{equation}\label{eq:an}
    a_{n} = \big\lvert P_{n} - y^{2}_{n} \big\rvert,
\end{equation}
where $P_{n}$ is the $n$-th square pyramidal number defined as
\begin{align}
   P_n &= \sum_{i = 1}^n i^2= \frac{2n^3+3n^2+n}{6},\label{eq:pyramidal} 
\end{align} 
and $y_n$ is the nonnegative integer for which $y_n^2$ is the closest square to $P_n$. The uniqueness of this choice is verified in Proposition~\ref{prop:distance}. We allow $n=0$ when displaying the sequence, with $P_0=a_0=y_0=0$, but all moments below start at $n=1$. Xausa \cite{paolo} tabulated the sequence through $n=10000$. The values for $0\leq n\leq 50$ are 
\begin{align*}
  &0,0,1,2,5,6,9,4,8,4,15,22,25,22,9,15,25,21,7,30,46,53,49,32,0,49,\\
  &40,41,30,91,46,12,9,15,4,26,77,114,25,91,61,105,15,122,129,66,22,1,1,24,76.
\end{align*}

 Geometrically, $a_n$ can be interpreted in terms of rearranging cannonballs. If $P_n>y_n^2$, then the $n$-th square pyramid contains more cannonballs than the nearest perfect square arrangement; after forming the square, exactly $a_n$ balls remain unused. Conversely, if $P_n<y_n^2$, then the nearest square arrangement exceeds the pyramidal number's count, so forming a square pyramid from the square arrangement leaves $a_n$ leftover cannonballs. Thus, $a_n$ measures the minimal unavoidable surplus while interchanging these two shapes.

Note that the only values of $n$ such that $a_{n} = 0$ are $0, 1$, and $24$, which correspond to the solutions to the cannonball problem. Moreover, as Conway and Sloane \cite{conway1982} noted, the solution corresponding to $n=24$ allows for the Lorentzian construction of the Leech lattice \cite[pp.~524--528]{conway1999}. Further applications and generalizations of the cannonball problem are discussed by Laub \cite{Laub1990}, Beeckmans \cite{beeckmans1994}, and Bennett \cite{bennett2002}. 

More generally, the problem of studying the distance between polynomial values and perfect squares has been considered by various authors. For example, V\^aj\^aitu and the fourth author studied integer points near elliptic curves $Y^2=X^3+\alpha X+\beta$ \cite{VaZa2001usually}, as well as integer points near hyperelliptic curves $Y^2=X^{2d+1}+P(X)$ \cite{VaZa2001hyper}, by investigating the differences $|Y^2-(X^3+\alpha X+\beta)|$ and $|Y^2-(X^{2d+1}+P(X))|$ for large $X$. In a related direction, Pink \cite{Pink2003} studied explicit lower bounds for the difference $|F(x)-by^m|$ under suitable hypotheses, where $F(x)$ is an integer polynomial of degree $n\geq 2$, $m\geq 2$, $|y|\geq 2$, $b\neq 0$, and $F(x)\neq by^m$.

For real $x \geq 1$, let $A(x)$ represent the normalized average of $(a_{n})$ for $1 \leq n \leq x$. More precisely,
\begin{equation}\label{eq:average}
A(x) := \frac{1}{x}\sum_{1 \leq n \leq x} a_{n}.
\end{equation}

For integer $x$, this is the arithmetic mean of the first $x$ distances; for real $x$, we retain the normalization by $x$ in \eqref{eq:average}. While the classical cannonball problem asks for exact coincidences between square pyramidal numbers and perfect squares, we also ask how close square pyramidal numbers are to squares on average. Thus, our first goal is to provide an asymptotic formula for $A(x)$. In this connection, small values of $a_n$ relative to the average show instances when one has an almost solution to the cannonball problem, in the sense that one can transform a square pyramid of cannonballs into a square, or the other way around, with a small surplus of cannonballs.
\begin{theorem}\label{thm:asymptotic}
For real $x\geq 1$, the function $A(x)$, given by \eqref{eq:average}, satisfies the asymptotic formula
\begin{equation}\label{eq:averageasymptotic}
    A(x) = \frac{1}{5\sqrt{3}}x^{3/2}+O(x^{5/4}),
\end{equation}
where the constant implied by the big $O$ symbol is absolute.
\end{theorem}
\begin{remark}
    The same argument applies, for example, to integer-valued cubic polynomials with positive leading coefficient, with the nearest square defined relative to the new polynomial.
\end{remark}
Next, we generalize Theorem \ref{thm:asymptotic} by considering higher moments of the sequence $(a_{n})$. For each $k\in\N$, let $M_{k}(x)$ represent the $k$-th moment of $(a_{n})_{n\leq x}$, that is,
\begin{equation}\label{eq:moments}
    M_{k}(x) := \sum_{1 \leq n \leq x}a_{n}^{k}.
\end{equation}
By Theorem \ref{thm:asymptotic},
\[
M_{1}(x) = xA(x) = \frac{1}{5\sqrt{3}}x^{5/2}+O(x^{9/4}).
\]
More generally, we have the following result. 
\begin{theorem}\label{thm:moments}
For any fixed positive integer $k$ and real $x\geq 1$,
\begin{equation}\label{eq:momentasymptotic}
    M_{k}(x)=  \frac{x^{\frac{3}{2}k + 1}}{3^{k/2}(\frac{3}{2}k + 1)(k+1)}+O_k\left(x^{\frac{3}{2}k+\frac{3}{4}}\right),
\end{equation}
 where $M_{k}$ is defined in \eqref{eq:moments}, and the constant implied by the $O_k$ symbol depends at most on $k$.  
\end{theorem}
\subsection{Structure of the paper}\label{subsec:structure}
The paper is organized as follows. We begin by introducing some standard notation in Section \ref{sec:notation}. In Section \ref{sec:auxiliary}, we provide some auxiliary results needed in the proofs of the main theorems. Lastly, in Section \ref{sec:proofs}, we complete the proofs of Theorems \ref{thm:asymptotic} and \ref{thm:moments}.

\section{General notation}\label{sec:notation}
We employ some standard notation used throughout the article. 
\begin{itemize} 
\item The expressions $f(X)=O(g(X))$, $f(X) \ll g(X)$, and $g(X) \gg f(X)$ are equivalent to the statement that $|f(X)| \leq C|g(X)|$ for all sufficiently large $X$, where $C>0$ is an absolute constant. A subscript of the form $\ll_{\alpha}$ means that the implied constant may depend on the parameter $\alpha$. Dependence on several parameters is indicated similarly, as in $\ll_{\alpha, \lambda}$.
\item Given $\alpha \in \mathbb{R}$, the notation $\|\alpha\|$ denotes the smallest distance of $\alpha$ to an integer.
\item Given a set $S$, the notation $|S|$ and $\#S$ both stand for the cardinality of $S$. 
\item For $x\in\R$, the notation $\lceil x\rceil$ denotes the ceiling of $x$, which is the smallest integer greater than or equal to $x$. The notation $\lfloor x\rfloor$ denotes the floor of $x$, which is the largest integer less than or equal to $x$. 
\item For real $x\in\R$, $\{x\}$ denotes the fractional part of $x$, that is, $x-\lfloor x\rfloor$.
\item The notation $e(x)$ stands for $\exp(2\pi i x)$.
\end{itemize}


\section{Auxiliary propositions}\label{sec:auxiliary}

Recall the sequence $(a_n)$ defined in
\eqref{eq:an}. We define a related sequence 
\begin{equation}
    d_n:=|\sqrt{P_n}-y_n|.\label{def:dn}
\end{equation}
In this section, we show that $d_n = \|\sqrt{P_n}\|$ and that the sequence $(d_n)$ is uniformly
distributed in $[0,1/2]$. We begin with the following lemma by Kuipers and Niederreiter \cite{UniformDistributionofSequences}. 

\begin{lemma}[{\cite[Thm.\ 2.7, p.\ 17]{UniformDistributionofSequences}}]
\label{lem:expsum}
Let $a<b$ be integers, and let $f$ be a real-valued twice continuously
differentiable function on $[a,b]$. Suppose that, for some $\rho>0$,
either $f''(t)\geq\rho$ for every $t\in[a,b]$ or
$f''(t)\leq-\rho$ for every $t\in[a,b]$. Then
\begin{equation}\label{eq:expsumbound}
 \left|\sum_{n=a}^{b}e(f(n))\right|
 \leq \bigl(|f'(b)-f'(a)|+2\bigr)
       \left(\frac{4}{\sqrt{\rho}}+3\right).
\end{equation}
\end{lemma}

Recall that a sequence $(u_n)$ in $[0,1)$ is \emph{uniformly distributed} if,
for every $0\leq\alpha<\beta\leq1$,
\[
 \lim_{N\to\infty}\frac{1}{N}
 \#\{n:1\leq n\leq N,\ \alpha\leq u_n<\beta\}
 =\beta-\alpha.
\]


\begin{lemma}[{\cite[Weyl's criterion, p.\ 1]{montgomery1994ten}}]
\label{lem:weyl}
A real sequence $(v_n)$ is uniformly distributed modulo one if and only if,
for every integer $\gamma\ne0$,
\[
 \lim_{N\to\infty}\frac{1}{N}\sum_{n=1}^{N}e(\gamma v_n)=0.
\]
\end{lemma}
Next, we show that $\{\sqrt{P_n}\}$ is uniformly distributed. To do so, we present a more general result.
\begin{proposition}\label{prop:cubic}
Let $q(t)=at^3+bt^2+ct+d$, where $a,b,c,d\in\R$ and $a>0$.
There is an integer $T\geq1$, depending only on $q$, such that $q(t)>0$
for $t\geq T$, and the sequence $(\sqrt{q(n)})_{n\geq T}$ is uniformly
distributed modulo one. In fact, for all integers $N\geq T$ and
$\gamma\in\Z\setminus\{0\}$,
\begin{equation}\label{eq:cubicexpsum}
 \left|\sum_{n=T}^{N}e(\gamma\sqrt{q(n)})\right|
 \ll_q |\gamma|^{1/2}N^{3/4}+|\gamma|N^{1/2}
       +|\gamma|^{-1/2}N^{1/4}+1.
\end{equation}
The implied constant is independent of $\gamma$ and $N$.
\end{proposition}

\begin{proof}
Since $a>0$, the polynomial $q(t)$ is positive on a sufficiently large
positive ray. On that ray let $h_q(t)=\sqrt{q(t)}$. Direct differentiation gives
\[
 h_q'(t)=\frac{q'(t)}{2\sqrt{q(t)}},\qquad
 h_q''(t)=\frac{2q(t)q''(t)-q'(t)^2}{4q(t)^{3/2}}.
\]
Equivalently, one may differentiate the convergent expansion in $1/t$ of
$\sqrt{a}\,t^{3/2}(1+b/(at)+c/(at^2)+d/(at^3))^{1/2}$.
Either way, one obtains
\begin{align}\label{eq:cubicderivatives}
 h_q'(t)&=\frac{3\sqrt a}{2}t^{1/2}+O_q(t^{-1/2}),\notag\\
 h_q''(t)&=\frac{3\sqrt a}{4}t^{-1/2}+O_q(t^{-3/2}),\\
 h_q'''(t)&=-\frac{3\sqrt a}{8}t^{-3/2}+O_q(t^{-5/2}).\notag
\end{align}
Thus we can choose an integer $T\geq1$ so that $q(t)>0$, $h_q''(t)>0$
and $h_q'''(t)<0$ for every $t\geq T$. In particular, $h_q''$ is decreasing
there and $h_q''(N)\gg_q N^{-1/2}$.

For $N>T$ and $\gamma\ne0$, apply Lemma~\ref{lem:expsum}
to $f=\gamma h_q$ on the integer interval $[T,N]$, with the positive parameter
\[
 \rho=|\gamma|h_q''(N).
\]
The lemma applies to either sign of $\gamma$, and
\[
 |f'(N)-f'(T)|\ll_q |\gamma|N^{1/2},\qquad
 \rho^{-1/2}\ll_q |\gamma|^{-1/2}N^{1/4}.
\]
By Lemma \ref{lem:expsum}, we obtain \eqref{eq:cubicexpsum}. For each fixed $\gamma$, the right side of
\eqref{eq:cubicexpsum} is $o(N)$. Since $N-T+1\sim N$, Weyl's criterion applied to the tail $(\sqrt{q(n)})_{n\geq T}$ finishes the proof.
\end{proof}
For $q(n)=P_n = n(n+1)(2n+1)/6$, Proposition~\ref{prop:cubic} gives, for
$N\geq1$ and $m\geq1$,
\begin{equation}\label{eq:pyramidalexpsum}
 \left|\sum_{n=1}^{N}e(m\sqrt{P_n})\right|
 \ll m^{1/2}N^{3/4}+mN^{1/2}+m^{-1/2}N^{1/4}+1,
\end{equation}
with an absolute implied constant, independent of $m$ and $N$. Note that the finitely many terms with \(1\leq n<T\) contribute at most \(T-1\),
uniformly in \(m\), and thus are absorbed into the $O(1) $ term. If \(N<T\), the entire sum is bounded trivially by
\(N\leq T-1\).
\begin{remark}\label{rem:evendegree}
The conclusion does not necessarily hold for polynomials of even degree. For example,
if $q$ is the square of a polynomial with rational coefficients, then
$\sqrt{q(n)}$ is the absolute value of that polynomial at $n$, so its fractional parts take only finitely many values, and thus the sequence is not uniformly distributed.
\end{remark}

\begin{remark}\label{rem:hardyfields}
    The qualitative conclusion of Proposition~\ref{prop:cubic} also follows
from Boshernitzan's theorem \cite{Boshernitzan1994}. A \emph{Hardy field} is a
field of germs at $+\infty$ of real functions that is closed under
differentiation. For a function $f$ in such a field with at most polynomial
growth, the criterion is
\begin{equation}\label{eq:boshernitzan}
 \frac{|f(t)-p(t)|}{\log t}\longrightarrow\infty
 \quad\text{for every }p\in\Q[t].
\end{equation}
This condition is equivalent to uniform distribution of $(f(n))$ modulo
one. On its positive tail, the function $\sqrt{q(t)}$ belongs to the Hardy field of
real algebraic function germs and has leading term $\sqrt a\,t^{3/2}$.
If $\deg p\leq1$, that term dominates $p$; if
$\deg p\geq2$, the leading term of $p$ dominates $\sqrt{q(t)}$.
Therefore, \eqref{eq:boshernitzan} holds in both cases. We retain the
elementary proof above because it gives an explicit estimate needed in the later Fourier argument, with an implied constant independent of $\gamma$ and $N$.
\end{remark}


\begin{proposition}\label{prop:distance}
The sequence $(d_n)$, defined in \eqref{def:dn}, satisfies 
\begin{equation}\label{eq:nearestidentity}
 d_n=\|\sqrt{P_n}\|\leq\frac12.
\end{equation}
Moreover, $(d_n)$ is uniformly distributed in $[0,1/2]$.
\end{proposition}

\begin{proof}
First let $m\geq0$ be any integer, and put $r=\lfloor\sqrt m\rfloor$.
The nearest square is either $r^2$ or $(r+1)^2$. The former is closer
precisely when
\[
 m-r^2<(r+1)^2-m
 \quad\Longleftrightarrow\quad m<r^2+r+\frac12,
\]
which, by integrality, is equivalent to $m\leq r^2+r$. 
The nearest integer to $\sqrt m$ is likewise either $r$ or $r+1$,
and it is $r$ precisely when
\[
 \sqrt m<r+\frac12
 \quad\Longleftrightarrow\quad m<r^2+r+\frac14,
\]
which is equivalent to $m\leq r^2+r$. Neither comparison admits a tie.
The nonnegative square root of the nearest square is therefore the
nearest integer to $\sqrt m$. Taking $m=P_n$ proves
\eqref{eq:nearestidentity}, including uniqueness.

By Proposition~\ref{prop:cubic}, the fractional parts of $\sqrt{P_n}$
are uniformly distributed. For fixed
$0\leq\alpha<\beta\leq1/2$, let
\[
 J_{\alpha,\beta}
 =\{u\in[0,1):\min(u,1-u)\in[\alpha,\beta]\}.
\]
This is a union of at most two intervals, has total length
$2(\beta-\alpha)$, and has only finitely many boundary points. The identity
\[
 \#\{n:1\leq n\leq x,\ d_n\in[\alpha,\beta]\}
 =\#\{n:1\leq n\leq x,\ \{\sqrt{P_n}\}\in J_{\alpha,\beta}\}
\]
is exact. 
It therefore shows that
for every $0\leq\alpha<\beta\leq1/2$,
\begin{equation}\label{eq:distanceud}
 \lim_{x\to\infty}\frac{1}{x}
 \#\{n:1\leq n\leq x,\ d_n\in[\alpha,\beta]\}
 =2(\beta-\alpha),
\end{equation}
which, by the definition of equidistribution, completes the proof of Proposition \ref{prop:distance}.
\end{proof}




\section{Proof of Theorems \ref{thm:asymptotic} and \ref{thm:moments}}
\label{sec:proofs}

In this section, we prove both theorems by reducing the moments to weighted sums involving a
periodic function
evaluated at \(h(n):=\sqrt{P_n}\). The exponential sums arising from the
Fourier expansion are estimated using Proposition~\ref{prop:cubic}. The
resulting bound is then combined with partial summation to obtain the stated
main terms and error terms.

\subsection{Reduction to a periodic function}\label{subsec:reduction}
Fix a positive integer $k$, and write
\begin{equation}\label{eq:periodicdefinitions}
 h(t)=\sqrt{\frac{t(t+1)(2t+1)}{6}},\qquad
 g_k(u)=\|u\|^k,\qquad s=\frac{3k}{2}.
\end{equation}
By Proposition~\ref{prop:distance},
$y_n=h(n)+O(1)$ and
\begin{equation}\label{eq:factorization}
 a_n=|h(n)-y_n|(h(n)+y_n)=\|h(n)\|(h(n)+y_n).
\end{equation}
Expanding $h(n)=\sqrt{P_n}$, we obtain
\begin{align}\label{eq:hexpansion}
 h(n)&=\sqrt{\frac{n(n+1)(2n+1)}{6}}
 =\frac{n^{3/2}}{\sqrt3}
   \sqrt{\left(1+\frac1n\right)\left(1+\frac1{2n}\right)}\notag\\
 &=\frac{n^{3/2}}{\sqrt3}+\frac{\sqrt3}{4}n^{1/2}
   +O(n^{-1/2}).
\end{align}
Therefore,
\[
 h(n)+y_n=\frac{2}{\sqrt3}n^{3/2}+O(n^{1/2}),
 \qquad
 (h(n)+y_n)^k=\left(\frac{2}{\sqrt3}\right)^k n^s
              +O_k(n^{s-1}).
\]
Since $0\leq g_k(u)\leq2^{-k}$, it follows that
\begin{equation}\label{eq:pointwisereduction}
 a_n^k=\left(\frac{2}{\sqrt3}\right)^k n^s g_k(h(n))
       +O_k(n^{s-1}).
\end{equation}
Here $s\geq3/2$, so summing the last error over $1\leq n\leq x$ gives
\begin{equation}\label{eq:momentreduction}
 M_k(x)=\left(\frac{2}{\sqrt3}\right)^k
        \sum_{1\leq n\leq x}n^s g_k(h(n))+O_k(x^s).
\end{equation}
The mean value of $g_k$ is
\begin{equation}\label{eq:periodicmean}
 \mu_k:=\int_0^1g_k(u)\,du
       =2\int_0^{1/2}u^k\,du=\frac{1}{2^k(k+1)}.
\end{equation}

\subsection{Fourier coefficients and the unweighted sum}\label{subsec:fourier}

\begin{lemma}\label{lem:fouriercoefficients}
For $m\in\Z$, let
\[
 c_k(m)=\int_0^1 g_k(u)e(-mu)\,du.
\]
Then $c_k(0)=\mu_k$ and $c_k(m)\ll_k |m|^{-2}$ for $m\ne0$.
The Fourier series of $g_k$ converges absolutely and uniformly. Moreover, for
every integer $H\geq1$, uniformly in $u\in\R$, we have
\begin{equation}\label{eq:fouriertruncation}
 g_k(u)=\mu_k+\sum_{0<|m|\leq H}c_k(m)e(mu)+O_k(H^{-1}).
\end{equation}
\end{lemma}

\begin{proof}
The value of $c_k(0)$ is \eqref{eq:periodicmean}. By symmetry and the
integrality of $m$, for $m\ne0$ we have
\[
 c_k(m)=2\int_0^{1/2}u^k\cos(2\pi m u)\,du
       =-\frac{2k}{2\pi m}\int_0^{1/2}u^{k-1}\sin(2\pi m u)\,du.
\]
The boundary terms in the first integration by parts vanish because
$\sin(0)=\sin(\pi m)=0$. If $k=1$, a direct integration gives
\begin{equation}\label{eq:fourierkone}
 c_1(m)=\frac{(-1)^m-1}{2\pi^2m^2}.
\end{equation}
For $k\geq2$, integration by parts gives
\[
 c_k(m)=\frac{2k}{(2\pi m)^2}
 \left(2^{1-k}(-1)^m
 -(k-1)\int_0^{1/2}u^{k-2}\cos(2\pi m u)\,du\right),
\]
where the integral is bounded by $2^{1-k}/(k-1)$. Therefore, we obtain 
$c_k(m)=O_k(|m|^{-2})$. 

The function $g_k$ is continuous and piecewise smooth, so 
Dirichlet's Fourier convergence theorem  gives pointwise convergence of its Fourier series. Since
\[
\sum_{m\in\mathbb Z}|c_k(m)|<\infty,
\]
the convergence is absolute and uniform. Moreover,
\[
\sum_{|m|>H}|c_k(m)|
\ll_k \sum_{m>H}m^{-2}
\ll_k H^{-1},
\]
which proves \eqref{eq:fouriertruncation}.
\end{proof}

\begin{lemma}\label{lem:periodicsum}
For every fixed positive integer $k$ and real $t\geq1$,
\begin{equation}\label{eq:periodicsum}
 G_k(t):=\sum_{1\leq n\leq t}g_k(h(n))
       =\mu_k t+O_k(t^{3/4}).
\end{equation}
\end{lemma}

\begin{proof}
Let $N\geq1$ be an integer and take $H=\lfloor N^{1/2}\rfloor$.
By \eqref{eq:pyramidalexpsum} and \eqref{eq:fouriertruncation}, we obtain 
\begin{align}\label{eq:frequencysums}
 |G_k(N)-\mu_kN|
 &\ll_k \frac{N}{H}
       +N^{3/4}\sum_{m=1}^{H}m^{-3/2}
       +N^{1/2}\sum_{m=1}^{H}m^{-1}\notag\\
 &\hspace{12mm}+N^{1/4}\sum_{m=1}^{H}m^{-5/2}
       +\sum_{m=1}^{H}m^{-2}\\
 &\ll_k N^{1/2}+N^{3/4}+N^{1/2}\log(2N)+N^{1/4}+1\notag\\
 &\ll_k N^{3/4},\notag
\end{align}
where the last inequality uses
$\log(2N)\ll N^{1/4}$ for $N\geq1$.
For real $t\geq1$, take $N=\lfloor t\rfloor$. Then $G_k(t)=G_k(N)$
and $\mu_k(t-N)=O_k(1)$, which proves \eqref{eq:periodicsum}.
\end{proof}

\subsection{Proof of the main theorems}\label{subsec:partialsummation}

\begin{proof}[Proof of Theorem~\ref{thm:moments}]
Let $s=3k/2$. For real $x\geq1$, partial summation gives 
\begin{equation}\label{eq:abelidentity}
 \sum_{1\leq n\leq x}n^s g_k(h(n))
 =x^sG_k(x)-s\int_1^x t^{s-1}G_k(t)\,dt.
\end{equation}
Applying Lemma \ref{lem:periodicsum},
we have
\begin{equation}
    \sum_{1\leq n\leq x}n^s g_k(h(n)) = \mu_kx^{s+1}-s\mu_k\int_1^x t^s\,dt + O_k\left(x^{s+3/4}+s\int_1^x t^{s-1+3/4}\,dt\right).\label{eq:nsgkhn}
\end{equation}
The main term in \eqref{eq:nsgkhn} equals
\[
 \frac{\mu_k}{s+1}x^{s+1}+\frac{s\mu_k}{s+1} = \frac{\mu_k}{s+1}x^{s+1}+O_k(1).
\]
The error from the integral in \eqref{eq:nsgkhn} is bounded by $O_k(x^{s+3/4})$. Thus,
\begin{equation}\label{eq:weightedperiodicsum}
 \sum_{1\leq n\leq x}n^s g_k(h(n))
 =\frac{\mu_k}{s+1}x^{s+1}+O_k(x^{s+3/4}).
\end{equation}
Combining \eqref{eq:momentreduction}, \eqref{eq:periodicmean}, and
\eqref{eq:weightedperiodicsum} gives
\[
 M_k(x)=\frac{x^{3k/2+1}}{3^{k/2}(3k/2+1)(k+1)}
        +O_k(x^{3k/2+3/4}).
\]
This finishes the proof of Theorem~\ref{thm:moments}.
\end{proof}

\begin{proof}[Proof of Theorem~\ref{thm:asymptotic}]
Taking $k=1$ in Theorem~\ref{thm:moments} gives
\[
 M_1(x)=\frac{x^{5/2}}{5\sqrt3}+O(x^{9/4}).
\]
Dividing $ M_1(x)$ by $x$ as in \eqref{eq:average}, we obtain that
$A(x)=x^{3/2}/(5\sqrt3)+O(x^{5/4})$.
\end{proof}
\begin{remark}\label{rem:relativeerror}
For $k=1$, the cumulative deviation from the main term is $O(x^{9/4})$
against a main term of order $x^{5/2}$, so the relative error is
$O(x^{-1/4})$. This is a deterministic estimate for the total of the
nearest-square differences, not an assertion of independence of the
individual differences. 
\end{remark}
\subsection{Comparison with the discrepancy method}\label{subsec:discrepancy}

\begin{definition}\label{def:discrepancy}
For points $u_1,\ldots,u_N\in[0,1)$, define the \emph{unnormalized star discrepancy} of the sequence $(u_n)$ as 
\begin{equation}\label{eq:discrepancydefinition}
 D(N)=\sup_{0\leq\alpha\leq1}
 \left|\#\{n:1\leq n\leq N,\ 0\leq u_n<\alpha\}-N\alpha\right|.
\end{equation}
\end{definition}

The Erd\H{o}s--Tur\'an inequality
\cite[Cor.\ 1.1, p.\ 8]{montgomery1994ten} gives, for every integer $K\geq1$,
\begin{equation}\label{eq:erdosturan}
 D(N)\ll \frac{N}{K+1}
 +\sum_{m=1}^{K}\frac1m\left|\sum_{n=1}^{N}e(mu_n)\right|.
\end{equation}
For $u_n=\{h(n)\}$, equation \eqref{eq:pyramidalexpsum} therefore yields
\begin{equation}\label{eq:globaldiscrepancy}
 D(N)\ll \frac{N}{K}+N^{3/4}K^{1/2}+N^{1/2}K
             +N^{1/4}+\log(2K).
\end{equation}
Taking $K=\lfloor N^{1/6}\rfloor$ gives
\begin{equation}\label{eq:discrepancyfivesixths}
 D(N)\ll N^{5/6}.
\end{equation}

The one-dimensional Koksma inequality
\cite[Ch.\ 2, Thm.\ 5.1]{UniformDistributionofSequences} states that
for a continuous function $g$ of bounded variation on $[0,1]$,
\begin{equation}\label{eq:koksma}
 \left|\sum_{n=1}^{N}g(u_n)-N\int_0^1g(u)\,du\right|
 \leq\Var_{[0,1]}(g)D(N),
\end{equation}
where $\Var_{[0,1]}(g)$ is the total variation of \(g\) on $[0,1]$. For our function, we have $\Var_{[0,1]}(g_k)=2^{1-k}$. It follows from \eqref{eq:discrepancyfivesixths} and \eqref{eq:koksma}
that $G_k(N)=\mu_kN+O_k(N^{5/6})$. Applying
\eqref{eq:abelidentity} then gives us a moment error
$O_k(x^{3k/2+5/6})$, which is weaker than the exponent $3/4$ in
Theorem~\ref{thm:moments}.



\section{Acknowledgments}\label{sec:acknowledgements}
The authors are grateful to the referee for the detailed corrections and
for suggesting the Fourier-series argument leading
to the improved error terms.

\begin{thebibliography}{99}

\bibitem{beeckmans1994}
L. Beeckmans, Squares expressible as sum of consecutive squares, {\em Amer. Math. Monthly} {\bf 101} (1994), 437--442.

\bibitem{bennett2002}
M. A. Bennett, Lucas' square pyramid problem revisited, {\em Acta Arith.} {\bf 105} (2002), 341--347.

\bibitem{Boshernitzan1994}
M. D. Boshernitzan, Uniform distribution and Hardy fields,
{\em J. Anal. Math.} {\bf 62} (1994), 225--240.

\bibitem{conway1982}
J. H. Conway and N. J. A. Sloane, Lorentzian forms for the Leech lattice, {\em Bull. Amer. Math. Soc.} {\bf 6} (1982), 215--217.

\bibitem{conway1999}
J. H. Conway and N. J. A. Sloane, {\it Sphere Packings, Lattices and Groups}, Springer, 1999.

\bibitem{UniformDistributionofSequences}
L. Kuipers and H. Niederreiter, {\it Uniform Distribution of Sequences}, Wiley-Interscience, 1974.

\bibitem{Laub1990}
M. Laub, Squares expressible as a sum of $n$ consecutive squares, {\em Amer. Math. Monthly} {\bf 97} (1990), 622--625.

\bibitem{question}
E. Lucas, Question 1180, {\em Nouv. Ann. Math.} {\bf 14} (1875), 336.
Available at \url{http://www.numdam.org/item/NAM_1875_2_14__336_0/}.

\bibitem{montgomery1994ten}
H. L. Montgomery, {\it Ten Lectures on the Interface Between Analytic Number Theory and Harmonic Analysis}, American Mathematical Society, 1994.

\bibitem{oeis}
OEIS~Foundation Inc., The {O}n-{L}ine {E}ncyclopedia of {I}nteger {S}equences,
  published electronically at \url{https://oeis.org}, 2026.

\bibitem{Pink2003}
I. Pink, On the differences between polynomial values and perfect powers,
{\em Publ. Math. Debrecen} {\bf 63} (2003), 461--472.

\bibitem{VaZa2001hyper}
M. V\^aj\^aitu and A. Zaharescu, Integer points near hyperelliptic curves,
{\em C. R. Math. Acad. Sci. Soc. R. Can.} {\bf 23} (2001), 84--90.

\bibitem{VaZa2001usually}
M. V\^aj\^aitu and A. Zaharescu, Integer points unusually close to elliptic curves,
{\em Port. Math.} {\bf 58} (2001), 211--218.

\bibitem{Watsoncannonball}
G. N. Watson, The problem of the square pyramid, {\em Messenger Math.} {\bf  48} (1918), 1--22.

\bibitem{paolo}
P. Xausa, Table of $n$, $a(n)$ for $0\leq n\leq10000$, sequence A351830, The On-Line Encyclopedia of Integer Sequences. Available at \url{https://oeis.org/A351830/b351830.txt}.

\end{thebibliography}
\bigskip
\hrule
\bigskip

\noindent 2020 {\it Mathematics Subject Classification}:
Primary 11L07; Secondary 11B99, 11K38, 11K06.

\noindent \emph{Keywords: } cannonball problem, exponential sum, discrepancy, equidistribution.

\bigskip
\hrule
\bigskip

\noindent 
(Concerned with sequences
\seqnum{A001032},
\seqnum{A350886},
\seqnum{A350887}, 
\seqnum{A350888}, and
\seqnum{A351830}.)

\bigskip
\hrule
\bigskip

\vspace*{+.1in}
\noindent
Received October 8 2024;
revised versions received  December 24 2025;
October 7 2026; October 8 2026.
Published in {\it Journal of Integer Sequences},
October 8 2026.

\bigskip
\hrule
\bigskip

\noindent
Return to \href{https://cs.uwaterloo.ca/journals/JIS/}{Journal of Integer Sequences home page}.
\vskip .1in


\end{document}

                                                                                

