Fast Modular Exponentiation (be % n): This algorithm is commonly used in public key cryptography. In cryptography, base b and power e are both large numbers. Multiplying b by itself (e - 1) times is not practical.
Algorithm Details: The last equation shows the pattern of repeatedly squaring b
e = ei · 2i + ei - 1 · 2i - 1 + ei - 2 · 2i - 2 + ··· + e1 · 21 + e0 · 20
where ei is the ith bit of e
b e % n = ( b ei · 2i + ei - 1 · 2i - 1 + ei - 2 · 2i - 2 + ··· + e1 · 21 + e0 · 20 ) % n
b e % n = ( bei · 2i · bei - 1 · 2i - 1 · bei - 2 · 2i - 2 · ··· · be1 · 21 · be0 · 20 ) % n