-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler034.cpp
More file actions
44 lines (35 loc) · 832 Bytes
/
Copy pathprojectEuler034.cpp
File metadata and controls
44 lines (35 loc) · 832 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
40
41
42
43
44
/* Problem 34
Digit Factorials
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 i) {
int result = 1;
for (int x = i; x > 0; x--) {
result *= x;
}
return result;
}
int factorialSum (int i) {
string iStr = to_string (i);
int iSum = 0;
for (int x = 0; x < iStr.length (); x++) {
int digitFact = factorial (int (iStr [x] - 48));
iSum += digitFact;
}
return iSum;
}
int main () {
int sumFacts = 0;
for (int i = 3; i < 99999; i++) {
if (i == factorialSum (i)) {
sumFacts += i;
}
}
cout << sumFacts << endl;
}