0

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

khashayar
  • 33
  • 7
  • 2
    Check the `BigInteger` and `BigDecimal` classes. – Susmit Agrawal May 18 '19 at 12:36
  • 1
    I already tried that , second value is very huge and I can't find mod – khashayar May 18 '19 at 12:38
  • 4
    "I can't find mod" .. then you didn't even look. https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#mod-java.math.BigInteger- – Tom May 18 '19 at 12:39
  • 3
    You want [BigInteger.modPow](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#modPow%28java.math.BigInteger,java.math.BigInteger%29). As in, `result = x.modPow(y, z);`. – VGR May 18 '19 at 12:49

1 Answers1

7

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

kkica
  • 4,034
  • 1
  • 20
  • 40