<s>[INST] 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?
 [/INST] The pattern you've seen here is that $3^{4k+1}\;\text{mod}\:5=3$ for integers $k\ge 0$. You can prove this inductively from the base case, and the fact that $3^4=81$ is equal to $1$ modulo $5$. Hence, since $4\cdot 11339+1=45357$, then your prediction holds.
</s>