2011-08-18 68 views

回答

0

相反的:

(int)(Math.pow(51,43)%(double)77) 

做:

(int)(Math.pow(51,43))%((double)77) 
+0

是否会产生正确的输出?...我怀疑它。 –

+0

不知道,看起来像你错过了一些括号。 – sbrichards

+0

不是我。我会这样做的代数方式...... –

1

一个double没有足够的精度持有Math.pow(51,43)所有数字。所以,当你拿它mod 77,答案很容易出现重大的舍入误差。

我建议使用BigInteger进行任意精度整数运算。

2
final BigInteger base = BigInteger.valueOf(51); 
    final BigInteger exponent = BigInteger.valueOf(43); 
    final BigInteger modulus = BigInteger.valueOf(77); 
    System.out.println(base.modPow(exponent, modulus)); 

打印2