# 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?

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:

1. $n \equiv 0 \pmod{10^d}$,
2. $n \equiv 1 \pmod{10^d}$,
3. $n \equiv 0 \pmod{2^d}$ and $n-1 \equiv 0 \pmod{5^d}$,
4. $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.

• I'm not sure how often it happens that $n_d = n_{d+1}$ or $m_d = m_{d+1}$. Gut feeling says it'll be rare, but my gut feeling is known to be sometimes wrong. – Daniel Fischer Apr 5 '18 at 20:44
• It would seem it happens fairly often using the script in the question's link. – Olivier Melançon Apr 5 '18 at 20:53