How to calculate $3^{45357} \mod 5$? I wrote some code, here is what it gives:
\begin{align*}
3^0 \mod 5 = 1 \\
3^1 \mod 5 = 3 \\
3^2 \mod 5 = 4 \\
3^3 \mod 5 = 2 \\\\
3^4 \mod 5 = 1 \\
3^5 \mod 5 = 3 \\
3^6 \mod 5 = 4 \\
3^7 \mod 5 = 2 \\\\
3^8 \mod 5 = 1 \\ 
3^9 \mod 5 = 3 \\ 
3^{10} \mod 5 = 4 \\
3^{11} \mod 5 = 2 \\\\
3^{12} \mod 5 = 1 \\
3^{13} \mod 5 = 3 \\
...
\end{align*}
As I see, there's a period $1, 3, 4, 2$. And intuitively, it's easy to predict that $3^{45357} \mod 5 = 3$. Unfortunately, I'm almost unfamilar with modular math. Could you give me strict analytic explaination of this thing?

Using Fermat's Little theorem, 
$a^{p-1}\equiv1\pmod p$ if $(a,p)=1$
$\implies (a^{p-1})^d\equiv1^d\pmod p \equiv1 \pmod p$
if  $b=c+(p-1)d$ i.e., $b\equiv c\pmod {p-1}$ where $a,b,c,d$ are non-negative integers and $p$ is prime,
$ a^b=a^{c+(p-1)d}=a^c\cdot(a^{p-1})^d\equiv a^c\pmod p$
Here, $3^{5-1}\equiv 1\pmod 5$ as $(3,5)=1$
As $45357\equiv 1\pmod 4\implies 3^{45357}\equiv 3^1\pmod 5\equiv 3$
