145 is a curious number, as 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.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
#define N 1000000
#define M 10
int factorial(int x);
int main()
{
clock_t ts,te;
ts=clock();
int answer = 0;
int t;
long num;
int i,j;
for (i = 3; i < N; i++)
{
t = i;
num = 0;
for (j = 0; t != 0;j++)
{
num += factorial(t%10);
t = t/10;
}
if (num == i)
{
printf("%d\n",i);
answer += i;
}
}
printf("\nanswer %d",answer);
te=clock();
printf("\ntime difference: %ds\n",(te-ts)/CLOCKS_PER_SEC);
getchar();
return 0;
}
int factorial(int x)
{
for (int i = 1,j = 1; i <=x; i++)
{
j *= i;
}
return j;
}