A mathematical phenomenon regarding perfect squares…

I was working on identifying perfect squares for one of my programs regarding Pythagorean triplet. And I found that for every perfect square if we add its digits recursively until we get a single digit number, e.g. 256 -> 13 -> 4 etc. we get the single digit as either 1,4,7 or 9. Is it an unnoticed phenomenon? What can be mathematical reason behind this?

The standard observation is that "add the digits of a number recursively" is essentially just a fancy way to say "find the value of the number modulo 9".

So your question is really asking "which numbers are squares modulo 9?" This is easy to answer with modular arithmetic: since there are only 9 numbers modulo 9, we can use brute force

• $0^2 = 0$
• $1^2 = 1$
• $2^2 = 4$
• $3^2 = 9 = 0 \pmod 9$
• $4^2 = 16 = 9 + 7 = 7 \pmod 9$
• $5^2 = 25 = 9\cdot 2 + 7 = 7 \pmod 9$
• $6^2 = 36 = 9\cdot 4 = 0 \pmod 9$
• $7^2 = 49 = 9\cdot 5 + 4 = 4 \pmod 9$
• $8^2 = 64 = 9\cdot 7 + 1 = 1 \pmod 9$

We could have saved some trouble by using $x^2 = (-x)^2$; e.g. $(-3) = 6 \bmod 9$, thus $6^2 = (-3)^2$. We only needed to check $0$ through $4$.

Because of the actual calculation you do, the representatives of the equivalence classes modulo $9$ that you wind up with are the numbers $1$ through $9$, rather than $0$ through $8$. Note $0 = 9 \bmod 9$. Thus, the squares are $1, 4, 7, 9$, as you've observed.

Any perfect square is congruent to one of $0,1,4,7$ modulo $9$. This can be checked by squaring $0,1,2,3,4$. And any positive integer is congruent modulo $9$ to the sum of its decimal digits. This is because $10\equiv 1 \pmod{9}$.

Remark: You could use the same idea to look at the digit sums of squares written in say base $16$.