Search code examples
javapow

How to power up a huge number in java?


I looking for a way to power up a number and finding mod, something like this in python
pow(x, y, z)
for example x ^ y % z
x is about 3 digits, y is about 450 digits, and z is about 400 digits
thanks in advance


Solution

  • The method you are looking for: public BigInteger modPow(BigInteger exponent,BigInteger m)

    Usage:

    BigInteger base= new BigInteger("111");
    BigInteger exponent= new BigInteger(yourExponent);
    BigInteger m= new BigInteger(yourM);
    System.out.println(base.modPow(exponent,m));
    

    For limitations of BigInteger see this question