繁体   English   中英

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

[英]Why is my program not giving the expected output?

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

考虑a ^ b的所有整数组合,2≤a≤5且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

如果它们按数字顺序放置,删除任何重复,我们得到以下15个不同术语的序列:

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

ab为2≤a≤100和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

执行时间:24.827827ms

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

line (int) (Math.pow(i, j))没有多大意义,因为100^100Integer.MAX_VALUE大得多。 你应该使用BigInteger来做功能。

它应该是

temp = BigInteger.valueOf(i).pow(j);

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

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

JLS的5.1.3节涵盖了这种缩小的原始转换

否则,以下两种情况之一必须为真:

  • 该值必须太小(大幅度或负无穷大的负值),第一步的结果是int或long类型的最小可表示值。

  • 该值必须太大(大幅度或正无穷大的正值),第一步的结果是int或long 类型最大可表示值

(强调我的)

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM