I understand how to do modular arithmetic on numbers with large exponents (like $8^{202}$). However, I am having trouble understanding how to calculate something like:
$ 3^{3^{3^{3^3}}}$ mod 5
(that's 5 3's there). Can anyone help me understand how to get this started?
By Fermat's Little Theorem, it suffices to look at the exponent modulo 4, so the problem reduces to finding $3^{3^{3^3}}$ modulo 4. But $3^{3^{3^3}} \equiv (-1)^{(-1)^{3^3}} \equiv (-1)^{-1} \equiv -1 \pmod 4$.
Thus $3^{3^{3^{3^3}}} \equiv 3^{-1} \equiv 2 \pmod 5$.
Edit: Fermat's Little Theorem tells us that for any prime $p$, we have $a^{p-1} \equiv 1 \pmod p$ for all $a \not \equiv 0 \pmod p$. So, in particular, $a^4 \equiv 1 \pmod 5$ provided $a \not \equiv 0 \pmod 5$, which is why we can reduce the exponent modulo 4.