Algorithm

Note

When b is huge, and a and c are coprime, Euler's theorem applies:
 a^b≡a^bmodϕ(c) mod c