In this post I will be discussing a method to find **(a ^{b})%MOD**. The problem is simple, we have been doing it from long time to find a

^{b}. But as

**b**increases, we may think of writing a code for it. Say a simple method like this:

long long int modPow(long long int m, long long int n, long long int mod) { long long int p = 1; for(int i = 0;i < n;i++) { p *= m; p %= mod; } return mod; }

Above mentioned function solves the need. But is not efficient. Complexity of the function is O(n) and hence cannot find required answer for large values of b or n(for n values > 10^{8}) in reasonable time. This can be optimised using Modular Exponentiation Algorithm. This algo solves the problem in O(log n) complexity. Here is the function:

long long int fastModPow(long long int a, long long int b, long long int mod) { long long int ans = 1; while(b > 0) { if(b%2 == 1) ans = (ans*a)%mod; a = (a*a)%mod; b /= 2; } return ans; }

How this function works?

Let us solve for 5^{50}. Leave the modulus as it is just an additional operation that is done commonly at each step.

bin(50) = 110010

Initial values : ans = 1 a = 5 b = 50

Step 1 : ans = 1 a = 5*5 b = 25

Step 2 : ans = 1 * 5^{2} a = 5^{2} * 5^{2} b = 12

Step 3 : ans = 5^{2} a = 5^{4} * 5^{4} b = 6

Step 4 : ans = 5^{2} a = 5^{8} * 5^{8} b = 3

Step 5 : ans = 5^{2} * 5^{16} a = 5^{16} * 5^{16} b = 1

Step 6 : ans = 5^{18} * 5^{32} a = 5^{32} * 5^{32} b = 0

We are left with ans = 5^{50} which is the desired result. As we move from LSB to MSB in binary form of exponent, compare the respective steps shown above. The calculations followed are different for the bit values 1 and 0.