What is the fastest way (general method) to calculate the quantity $a^b \!\mod c$? For example $a=2205$, $b=23$, $c=4891$.
calculating $a^b \!\mod c$
5.7k Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail AtThere are 5 best solutions below
On
One common way of fast exponentiation, either in modular arithmetic or in general, is exponentiation by squaring. Here is the section where they talk specifically about $a^b\bmod c$.
On
The Russian peasant's method is pretty straightforward for computing $a^b\bmod m$:
c=a;d=b;r=1;
while d≠0
if d is odd then r=(cr) mod m;
d=d div 2; \\ integer division; one usually right-shifts bits in practice
c=c^2 mod m;
endwhile
r
On
Generally repeated squaring works well. It deserves to be better known that this arises simply from writing the exponent in binary radix in Horner polynomial form, i.e. $\rm\ d_0 + x\ (d_1 + x\ (d_2\ +\:\cdots))\:.\ $ Below is an example of computing $\rm\ a^{101}\ $ by repeated squaring. Note that the repeated square form arises simply from performing various substitutions into the binary polynomial Horner form namely $\rm\ 1\to a,\ \ 0\to 1,\ \ (x)\:2\to (x)^2\ $ into $101_{10} = 1100101_2\ $ expanded into Horner form, viz.

Let's assume that
a,b,creferred to here are positive integers, as in your example.For a specific exponent
b, there may be a faster (shorter) method of computinga^bthan binary exponentiation. Knuth has a discussion of the phenomenon in Art of Computer Programming Vol. 2 (Semi-numerical Algorithms), Sec. 4.6.3 and the index term "addition chains". He citesb=15as the smallest case where binary exponentiation is not optimal, in that it requires six multiplication buta^3can be computed in two multiplications, and then(a^3)^5in three more for a total of five multiplications.For the specific exponent
b=23the parsimonious addition chain involves the exponents (above1)2,3,5,10,13, at which pointa^23 = (a^10)*(a^13), for a total of six multiplications. Binary exponentiation forb=23requires seven multiplications.Another approach that can produce faster results when
bis large (not in your example) depends on knowing something about the baseaand modulusc. Recall from Euler's generalization of Fermat's Little Thm. that ifa,care coprime, thena^d = 1 mod cfordthe Eulerphifunction ofc(the number of positive integers less thancand coprime to it). In particular ifcis a prime, then by Fermat's Little Thm. eithercdividesaanda^b = 0 mod cor elsea^b = a^e mod cwheree = b mod (c-1)sincephi(c) = c-1for a primec.If the base
aand moduluscare not coprime, then it might be advantageous to factorainto its greatest common factor withcand its largest factor that is coprime toc.Also it might be advantageous if
cis not prime to factor it into prime powers and do separate exponentiations for each such factor, piecing them back together via the Chinese Remainder Thm. In your examplec = 4891 = 67*73, so you might computea^b mod 67anda^b mod 73and combine those results to geta^b mod c. This is especially helpful if you are limited in the precision of integer arithmetic you can do.