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