# Why is there a pattern to the last digits of square numbers?

I was programming and I realized that the last digit of all the integer numbers squared end in $0, 1, 4, 5, 6,$ or $9$.

And in addition, the numbers that end in $1, 4, 9, 6$ are repeated twice as many times as the numbers that end in $0, 5$

I checked the numbers from $1$ to $1000$, and the results are:

$1.$ The numbers on the left are the last digit of each digit squared.

$2.$ The numbers on the right are the number of times that the last digit is repeated.

$$\begin{array}{cc} 0: &100, \\ 1: &200, \\ 4: &200, \\ 5: &100, \\ 6: &200, \\ 9: &200 \end{array}$$

So, why does this happen? What is the property that all integers have?

• This is called the set of quadratic residues modulo $10$. Commented May 24, 2018 at 23:42
• For your first question, you’re simply asking what is $x^2 \mod(10)$, which you can without loss of generality manually calculate for $x=0,\dots,9$. Commented May 24, 2018 at 23:44
• When you do long multiplication, the ones digit is always what you get from multiplying the ones place first.
– user211599
Commented May 24, 2018 at 23:50
• it's also worth noting that similar patterns emerge for different bases. Commented May 25, 2018 at 20:44
• First, you might ask yourself why there is a pattern in the last digits of counting numbers.
– Owen
Commented May 25, 2018 at 20:44

The answer to this question is a bit less profound than you might hope. To see why, first note that the last digit of the square of any natural number only depends on the number's last digit - any other digits represent powers of 10 and do not make any difference to the last digit of the square.

So the problem amounts to working out the last digit of the squares of single digit numbers (and 10, if we don't consider 0 a natural number). They are:

1. 1
2. 4
3. 9
4. 6
5. 5
6. 6
7. 9
8. 4
9. 1
10. 0

The relative frequencies of these last digits here explain why they take up the proportions of square numbers that you observe.

• a much more interesting question would be about the symetery of the last digit in square numbers. Commented May 25, 2018 at 8:53
• @KamiKaze That's because of $(-x)^2 = x^2$. Commented May 25, 2018 at 11:41
• @KamiKaze, what's probably even more interesting is that fifth powers keep their last digit. Commented May 30, 2018 at 21:01
• @Kitegi I do not think that explains why the there is a symetry of the last digits with the 5 in the middle. 1 4 9 6 5 6 9 4 1 Commented Jun 4, 2018 at 7:30
• @KamiKaze I'm not sure if there's much to explain about it. When working in an even base $b$, multiples of $b/2$ are equal to either $b/2$ or $0$ modulo $b$, depending on whether it's an even or odd multiple. We get $(b/2)^2 = b/2$ here because $5$ is odd. Commented Jun 5, 2018 at 8:32

These numbers are the squares modulo 10. Notice that the square of the number $10n+k$ is $$(10n+k)^2 = 10(10n^2+2nk)+k^2,$$ so the last digit of the square is determined by only the last digit of the original number. In particular, we find $$0^2=0 \quad 1^2=1 \quad 2^2 = 4 \quad 3^2 = 9 \quad 4^2 = 10+6 \\ 5^2 = 20+5 \quad 6^2 = 30+6 \quad 7^2 = 40+9 \quad 8^2 = 60+4 \quad 9^2 = 80+1,$$ or writing "$\equiv$" to mean that they have the same last digit, $$0^2 \equiv 0 \\ 1^2 \equiv 1 \equiv 9^2 \\ 2^2 \equiv 4 \equiv 8^2 \\ 3^2 \equiv 9 \equiv 7^2 \\ 4^2 \equiv 6 \equiv 6^2 \\ 5^2 \equiv 5,$$ so every last digit except $0$ and $5$ is the last digit of two squares out of a block of 10 consecutive numbers, while $0$ and $5$ are the last digit of only one each.

• Good answer, so for any number to pow of $n$, that is $(10n + l)^n$ , its last number will the last number of $l^n$?
– ESCM
Commented May 25, 2018 at 0:18
• per example, $232^4 = 2897022976$, and the last number of $232$ is $2$ and $2^4 = 16$, and the last number of $16$ is $6$ that is equal to the last number of $2897022976$
– ESCM
Commented May 25, 2018 at 0:19
• Is correct this ?
– ESCM
Commented May 25, 2018 at 0:36
• Yes. In fact in any base $b$, the last digit of any sum or product depends only on the last digits of the numbers: $(nb+k)+(mb+j) \equiv k+j$ and $(nb+k)(mb+j) \equiv kj$. Commented May 25, 2018 at 7:06
• Those equivalences can also be written: $$0^2 \equiv 0 \\ (\pm 1)^2 \equiv 1 \\ (\pm 2)^2 \equiv 4 \\ (\pm 3)^2 \equiv 9 \\ (\pm 4)^2 \equiv 6 \\ 5^2 \equiv 5$$ of course. Commented May 25, 2018 at 17:12

Others have covered the reason why the last digit of the number you are squaring is all that matters. There is also a good reason why some digits appear twice and others appear once. The point is that if $$k$$ is any digit then $$(10-k)^2=100-20k+k^2$$ has the same last digit as $$k^2$$, so for any $$k$$ other than $$0$$ or $$5$$ there is another digit whose square ends in the same thing. ($$0$$ and $$5$$ are special because $$10-0$$ isn't a digit and $$10-5=5$$.) The same thing applies in any base, with the caveat that there is only an analogue of $$5$$ in even bases ($$5=10/2$$); in odd bases every $$k$$ except $$0$$ comes in a pair.

(This argument immediately tells you that in base $$b$$, squares can have at most $$1+\lfloor b/2\rfloor$$ possible last digits. In fact this bound is attained if and only if $$b$$ is either a prime or twice an odd prime.)

(To answer Vignesh Manoharan: The bound is exact if and only if for any $$a$$ the only solutions to $$x^2\equiv a^2$$ mod $$n$$ are $$x\equiv\pm a$$ mod $$n$$. As you say, this is equivalent to $$n\mid (x-a)(x+a)$$ implies $$n\mid (x-a)$$ or $$n\mid (x+a)$$, which is certainly true for $$n$$ prime. But it's also true for $$n=2p$$ where $$p$$ is an odd prime, since $$p$$ will divide one factor, and $$2$$ must divide both as they differ by an even number. It's not true if $$n=qr$$ where $$q,r>1$$ are the same parity, by setting $$x=(q+r)/2$$ and $$a=(q-r)/2$$; any other base has a factorisation of this form.)

• The only answer that seems to mathematically address the frequencies of those last digits! Commented May 25, 2018 at 11:30
• How would we go about proving this "In fact this bound is attained if and only if is either a prime or twice an odd prime"? I can prove that this is true for prime ($a^2 = b^2 \mod p \Rightarrow (a-b)(a+b) = 0 \mod p$) Commented May 27, 2018 at 8:17
• @VigneshManoharan I'll edit to include an explanation. Commented May 27, 2018 at 9:51

What you are looking at is the residues of squares modulo $10$.

$0^2=\color{red}0\bmod 10\\1^2=\color{blue}1\bmod 10\\2^2=\color{orange}4\bmod 10\\3^2=9\bmod 10\\4^2=\color{green}6\bmod 10\\5^2=\color{brown}5\bmod 10\\6^2=\color{green}6\bmod 10\\7^2=9\bmod 10\\8^2=\color{orange}4\bmod 10\\9^2=\color{blue}1\bmod 10$

As you can see, $0$ and $5$ are half as frequent as the other residues which are indeed $1,4,6,9$.

• The last sentence should end with $1,4,9,6$. Commented May 25, 2018 at 17:14
• Right, I forgot $6$. Thanks. Commented May 25, 2018 at 19:22

Note that any number can be written in the form $10a+(5 \pm b)$ where $0 \leq b \leq 5$. If n = $10a+(5 \pm b)$, then we can calculate $n^2$ as

$(10a)^2+2(10a)(5 \pm b) + (5 \pm b)^2=$
$100a^2 + 100a \pm 20ab+(5 \pm b)^2$

$100a^2$, $100a$, and $\pm 20ab$ are all divisible by 10, so we can ignore them, and we're left with $(5 \pm b)^2 = 25 \pm 10b +b^2$. Again, we can ignore $\pm10b$, and we can reduce 25 to 5, leaving $5+b^2$. Note that the $\pm$ part has disappeared; $(10a+(5 + b))^2$ has the same last digit as $(10a+(5 - b))^2$. So, normally, each value of $b$ gives the same remainder twice, once for $+b$ and once for $-b$. But if $b=0$, then $+b$ and $-b$ are the same number, so it gives the remainder only once. And if $b=5$, then $5-b$ gives 0, and $5+b$ gives 10, which also corresponds to a last digit of 0.

So we have:

$b = 0$: last digit of $n$ is 5, last digit of $n^2$ is 5
$b = 1$: last digit of $n$ is 4 or 6, last digit of $n^2$ is 6
$b = 2$: last digit of $n$ is 3 or 7, last digit of $n^2$ is 9
$b = 3$: last digit of $n$ is 2 or 8, last digit of $n^2$ is 4
$b = 4$: last digit of $n$ is 1 or 9, last digit of $n^2$ is 1
$b = 5$: last digit of $n$ is 0, last digit of $n^2$ is 0

Hence, 0 and 5 show up once, while 1,4,6, and 9 show up twice.

Consider: $$(x+k)^2=(x+k)(x+k)=x^2+2xk+k^2$$ In your case, $x=10z, z\in \Bbb Z$, and $0\le k\le9, k\in \Bbb Z$. Thus it becomes: $$(x+k)^2=100z^2+20zk+k^2$$ for which the only possible unit is the unit from $k^2$, and so the facts that: $$1^2,9^2\space\text{end in}\space 1$$ $$2^2,8^2\space\text{end in}\space 4$$ $$3^2,7^2\space\text{end in}\space 9$$ $$4^2,6^2\space\text{end in}\space 6$$ $$5^2\space\text{ends in}\space 5$$ $$0^2\space\text{ends in}\space 0$$ means that those are your only possibilities.

Given any integer base $b > 1$, you will see a pattern to the squares that matches the squares modulo $b$. For example, the squares modulo 20 are 1, 4, 9, 16, 5, 16, 9, 4, 1, 0, 1, 4, 9, 16, 5, 16, 9, 4, 1, 0. Take good note of the symmetries. Then, the first twenty squares in vigesimal are 1, 4, 9, G, 15, 1G, 29, 34, 41, 50, 61, 74, 89, 9G, B5, CG, E9, G4, I1, 100.

There's a similar pattern in binary but it's more interesting if you look at it in octal or hexadecimal, since those give you a broader view of the patterns.

I know you asked about decimal. The principle is the same, only the specific numbers are different. For instance, 1 squared is 1. No surprise there. You also know that $(-1)^2 = 1$. And also $9 = 10 - 1$. So $9^2 \equiv (-1)^2 = 1 \pmod{10}$. Along the same lines, $8 = 10 - 2$, so then you know what $8^2$ is...