# Find the rightmost digit of: $1^n+2^n+3^n+4^n+5^n+6^n+7^n+8^n+9^n$

Find the rightmost digit of: $1^n+2^n+3^n+4^n+5^n+6^n+7^n+8^n+9^n(n$ arbitrary positive integer)
First of all I checked a few cases for small $n$'s and in all cases the rightmost digit was $5$, so maybe this is the case for all values of $n$.
Then I thought maybe it's better to consider odd and even cases for $n$ but there's no unified rule here, because for example: $8^2\equiv{4}\pmod{10}$ and $8^4\equiv{6}\pmod{10}$. Any ideas??

• It is not $5$ for all $n$. For $4\mid n$ the last digit is $3$ (if I computed it correctly). For $n=1,2,3,\ldots$ I find $\{5, 5, 5, 3, 5, 5, 5, 3, 5, 5, 5, 3,\ldots\}$ – Winther Jun 6 '16 at 3:23
• So I made a mistake! – Hamid Reza Ebrahimi Jun 6 '16 at 3:27
• Maybe I write a computer program to evaluate that expression for $n<=20$ – Hamid Reza Ebrahimi Jun 6 '16 at 3:46
• That is a good idea (or you can use WolframAlpha). As pointed out in the answers below you only need to evaluate it for $n=1,2,3,4$ to know the result for all $n$ as the sequence has to be periodic. – Winther Jun 6 '16 at 3:49
• I wrote a C# program and verified that @Winther is right,and Brian answer is the best. – Hamid Reza Ebrahimi Jun 6 '16 at 8:35

Modulo $10$ easy inductions show that

\begin{align*} &1^n+9^n\equiv\begin{cases} 0,&\text{if }n\text{ is odd}\\ 2,&\text{if }n\text{ is even}\;, \end{cases}\\ &2^n+8^n\equiv\begin{cases} 0,&\text{if }n\text{ is odd}\\ 8,&\text{if }n\equiv 2\pmod4\\ 2,&\text{if }n\equiv 4\pmod4\;, \end{cases}\\ &3^n+7^n\equiv\begin{cases} 0,&\text{if }n\text{ is odd}\\ 8,&\text{if }n\equiv 2\pmod4\\ 2,&\text{if }n\equiv 0\pmod4\;, \end{cases}\\ &4^n+6^n\equiv\begin{cases} 0,&\text{if }n\text{ is odd}\\ 2,&\text{if }n\text{ is even}\;,\text{ and} \end{cases}\\ &5^n\equiv 5\;. \end{align*}

Thus,

$$\sum_{k=1}^9k^n\equiv\begin{cases} 5,&\text{if }n\not\equiv0\pmod4\\ 3,&\text{if }n\equiv 0\pmod4\;. \end{cases}$$

And as a quick check of the second case above,

$$\sum_{k=1}^9k^4=1+16+81+256+625+1296+2401+4096+6561=15,333\;.$$

Note that the stated result is true only when $n$ is not a multiple of $4$.

Hint: This is equivalent to asking what $$1^n+2^n+3^n+4^n+5^n+6^n+7^n+8^n+9^n$$ is modulo $10$. But $m=m-10$ mod $10$, so modulo 10 the above is the same as $$1^n+2^n+3^n+4^n+5^n+(-4)^n+(-3)^n+(-2)^n+(-1)^n.$$ What does this equal if $n$ is odd versus even?

• Looking further at this, the above answers the odd case but I'm not sure I know how to answer the even case. – Semiclassical Jun 6 '16 at 3:13
• For odd $n$ the expression is $5^n$ modulus 10 and obviously 5 , for even $n$ I don't know – Hamid Reza Ebrahimi Jun 6 '16 at 3:17
• @HamidRezaEbrahimi If $n=2k$, use $a^{2k}=(a^2)^k$ a be ready for a surprise. – user228113 Jun 6 '16 at 3:31
• @G.Sassatelli Ahh, now that's a great hint. I'm a bit disappointed I didn't notice it. – Semiclassical Jun 6 '16 at 3:41

Our sum is odd, so all we need to do is to compute it modulo $5$.

Note that the congruence class of $k^n$ modulo $5$ is the same as the congruence class of $k^{n+4}$ modulo $5$. This is obvious if $k$ is divisible by $5$. And if $k$ is not divisible by $5$ then $k^4\equiv 1\pmod{5}$.

So to find the last digit for any $n$, it is enough to know the last digit for $n=1,2,3,4$. Now you need to compute the last digit for $n=1,2,3,4$, and you will know the situation for all $n$.

• See the comment by @Winter. The suggested result is not true. – awllower Jun 6 '16 at 3:27
• @awllower: Thanks, the overall analysis may still be useful. – André Nicolas Jun 6 '16 at 3:31
• Yes, the argument is valid, but the result that this argument shows is not as expected. :P – awllower Jun 6 '16 at 3:32

Given that the order of any of the components here will divide $4$, since that is the Carmichael function value for $10$, it is only necessary to check the values for $n=\{1,2,3, 4\}$. The results follow a pattern across the range of values (all $\bmod 10$):

$$\begin{array}{c|c} n & 1^n & 2^n & 3^n & 4^n & 5^n & 6^n & 7^n & 8^n & 9^n & \sum \\ \hline 1 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 5 \\ \hline 2 & 1 & 4 & 9 & 6 & 5 & 6 & 9 & 4 & 1 & 5 \\ \hline 3 & 1 & 8 & 7 & 4 & 5 & 6 & 3 & 2 & 9 & 5 \\ \hline 4 & 1 & 6 & 1 & 6 & 5 & 6 & 1 & 6 & 1 & 3 \end{array}$$

and you can check that the following line for $n=5$ repeats the values for $n=1$ if required.

Thus the answer is that the last digit is $3$ when $n\equiv 0 \bmod 4$ and is $5$ otherwise.

• You can also see here the pattern of $x^n \equiv -(10-x)^n \bmod 10$ for $n$ odd and $x^n \equiv (10-x)^n \bmod 10$ for $n$ even. – Joffan Jun 6 '16 at 15:53

If $n$ is odd, then as in the answer by @Semiclassical, $1^n+2^n+3^n+4^n+5^n+6^n+7^n+8^n+9^n\equiv5\pmod{10}.$
If $n$ is even, say $n=2m,$ then first observe $4^m\equiv 5+(-1)^m\pmod{10}$ and $9^m\equiv(-1)^m\pmod{10}.$
So we have
\begin{align}1^n+2^n+3^n+4^n+5^n+6^n+7^n+8^n+9^n&\equiv2(1+2^n+3^n+4^n)+5^n\\&\equiv2(1+(5+(-1)^m)+((-1)^m)+(5+(-1)^{2m}))+5\\ &\equiv4(1+(-1)^m)+5\pmod{10}\end{align}

Hope this helps.

For $X>0$, and sequence indexed by $n > 0$:

$X^n$ repeats each $2 - 1$ in $\pmod 2$ since $2$ is prime.

$X^n$ repeats each $5 - 1$ in $\pmod 5$ since $5$ is prime.

So the sequence $X^n$ repeats each ${\rm gcd}(2 - 1, 5 - 1)$ in $\pmod {10}$, so you only have to check the value of

$$\sum_{X = 1}^9 X^n \pmod {10}$$

for the values $n \in \{1, 2, 3, 4\}$ and the sequence will repeat, and the case $n = 0$ should be considerred separately.