21. N2 (USA) Each positive integer \( a \) undergoes the following procedure in order to obtain the number \( d = d(a) \): (1) move the last digit of \( a \) to the first position to obtain the number \( b \); (2) square \( b \) to obtain the number \( c \); (3) move the first digit of \( c \) to the end to obtain the number \( d \). (All the numbers in the problem are considered to be represented in base 10.) For example, for \( a = 2003 \), we have \( b = 3200 \), \( c = 10240000 \), and \( d = 02400001 = 2400001 = d(2003) \). Find all numbers \( a \) for which \( d(a) = a^2 \).
21. Let $a$ be a positive integer for which $d(a)=a^{2}$. Suppose that $a$ has $n+1$ digits, $n \geq 0$. Denote by $s$ the last digit of $a$ and by $f$ the first digit of $c$. Then $a=\overline{* \ldots * s}$, where $*$ stands for a digit that is not important to us at the moment. We have $\overline{\ldots * s^{2}}=a^{2}=d=\overline{* \ldots * f}$ and $b^{2}=\overline{s * \ldots *}^{2}=$ $c=\overline{f * \ldots *}$. We cannot have $s=0$, since otherwise $c$ would have at most $2 n$ digits, while $a^{2}$ has either $2 n+1$ or $2 n+2$ digits. The following table gives all possibilities for $s$ and $f$ :  | $s$ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | $f=$ last digit of $\overline{* \ldots * s}^{2}$ | 1 | 4 | 9 | 6 | 5 | 6 | 9 | 4 | 1 | | $f=$ first digit of $\overline{s * \ldots *}^{2}$ | $1,2,3$ | $4-8$ | 9,1 | 1,2 | 2,3 | 3,4 | $4,5,6$ | $6,7,8$ | 8,9 |  We obtain from the table that $s \in\{1,2,3\}$ and $f=s^{2}$, and consequently $c=b^{2}$ and $d$ have exactly $2 n+1$ digits each. Put $a=10 x+s$, where $x<10^{n}$. Then $b=10^{n} s+x, c=10^{2 n} s^{2}+2 \cdot 10^{n} s x+x^{2}$, and $d=$ $2 \cdot 10^{n+1} s x+10 x^{2}+s^{2}$, so from $d=a^{2}$ it follows that $x=2 s \cdot \frac{10^{n}-1}{9}$. Thus $a=\underbrace{6 \ldots 6}_{n} 3, a=\underbrace{4 \ldots 4}_{n} 2$ or $a=\underbrace{2 \ldots 2}_{n} 1$. For $n \geq 1$ we see that $a$ cannot be $a=6 \ldots 63$ or $a=4 \ldots 42$ (otherwise $a^{2}$ would have $2 n+2$ digits). Therefore $a$ equals $1,2,3$ or $\underbrace{2 \ldots 2}_{n} 1$ for $n \geq 0$. It is easy to verify that these numbers have the required property.