Different powers somewhat Java

0

Problem

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

2 ^ 2 = 4, 23 = 8, 24 = 16, 25 = 32

3 ^ 2 = 9, 33 = 27, 34 = 81, 35 = 243

4 ^ 2 = 16, 43 = 64, 44 = 256, 45 = 1024

5 ^ 2 = 25, 53 = 125, 54 = 625, 55 = 3125

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

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

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

Question

I have the algorithm done, I test it with a = 5, b = 5 and it works fine, with the number 100 I get 422, I'm not sure it's correct since the problem tells me it is not, but I do not detect Any mistake, could you help me out?

Code

    getPowers(100, 100);
    Collections.sort(numbers);
    System.out.println(numbers);
    System.out.println(numbers.size());

}

static List<Integer> numbers = new ArrayList<>();

public static void getPowers(int a, int b){

    double num = 0;

    for (int i = 2; i <=a ; i++) {

        for (int j = 2; j <=b; j++) {

            num = Math.pow(i,j);
            if (!numbers.contains((int)num)) numbers.add((int)num);

        }
    }

}
    
asked by Romulo Gallegos 20.09.2017 в 04:48
source

1 answer

1

You must evaluate the subject well, I made some changes to your code which I leave below and I describe:

  • Change the data structure to a TreeSet, in this way you do not have to validate if the number already exists since this structure already controls it and on the other hand the data is stored in an orderly manner at the time of add them.

  • Change the data type to Long which is a larger number ( 2^64 ) and give me 997 different numbers.

    public static void getPowers (int a, int b) {

    long num = 0;
    TreeSet<Long> numbers = new TreeSet();
    
    for (int i = 2; i <= a; i++) {
        for (int j = 2; j <= b; j++) {
            num = (long) (Math.pow(i, j));
            numbers.add(num);
        }
    }
    
    System.out.println(numbers.size());
    
    for(Long in : numbers){
        System.out.println(in);
    }
    

    }

  • If I understand the exercise, you must do a double cycle so that all the calculations of type a^b are stored where 2 <= a <= 100 and 2 <= b <= 100 , which would give a series of the form 2^2, 2^3, ... 2^100, 3^2, 3^3, ... 3^100, ... 100^2, 100^3, ... 100^100 , if this is so you must think of a data type like BigInteger since the number 2^100 is much greater than 2^64 which is the maximum allowed by a data type Long , and the largest number to calculate is 100^100

  • Next I leave the code with the implementation changing the Data Type for BigInteger

    public static void getPowers(int a, int b) {
            TreeSet<BigInteger> numbers = new TreeSet();
            BigInteger numbi;
            for(int i = 2; i <= a; i++){
                numbi = new BigInteger(String.valueOf(i));
                for(int j = 2; j <= b; j++){
                    numbers.add(numbi.pow(j));
                }
            }
            System.out.println(numbers.size());
            for(BigInteger in : numbers){
                System.out.println(in);
            }
        }
    
        
    answered by 20.09.2017 / 07:26
    source