Humble Numbers
当一个数的因子只有2,3,5,7时,它被称为是Humble Numbers,例如1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27是前20个Humble Numbers。
求第100个(1分),第1000个(3分),和第5842个(4分)Humble Numbers
我的算法:
public class HNList {
public static void main(String[] args) throws Exception {
int[] hnList = new int[5842];
hnList[0] = 1;
int i2 = 0;
int i3 = 0;
int i5 = 0;
int i7 = 0;
for (int i=1;i < hnList.length;i++){
hnList[i] = getMinNumber(2*hnList[i2],3*hnList[i3],5*hnList[i5],7*hnList[i7]);
if (hnList[i]==2*hnList[i2]){
i2++;
}
if (hnList[i]==3*hnList[i3]){
i3++;
}
if (hnList[i]==5*hnList[i5]){
i5++;
}
if (hnList[i]==7*hnList[i7]){
i7++;
}
}
System.out.println(hnList[100-1]);
System.out.println(hnList[1000-1]);
System.out.println(hnList[5842-1]);
}
public static int getMinNumber(int i1,int i2,int i3,int i4){
int iRtn = 0;
iRtn = getMinNumber(getMinNumber(i1,i2),getMinNumber(i3,i4));
return iRtn;
}
public static int getMinNumber(int i1,int i2){
int iRtn = i1;
if (i1 > i2){
iRtn = i2;
}
return iRtn;
}
}