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 .