java – 为什么我的程序没有给出预期的输出?

我正在研究Euler项目的问题29,其中指出:

Consider all integer combinations of a^b for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

2^2=4, 2^3=8, 2^4=16, 2^5=32

3^2=9, 3^3=27, 3^4=81, 3^5=243

4^2=16, 4^3=64, 4^4=256, 4^5=1024

5^2=25, 5^3=125, 5^4=625, 5^5=3125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

我已经编写了用Java解决这个问题的程序.它运行时没有任何错误,但是没有给出预期的输出.我在下面提供了我的代码和输出.

import java.util.ArrayList;
import java.math.BigInteger;
public class problemTwentyNine {
  public static void main(String [] args) {
    long start = System.nanoTime();
    ArrayList<BigInteger> distinctTerms = new ArrayList<BigInteger>();
    BigInteger temp;
    for(int i = 2; i <= 100; i++) {
      for(int j = 2; j <= 100; j++) {
        temp = new BigInteger("" + (int) (Math.pow(i, j)));
        if(!distinctTerms.contains(temp)) {
          distinctTerms.add(temp);
        }
      }
    }
    System.out.println(distinctTerms.size());
    long stop = System.nanoTime();
    System.out.println("Execution time: " + ((stop - start) / 1e+6) + "ms");
  }
}

输出:

422

Execution time: 24.827827ms

在项目euler上输入这个答案后,我们可以看到它是不正确的;但我无法看到我的代码出错了.

解决方法:

你只得到422个不同元素的原因是你的值太大而你正在将它们转换为int.

当Math.pow(i,j)(一个double)的结果大于Integer.MAX_VALUE并且转换为int时,结果为Integer.MAX_VALUE.

Section 5.1.3 of the JLS涵盖了这种缩小的原始转换:

Otherwise, one of the following two cases must be true:

  • The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.

  • The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

(强调我的)

你得到的结果很多,所以只有422个不同的结果.

将您的计算更改为BigInteger数学.

temp = new BigInteger("" + i).pow(j);

随着这一变化,我现在获得了9,183个不同的元素.这更有意义,因为一些完美正方形的力量将被重复.

上一篇:java – 如何使用BigInteger类?


下一篇:Java – BigInteger奇怪的行为