简体   繁体   中英

Why is my program not giving the expected output?

I am working on problem 29 of Project Euler which states:

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?

I have written the prgram to solve this problem in 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

Execution time: 24.827827ms

After inputting this answer on project euler, we can see that it is incorrect; 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 . You should do the powers using 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 .

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 .

Section 5.1.3 of the JLS covers this narrowing primitive conversion :

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.

(emphasis mine)

You get that result a lot, so there are only 422 distinct results.

Change your calculation to BigInteger math.

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

With that change, I now get 9,183 distinct elements. That makes more sense, since some powers of perfect squares will be repeated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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