-2
我有以下代碼
BigInteger.pow()在最後2次迭代之前不起作用?
public static void main(String[] args) {
BigInteger iter = BigInteger.valueOf(140);
BigInteger y = BigInteger.valueOf(1114112);
BigInteger sum = BigInteger.valueOf(0);
while(iter.intValue() != 0) {
BigInteger z = BigInteger.valueOf((y.pow(iter.intValue())).longValue());
sum = sum.add(z);
iter = iter.subtract(BigInteger.valueOf(1));
System.out.println("Itereration: " + (140 - iter.longValue()));
System.out.println("Y: " + y.longValue());
System.out.println("Z: " + z.longValue());
System.out.println("Sum: " + sum.longValue());
}
}
然而,輸出是這個(僅最後3次迭代)
Iteration: 137
Y: 1114112
Z: 0
Sum: 0
Iteration: 138
Y: 1114112
Z: 1382886560579452928
Sum: 1382886560579452928
Iteration: 139
Y: 1114112
Z: 1241245548544
Sum: 1382887801825001472
Iteration: 140
Y: 1114112
Z: 1114112
Sum: 1382887801826115584
迭代1-136的其餘部分是相同的迭代137
邊注:爲什麼地球上是'iter'一個'BigInteger'? – 2017-03-18 13:04:18
你的問題是什麼? –
停止將您的BigInteger轉換成長整型和整數... –