-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy patheuler-0034.cpp
76 lines (68 loc) · 1.92 KB
/
euler-0034.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// ////////////////////////////////////////////////////////
// # Title
// Digit factorials
//
// # URL
// https://projecteuler.net/problem=34
// http://euler.stephan-brumme.com/34/
//
// # Problem
// `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.
//
// # Solved by
// Stephan Brumme
// February 2017
//
// # Algorithm
// This problem is very similar to problem 30.
//
// There is no 8-digit number which can be the sum of the factorials of its digits because `8 * 9! = 2903040` is a 7-digit number.
//
// I precomputed the factorials `0!` to `9!` instead of writing a short and simple __factorial__ function.
// Each number is split into its digits (again I begin with the least-significant, "I chop them from the right side")
// and then the factorial of these digits is looked up and added.
//
// Nothing spectacular - a very easy problem.
//
// # Hackerrank
// The sums must be divisible by the number, not equal.
#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;
}