Are there infinitely many integers which form the trailing digits of their square? Recently on StackOverflow, a question was asked about a way to find numbers which form the trailing digits of their square. Here are some examples of such numbers.
$6 ^ 2 = 36$
$376 ^ 2 = 141 376$
$90625 ^ 2 = 8212890625$
I suggested a solution that would build such numbers from right to left, making it sub-linear. This allowed to test if such numbers exist for large numbers of digits. It would seem these numbers are fairly common. But is there infinitely many of them?
 A: Yes, there are infinitely many such numbers.
Suppose $n$ is a $d$-digit number with that property, i.e. the last $d$ digits of $n^2$ are the digits of $n$ (in order). That means $n^2 \equiv n \pmod{10^d}$, or equivalently $n\cdot (n-1) \equiv 0 \pmod{10^d}$. Since $n$ and $n-1$ are coprime, we must have one of the following cases:


*

*$n \equiv 0 \pmod{10^d}$,

*$n \equiv 1 \pmod{10^d}$,

*$n \equiv 0 \pmod{2^d}$ and $n-1 \equiv 0 \pmod{5^d}$,

*$n \equiv 0 \pmod{5^d}$ and $n - 1 \equiv 0 \pmod{2^d}$.


In case 1, the only way that $n$ can have at most $d$ digits is if $n = 0$. We can count that or not. In case 2, we must have $n = 1$, otherwise $n$ would have too many digits. We can count this or not, it's again not very interesting.
The other cases, 3 and 4, are interesting. By the Chinese remainder theorem, there are solutions to these systems of congruences, unique modulo $10^d$. In particular, there is a unique $n_d$, $0 \leqslant n_d < 10^d$, with $n_d \equiv 0 \pmod{5^d}$ and $n_d \equiv 1 \pmod{2^d}$, and a unique $m_d$, $0 \leqslant m_d < 10^d$, with $m_d \equiv 0 \pmod{2^d}$ and $m_d \equiv 1 \pmod{5^d}$. It is fairly obvious that we have $m_d,n_d > 0$ in fact.
It is possible that $n_d$ or $m_d$ actually has fewer than $d$ digits - for example $n_4 = 625 = n_3$ has only three digits - but there are infinitely many distinct $n_d$ and infinitely many distinct $m_d$. And apart from $0$ and $1$, these are all numbers forming the trailing digits of their squares.
