  Let $s_q(n)$ denote the sum of the digits in the $q$-ary expansion of an
integer $n$. In 2005, Melfi examined the structure of $n$ such that $s_2(n) =
s_2(n^2)$. We extend this study to the more general case of generic $q$ and
polynomials $p(n)$, and obtain, in particular, a refinement of Melfi's result.
We also give a more detailed analysis of the special case $p(n) = n^2$, looking
at the subsets of $n$ where $s_q(n) = s_q(n^2) = k$ for fixed $k$.
