Skip to content

Instantly share code, notes, and snippets.

@ssysm
Created September 19, 2019 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssysm/ed699c397c4a4b3f293a7395cc4edc16 to your computer and use it in GitHub Desktop.
Save ssysm/ed699c397c4a4b3f293a7395cc4edc16 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
public class DistinctPowers{
//Check if number exist in array
private static boolean isNumberExist(ArrayList<Double> arr, double cmp){
for(double d: arr){
if(d == cmp){
return true;
}
}
return false;
}
public static void main(String[] args) throws ParseException{
int a_min_base = 2;
int a_max_base = 100;
int b_min_power = 2;
int b_max_power = 100;
ArrayList<Double> products = new ArrayList<Double>();
// compute and popluate array
for(int base = a_min_base; base <= a_max_base; base++){
for(int pow = b_min_power; pow <= b_max_power; pow++){
double prod = Math.pow(base, pow);// compute product
if(!isNumberExist(products,prod)){ // does number exist?
products.add(prod); // push to array
}
}
}
System.out.println(products.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment