private int modularExponentation ( int a , int b , int n ) throws IllegalArgumentException { int result = 1 ; int counter ; int maxBinarySize = 32 ; boolean [ ] b2Binary = new boolean [ maxBinarySize ] ; for ( counter = 0 ; b > 0 ; counter ++ ) { if ( counter >= maxBinarySize ) { throw new IllegalArgumentException ( "Exponent " + b + " is too big !" ) ; } b2Binary [ counter ] = ( b % 2 != 0 ) ; b = b / 2 ; } for ( int k = counter - 1 ; k >= 0 ; k -- ) { result = ( result * result ) % n ; if ( b2Binary [ k ] ) { result = ( result * a ) % n ; } } return result ; }
Gets the modular exponentiation i . e . result of a^b mod n . Use to calculate hashcode .