-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 34
More file actions
39 lines (32 loc) · 846 Bytes
/
Copy pathproblem 34
File metadata and controls
39 lines (32 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*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.
*/
#include <iostream>
#include<string>
using namespace std;
int factorial(int num){
int factorial[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
return factorial[num];
}
int factorial_digits(int num){
int total_fact = 0;
while(num){
int first = num % 10;
total_fact += factorial(first);
num = num / 10;
}
return total_fact;
}
int main(){
int sum_of_all_curious = 0;
const int LIMIT = 100000;
for(int num = 10; num < LIMIT; num++){
int result = factorial_digits(num);
if(result == num){
sum_of_all_curious += result;
}
}
cout << sum_of_all_curious <<endl;
return 0;
}