Problem 34: Note: 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Since 1! = 1 and 2! = 2 are not sums they are not included.
class runner
{
public static void main (String[] args) throws java.lang.Exception
{
long time = System.currentTimeMillis();
int[] factorials = {1,1,2,6,24,120,720,5040,40320,362880};
int sum =0;
/* Notice the Limit:
* 900000 = 362880
* 990000 = 725760
* 9990000 = 1088640
* 9999000 = 1451520
* 9999900 = 1811440
* 9999990 = 2177280;
* Note: limit>9999990 as that can be written as 1999999
*
* 9999999 = 2540160
*/
int limit = 9999999;
for(int i=3;i<=limit;i++){
int n=i, n_sum=0;
while(n>0){
n_sum+=factorials[n%10];
n/=10;
}
if(n_sum==i){
//System.out.println(i+" "+n_sum);
sum+=n_sum;
}
}
System.out.println(sum);
System.out.println("time: "+(System.currentTimeMillis() - time));
}
}