Modular Exponentiation Algorithm

Modular Exponentiation Algorithm raises a number to a power and then takes the mod of it. This page uses the Right-to-Left Binary Method which is derived from the Square and Multiply Algorithm.

Start with the rightmost 1, and write down your number (to the 1st power).
For every 1, there after, square what you have, and multiply by your original number (to the 1st power).
For every 0, after the first 1, just square what you have.