-
Notifications
You must be signed in to change notification settings - Fork 0
/
day 34
43 lines (36 loc) · 861 Bytes
/
day 34
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
#include <iostream>
int main()
{
// precompute factorials of all possible digits 0!..9!
const unsigned int factorials[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
// no more than 7*9! = 2540160 for the original problem
unsigned int limit;
std::cin >> limit;
// result (differs for Hackerrank modified problem !)
unsigned int result = 0;
for (unsigned int i = 10; i < limit; i++)
{
unsigned int sum = 0;
// split i into its digits
unsigned int x = i;
while (x > 0)
{
// add factorial of the right-most digit
sum += factorials[x % 10];
// remove that digit
x /= 10;
}
#define ORIGINAL
#ifdef ORIGINAL
// equal ?
if (sum == i)
result += i;
#else
// divisible ?
if (sum % i == 0)
result += i;
#endif
}
std::cout << result << std::endl;
return 0;
}