简体   繁体   English

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

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

I am working on problem 29 of Project Euler which states: 我正在研究Euler项目的问题29,其中指出:

Consider all integer combinations of a^b for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: 考虑a ^ b的所有整数组合,2≤a≤5且2≤b≤5:

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

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

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

5^2=25, 5^3=125, 5^4=625, 5^5=3125 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: 如果它们按数字顺序放置,删除任何重复,我们得到以下15个不同术语的序列:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 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? ab为2≤a≤100和2≤b≤100产生的序列中有多少个不同的项?

I have written the prgram to solve this problem in Java. 我已经编写了用Java解决这个问题的程序。 it runs without any errors, however, is not giving the expected output. 它运行时没有任何错误,但是没有给出预期的输出。 I have provided my code and the output below. 我在下面提供了我的代码和输出。

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");
  }
}

Output: 输出:

422 422

Execution time: 24.827827ms 执行时间:24.827827ms

After inputting this answer on project euler, we can see that it is incorrect; 在项目euler上输入这个答案后,我们可以看到它是不正确的; but I can not see where I have gone wrong in my code. 但我无法看到我的代码出错了。

The line (int) (Math.pow(i, j)) doesn't make a lot of sense as 100^100 is much bigger than Integer.MAX_VALUE . line (int) (Math.pow(i, j))没有多大意义,因为100^100Integer.MAX_VALUE大得多。 You should do the powers using BigInteger . 你应该使用BigInteger来做功能。

It should be 它应该是

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

The reason you are getting only 422 distinct elements is that your values are so large and you're casting them to an int . 你只得到422个不同元素的原因是你的值太大而你正在将它们转换为int

When the result of Math.pow(i, j) (a double ) is larger than Integer.MAX_VALUE and you cast to an int , then the result is Integer.MAX_VALUE . Math.pow(i, j) (一个double )的结果大于Integer.MAX_VALUE并且转换为int ,结果为Integer.MAX_VALUE

Section 5.1.3 of the JLS covers this narrowing primitive conversion : JLS的5.1.3节涵盖了这种缩小的原始转换

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. 该值必须太小(大幅度或负无穷大的负值),第一步的结果是int或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. 该值必须太大(大幅度或正无穷大的正值),第一步的结果是int或long 类型最大可表示值

(emphasis mine) (强调我的)

You get that result a lot, so there are only 422 distinct results. 你得到的结果很多,所以只有422个不同的结果。

Change your calculation to BigInteger math. 将您的计算更改为BigInteger数学。

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

With that change, I now get 9,183 distinct elements. 随着这一变化,我现在获得了9,183个不同的元素。 That makes more sense, since some powers of perfect squares will be repeated. 这更有意义,因为一些完美正方形的力量将被重复。

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

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