ph-math/src/main/java/com/helger/numbercruncher/mathutils/ModuloArithmetic.java
public static int raise (final int pbase, final int pexponent, final int m)
  {
    int base = pbase;
    int exponent = pexponent;
    int power = 1;

    // Loop to compute power = (base^exponent)%m.
    while (exponent > 0)
    {

      // Does the rightmost bit of the exponent == 1?
      if ((exponent & 1) == 1)
      {
        power = multiply (power, base, m);
      }

      // Square the base modulo m and
      // shift the exponent 1 bit to the right.
      base = multiply (base, base, m);
      exponent >>= 1;
    }

    return power;
  }
public static int raise (final int pbase, final int pexponent, final int m)
  {
    int base = pbase;
    int exponent = pexponent;
    int power = 1;

    // Loop to compute power = (base^exponent)%m.
    while (exponent > 0)
    {

      // Does the rightmost bit of the exponent == 1?
      if ((exponent & 1) == 1)
      {
        power = multiply (power, base, m);
      }

      // Square the base modulo m and
      // shift the exponent 1 bit to the right.
      base = multiply (base, base, m);
      exponent >>= 1;
    }

    return power;
  }
Raise a to the b power modulo m.

@param pbase
the value of a
@param pexponent
the value of b
@param m
the modulus m
@return the value of a^b (mod m)