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 in the congruence .
-
1.
Assign , and .
- 2.
-
3.
Test if . If not, repeat the previous step.
-
4.
Return .
For example, solve . At the first step we have , and .
At the second step, since is even, we reassign and square 179, which is 32041, and that is 9 modulo 13.
is not zero yet, so we go back to the second step, and is still even, so we halve it to 3. The square of 9 is 81, which is just 3 over 78, the nearest multiple of 13, so now .
On the third iteration of the second step is now odd, so we decrease it to 2 and multiply (which is still 1 right now) by , which is now 3, so now .
On the next iteration of the second step is again even, but with it being 2 either halving it or decrementing sets , though of course we gon on with the halving branch of the algorithm: is still 3, and its square 9 is less than 13.
Now on the las iteration of the second step, is odd so we decrement it down to 0. 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 .
Since is now 0, our answer should be in , which is 1. As we know that 13 is a prime number and 179 is coprime to 13, by Fermat’s little theorem we know the answer must be 1.
In this run of the algorithm four squaring operations were necessary as well as various additions and subtractions, 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.
References
- 1 Harold M. Edwards, Higher Arithmetic: An Algorithmic Introduction to Number Theory. Providence: American Mathematical Society (2008): 37
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 |