我正在研究一种算法来检查数字是否为素数并且需要处理非常大的数字,因此我正在使用 BigInteger 类。问题是抛出这个异常ArithmeticException BigInteger 会溢出支持的范围。
Exception in thread "main" java.lang.ArithmeticException: BigInteger would overflow supported range
at java.math.BigInteger.reportOverflow(Unknown Source)
at java.math.BigInteger.checkRange(Unknown Source)
at java.math.BigInteger.<init>(Unknown Source)
at java.math.BigInteger.shiftLeft(Unknown Source)
at java.math.BigInteger.pow(Unknown Source)
at Kitas.main(Kitas.java:118)
以及引发异常的行:
b = BigInteger.valueOf(2).pow((int) (35*(Math.pow(2, counter))));
一旦计数器达到 26 的值,就会引发异常。