algorithm for modular exponentiation

Whereas even for fairly small bases and exponents the results can be too large for calculation with pencil and paper or even with a calculator, there is a fairly simple algorithm to solve for x in the congruenceMathworldPlanetmathPlanetmathPlanetmathPlanetmath abxmodc.

  1. 1.

    Assign x=1, y=a and z=b.

  2. 2.

    If z is even, then halve z and reassign y to its square, then reassign y to its remainder modulo c. But if z is odd, decrement z by 1, and reassign to x its productPlanetmathPlanetmath times y, and then reassign x to its remainder modulo c.

  3. 3.

    Test if z=0. If not, repeat the previous step.

  4. 4.

    Return x.

For example, solve 17912=xmod13. At the first step we have x=1, y=179 and z=12.

At the second step, since z is even, we reassign z=6 and square 179, which is 32041, and that is 9 modulo 13.

z is not zero yet, so we go back to the second step, and z is still even, so we halve it to 3. The square of 9 is 81, which is just 3 over 78, the nearest multipleMathworldPlanetmath of 13, so now y=3.

On the third iteration of the second step z is now odd, so we decrease it to 2 and multiply x (which is still 1 right now) by y, which is now 3, so now x=3.

On the next iteration of the second step z is again even, but with it being 2 either halving it or decrementing sets z=1, though of course we gon on with the halving branch of the algorithm: y is still 3, and its square 9 is less than 13.

Now on the las iteration of the second step, z is odd so we decrement it down to 0. x is 3 (set back on the third iteration of the second step) and 3 times 9 is 27, which is 1 more than twice 13, so now x=1.

Since z is now 0, our answer should be in x, which is 1. As we know that 13 is a prime numberMathworldPlanetmath and 179 is coprimeMathworldPlanetmath to 13, by Fermat’s little theoremMathworldPlanetmath we know the answer must be 1.

In this run of the algorithm four squaring operationsMathworldPlanetmath were necessary as well as various additions and subtractionsPlanetmathPlanetmath, but all this could have easily been accomplished easily either with pencil or paper or with the help of a basic calculator with only a 10-digit display. To actually raise 179 to the 12th power with the necessary accuracy on paper would have required eleven multiplications with plenty of opportunity for errors to creep into the calculation. With Mathematica it is a snap to verify that the answer is 1082022699327332498100696241, and that 83232515332871730623130480 goes into 1082022699327332498100696240 thirteen times, but there are much larger numbers still which would be beyond Mathematica’s range to exponentiate yet which the remainders could easily be obtained with this algorithm.


Title algorithm for modular exponentiation
Canonical name AlgorithmForModularExponentiation
Date of creation 2013-03-22 18:09:11
Last modified on 2013-03-22 18:09:11
Owner PrimeFan (13766)
Last modified by PrimeFan (13766)
Numerical id 4
Author PrimeFan (13766)
Entry type Algorithm
Classification msc 11N25