// Solution to Project Euler #34
// sum of factorial of digits
#include <stdio.h>
#include <stdlib.h>
main() {
int i, sum=0;
for (i=10; i<2540160; i++) { // 2540160 is the upper bound: 9! * 7
int temp=i, tempsum=0;
while (temp) {
tempsum += factorial(temp%10);
temp /= 10;
}
if (tempsum == i) {
printf("Found: %d\n", i);
sum += i;
}
}
printf("\nSolution: %d\n", sum);
}
if (i<0 || i>12) {
fprintf(stderr, "Factorial input out of range\n");
exit(EXIT_FAILURE);
}
static const int factorials[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600};
return factorials[i];
}