When I do the following calculation (97 ^ 23) mod 187
in Java:
double ret = (Math.pow (97, 23))% 187;
ret value is 74
. The value of (Math.Pow (97, 23))
is 4.963064143419832E45;
My problem is the following: the value of the expression (97 ^ 23) mod 187 = 58
. Try to do the windows calculator. I've tried using BigDecimal
and still can not get the value I need.
Making (97 ^ 23)
in the windows calculator it returns 4.9630641434198319969863989680919 +45
and so much more precise.
If someone can help me, thank you very much!
Modular power is implemented in the BigInteger
class:
BigInteger a = new BigInteger("97");
BigInteger x = new BigInteger("23");
BigInteger m = new BigInteger("187");
BigInteger result = a.modPow(x, m); // 97^23 % 187
To get the result as an int you can use the intValue()
method.