modExp function

int modExp(
  1. int base,
  2. int exp,
  3. int mod
)

============================================= Modular exponentiation.

Implementation

int modExp(int base, int exp, int mod) {
  int result = 1;
  base = base % mod;
  while (exp > 0) {
    if ((exp & 1) == 1) {
      result = (result * base) % mod;
    }
    base = (base * base) % mod;
    exp >>= 1;
  }
  return result;
}