code: 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;
      }
docs: Gets the modular exponentiation, i.e. result of  a^b mod n. Use to calculate hashcode.
  @param a A number.
  @param b An exponent.
  @param n A modulus.
  @return Result of modular exponentiation, i.e. result of  a^b mod n.