![]() | ![]() |
The idea behind fast exponentiation is a simple one. If we are trying to evaluate ak(modm), we have two cases to consider.
Case 1
If k is a power of 2, we can find ak by successive squaring, reducing the numbers involved(modm) along the way to keep the magnitudes of the values we are dealing with reasonable.
For example, to find 764(mod13), we make the following calculations: 71≡772≡49≡1074≡100≡978≡81≡3716≡9732≡3764≡9 Case 2
If k is not a power of 2, we can write it as a sum of powers of 2 (i.e., its base 2 representation), find the related powers using the calculations suggested in Case 1 above, and then multiply the results together(modm).
For example, to find 787(mod13), we observe that: 87=64+16+4+2+1 So, 787=764⋅716⋅74⋅72⋅71≡9⋅9⋅9⋅10⋅7≡729⋅70≡1⋅5≡5(mod13)