Binary Exponentiation
Binary exponentiation is a trick that allows us to calculate in instead of .
Resources
I still need to understand, this recursive relationship (Specifically how to deal with these odd powers), but I get the overall idea.
Recursive code
You always get the largest power, and you halve it each time.
Iterative version (faster, duh)
Modular Exponentiation
See Modular Exponentiation for implementation of .