File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff line change 1
1
#include <stdio.h>
2
2
#include <stdint.h>
3
3
4
- #define MAX 2177280
4
+ /* The upper search bound is 2540160. If the upper bound has 6 digits, the
5
+ * maximum factorial sum that can be obtained is 6*9! = 2177280 > 999999. So
6
+ * every numbers between 1 and 999999 can be a curious number. If the upper
7
+ * bound has 7 digits, the maximum achievable sum is 7*9! = 2540160, so all
8
+ * the numbers after 2540160 can not be a curious number as the sum of the
9
+ * factorial of its digits will be less than 2540160 so less than itself
10
+ * (therefore not equal).
11
+ */
12
+ #define MAX 2540160
5
13
6
14
int32_t factorial [] = {
7
15
1 , /* 0! */
@@ -16,6 +24,8 @@ int32_t factorial[] = {
16
24
362880 /* 9! */
17
25
};
18
26
27
+ /* is_sum_factorial: return 1 if n is equal to the sum of the factorial of its
28
+ * digits. */
19
29
int32_t is_sum_factorial (int32_t n )
20
30
{
21
31
int32_t sum , i ;
@@ -29,6 +39,8 @@ int32_t main(void)
29
39
{
30
40
int32_t n , answer ;
31
41
42
+ /* start at 3 because 1! and 2! are no included according to the problem
43
+ * statement. */
32
44
for (n = 3 , answer = 0 ; n <= MAX ; ++ n )
33
45
if (is_sum_factorial (n ))
34
46
answer += n ;
You can’t perform that action at this time.
0 commit comments