Problem 30: Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
class runner
{
public static void main (String[] args) throws java.lang.Exception
{
long time = System.currentTimeMillis();
int[] storage = new int[10];
//populate hashmap
for(int i=1; i<10; i++){
storage[i] = (int) Math.pow(i,5);
}
int match_sum = 0;
int i=2;
while(true){
int cur = i; int sum = 0;
while(cur>0){
int div = cur % 10;
sum += storage[div];
cur /= 10;
}
//System.out.println(i + " sum:" +sum);
if(i == sum){
match_sum+=i;
}else if(i > 999999){//limit.. 999999 = 354294
break;
}
i++;
}
System.out.println(match_sum);
System.out.println("time:"+(System.currentTimeMillis()-time));
}
}
Note: Limit is 999999 as 9^5*6 = 354294; actual limit is left for reader to solve.