TITLE: Peculiar numbers
QUESTION [12 upvotes]: This is from the weekly math challenge of the French newspaper Le Monde.

"Magical" numbers have a remarkable property: when one of them, say $n$, is squared, it is such that $n^2$ ends with $n$.
For example, $76$ is one of those since $76^2=5776$
Can you list these numbers without a CAS or a calculator ?

$u$ is such number if and only if $\displaystyle u(u-1)=10^qm$ where $q$ is the number of digits of $u$ (namely $\displaystyle \left\lfloor\frac{\ln(u)}{\ln(10)}\right\rfloor+1)$ and $m$ is arbitrary.
Using the fact that $u$ and $u-1$ are coprime , I'm able to find the first few numbers by some narrowed trial-and-error : $5,6,25,76,376,625$.
But it becomes tedious with $4$ digit numbers...
Can someone answer the original question ?

REPLY [2 votes]: If you just look at the units digit, you see that magical numbers must end in $0, 1, 5, $ or $6$. We will show that the only magical numbers that end in $1$ are $1, 01, 001, \dots$.
If $u$ is a two-digit number that ends in 1, then $u = 10x + 1$ for some digit $x$.
\begin{align}
         u(u - 1) &\equiv 0 \pmod{100}\\
   (10n + 1)(10n) &\equiv 0 \pmod{100}\\
              10n &\equiv 0 \pmod{100}\\
                n &\equiv 0 \pmod{10}\\
\end{align}
So $u = 01$. Suppose, inductively, that the only $q$-digit magical number that ends in $1$ is $000\dots01$, where there are $q - 1$ $0$s to the left of $1$.
Then a $(q+1)$-digit magical number that ends in $1$ must look like $u = 10^q x + 1$ for some digit $x$. Reasoning similar to that shown above will show that $x = 0$. It follows by induction that then only magical numbers that end in $1$ are  $1, 01, 001, \dots$.
Similar reasoning will show that the only magical nubers that end in $0$ are $0, 00, 000, \dots$
There is a peculiar pattern with the magic numbers that end in $5$. The first three are listed below.
5²   =     25
25²  =    625
625² = 390625

Note the magical numbers are formed by prepending a digit to the previous magic number.
Note also that the digit prepended is the first nonrepeated digit in $n^2$.
So the next three numbers must be
0625²   =       390625
90625²  =   8212890625
890625² = 793212890625

Where we have to accept $0625$ as a four-digit number.
We can prove this.
Let $N$ be a q-digit magical number that ends in $5$.
We seek a digit $\alpha$ such that the $(q+1)$-digit number 
$N' = 10^q\alpha + N$ is the next magical number that ends in 5.
\begin{align}
   N'^2 - N' &\equiv 0 \pod{10^{q+1}}\\
   (10^q\alpha + N)^2 - (10^q\alpha + N) &\equiv 0 \pod{10^{q+1}}\\
   2 \cdot 10^q\alpha N - 10^q\alpha  + N^2 - N &\equiv 0 \pod{10^{q+1}}\\
   -10^q\alpha  + N^2 - N &\equiv 0 \pod{10^{q+1}}\\
   10^q\alpha  &\equiv N^2 - N \pod{10^{q+1}}\\
   \alpha  &\equiv \dfrac{N^2 - N}{10^q} \pod{10}\\
\end{align}
Which means that $\alpha$ is the first nonrepeating digit of $N^2$.
To find the magical numbers that end in $6$, note that
6²   =     36  and  5 + 6 = 11
76²  =   5776  and  25 + 76 = 101
376² = 141376  and  625 + 376 = 1001

It seems that the two $q$-digit magical numbers, the one that ends in $5$ and the other that ends in $6$,  sum to $10^q + 1$.
It turns out that this is easy to prove.
Let $N$ be a q-digit magical number that ends in $5$. Then
\begin{align}
   (10^q + 1 - N)^2 - (10^q + 1 - N)
      &\equiv  (1 - N)^2 - (1 - N) \pod{10^q}\\
      &\equiv  N^2 - N \pod{10^q}\\
      &\equiv  0 \pod{10^q}\\
\end{align}
so we find
10001   -   0625 =   9376  and  9376²   =    87909376
100001  -  90625 =  09376  and  09376²  =    87909376
1000001 - 890625 = 109376  and  109376² = 11963109376