Skip to content

Commit ae5f15b

Browse files
committed
add explanations for problem 34
1 parent 6541902 commit ae5f15b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

problem_026-050/problem_034.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#include <stdio.h>
22
#include <stdint.h>
33

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
513

614
int32_t factorial[] = {
715
1, /* 0! */
@@ -16,6 +24,8 @@ int32_t factorial[] = {
1624
362880 /* 9! */
1725
};
1826

27+
/* is_sum_factorial: return 1 if n is equal to the sum of the factorial of its
28+
* digits. */
1929
int32_t is_sum_factorial(int32_t n)
2030
{
2131
int32_t sum, i;
@@ -29,6 +39,8 @@ int32_t main(void)
2939
{
3040
int32_t n, answer;
3141

42+
/* start at 3 because 1! and 2! are no included according to the problem
43+
* statement. */
3244
for (n = 3, answer = 0; n <= MAX; ++n)
3345
if (is_sum_factorial(n))
3446
answer += n;

0 commit comments

Comments
 (0)