How do I compute $a^b\,\bmod c$ by hand?

Wikipage on modular arithmetic is not bad.

  • When $b$ is huge, and $a$ and $c$ are coprime, Euler's theorem applies: $$ a^b \equiv a^{b \, \bmod \, \phi(c)} \, \bmod c $$ For the example at hand, $\phi(21) = \phi(3) \times \phi(7) = 2 \times 6 = 12$. $ 844325 \bmod 12 = 5$, so $5^5 = 5 \times 25^2 \equiv 5 \times 4^2 = 80 \equiv 17 \mod 21$.

  • When $a$ and $c$ are coprime, but $0<b<\phi(c)$, repeated squaring (or using other compositions of powers) is the fastest way to go (manually): $$ \begin{eqnarray} 5^4 \equiv 5 \times 5^3 \equiv 5 \times 24 \equiv 19 &\pmod{101}\\ 19^4 \equiv (19^2)^2 \equiv 58^2 \equiv (-43)^2 \equiv 1849 \equiv 31 &\pmod{101} \\ 31^4 \equiv (31^2)^2 \equiv (961)^2 \equiv 52^2 \equiv 2704 \equiv 78 &\pmod{101} \\ 5^{69} \equiv 5 \times 5^4 \times ((5^4)^4)^4 \equiv 5 \times 19 \times 78 \equiv 5 \times 19 \times (-23)\\ \equiv 19 \times (-14) \equiv -266 \equiv 37 & \pmod{101} \end{eqnarray} $$

  • When $a$ and $c$ are not coprime, let $g = \gcd(a,c)$. Let $a = g \times d$ and $c = g \times f$, then, assuming $b > 1$: $$ a^b \bmod c = g^b \times d^b \bmod (g \times f) = ( g \times (g^{b-1} d^b \bmod f) ) \bmod c $$ In the example given, $\gcd(6,14) = 2$. So $2^{102} \times 3^{103} \mod 7$, using Euler'r theorem, with $\phi(7) = 6$, and $102 \equiv 0 \mod 6$, $2^{102} \times 3^{103} \equiv 3 \mod 7$, so $6^{103} \equiv (2 \times 3) \equiv 6 \mod 14 $.


Let's try $5^{844325} \bmod 21$: $$ \begin{align} 5^0 & & & \equiv 1 \\ 5^1 & & &\equiv 5 \\ 5^2 & \equiv 25 & & \equiv 4 \\ 5^3 & \equiv 4\cdot 5 & & \equiv 20 \\ 5^4 & \equiv 20\cdot 5 & & \equiv 16 \\ 5^5 & \equiv 16\cdot 5 & & \equiv 17 \\ 5^6 & \equiv 17\cdot 5 & & \equiv 1 \end{align} $$ So multiplying by $5$ six times is the same as multiplying by $1$. We want to multiply by $5$ a large number of times: $844325$. How many times do we multiply by $5$ six times? The number of times $6$ goes into $844325$ is $140720$ with a remainder of $5$. That remainder is what matters. Multiply by $5^6$ exactly $140720$ times and that's the same as multiplying by $1$ that many times. Then multiply by $5$ just $5$ more times, and get $17$.

So $5^{844325} \equiv 17 \bmod 21$.


Here are two examples of the square and multiply method for $5^{69} \bmod 101$:

$$ \begin{matrix} 5^{69} &\equiv& 5 &\cdot &(5^{34})^2 &\equiv & 37 \\ 5^{34} &\equiv& &&(5^{17})^2 &\equiv& 88 &(\equiv -13) \\ 5^{17} &\equiv& 5 &\cdot &(5^8)^2 &\equiv& 54 \\ 5^{8} &\equiv& &&(5^4)^2 &\equiv& 58 \\ 5^{4} &\equiv& &&(5^2)^2 &\equiv& 19 \\ 5^{2} &\equiv& &&(5^1)^2 &\equiv& 25 \\ 5^{1} &\equiv& 5 &\cdot &(1)^2 &\equiv& 5 \end{matrix} $$

The computation proceeds by starting with $5^{69}$ and then working downward to create the first two columns, then computing the results from the bottom up. (normally you'd skip the last line; I put it there to clarify the next paragraph)

As a shortcut, the binary representation of $69$ is $1000101_2$; reading the binary digits from left to right tell us the operations to do starting from the value $1$: $0$ says "square" and $1$ says "square and multiply by $5$".


The other way is to compute a list of repeated squares:

$$ \begin{matrix} 5^1 &\equiv& 5 \\ 5^2 &\equiv& 25 \\ 5^4 &\equiv& 19 \\ 5^8 &\equiv& 58 \\ 5^{16} &\equiv& 31 \\ 5^{32} &\equiv& 52 \\ 5^{64} &\equiv& 78 \end{matrix} $$

Then work out which terms you need to multiply together:

$$ 5^{69} \equiv 5^{64 + 4 + 1} \equiv 78 \cdot 19 \cdot 5 \equiv 37 $$