-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem034.cpp
More file actions
32 lines (30 loc) · 766 Bytes
/
problem034.cpp
File metadata and controls
32 lines (30 loc) · 766 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
#include <iostream>
#include <vector>
std::vector<int> digitFactorials()
{
int fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
std::vector<int> results;
// The upper limit is 7 * 9! since the maximum number of digits is 7.
for (int i = 3; i <= 2540160; ++i) {
int x = i;
int digitFactSum = 0;
do {
digitFactSum += fact[x % 10];
x /= 10;
} while (x != 0);
if (digitFactSum == i) {
results.push_back(i);
}
}
return results;
}
int main()
{
int ans = 0;
std::vector<int> results = digitFactorials();
for (int i = 0; i < results.size(); ++i) {
ans += results[i];
}
std::cout << "Answer: " << ans << '\n';
return 0;
}