TITLE: Trick with modular exponentiation
QUESTION [5 upvotes]: For example,
$$123^{25} \pmod{10}$$
$$ 123 \equiv 3 \pmod{10}$$
$$123^{2} \equiv 9 \pmod{10}$$
$$123^{3} \equiv 7 \pmod{10}$$
$$123^{4} \equiv 1 \pmod{10}$$
$$123^{5} \equiv 3 \pmod{10}$$
It is easy to see:
$$123^n \equiv 123^{4(k) + n} \pmod{10}$$
$$4(6) = 4(k) = 24$$ Hence,
$$123^{1} \equiv 123^{4(6) + 1} \equiv 123^{25} \pmod{10} \equiv 3 \pmod{10}$$
My question is why does this work?

REPLY [2 votes]: Yes, $\,{\rm mod}\ 10\!:\ \color{#c00}{3^4\equiv 1}\,\Rightarrow\, 3^{J+4K}\!\equiv 3^J(\color{#c00}{3^4})^K\!\equiv 3^J\color{#c00}1^K\!\equiv 3^J $ by $ $ Congruence Power, Product Rules. 
If $\,a\,$ is coprime to $\,m\,$ then $\ a^{\varphi(m)}\equiv 1\pmod{m}\ $ by Euler's Theorem. Therefore mod $\,m,\,$  the sequence of powers of $\,a\,$ will be periodic. The exact period length, i.e. the least $\,n\,$ such that $\,a^n\equiv 1\pmod m\,$ is  known as the order of $\,a\ {\rm mod}\ m.\,$ It can be less than $\,\varphi(m),\,$ but must be divisor of $\,\varphi(m).$