-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem23.c
71 lines (60 loc) · 1.23 KB
/
problem23.c
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
/*
* Copyright (c) 2009, eagletmt
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
*/
#include <stdio.h>
#include <stdlib.h>
static int is_abundant(int n);
int main(void)
{
int i, j;
const int upper_limit = 28123;
int *abundants, abundants_count = 0;
char *expressed;
int sum = 0;
for (i = 1; i < upper_limit; i++) {
if (is_abundant(i)) {
abundants_count++;
}
}
abundants = malloc(abundants_count * sizeof *abundants);
j = 0;
for (i = 1; i < upper_limit; i++) {
if (is_abundant(i)) {
abundants[j++] = i;
}
}
expressed = calloc(upper_limit, sizeof *expressed);
for (i = 0; i < abundants_count; i++) {
for (j = i; j < abundants_count; j++) {
int k = abundants[i] + abundants[j];
if (k >= upper_limit) {
break;
}
expressed[k-1] = 1;
}
}
for (i = 1; i < upper_limit; i++) {
if (!expressed[i-1]) {
sum += i;
}
}
printf("%d\n", sum);
free(abundants);
free(expressed);
return 0;
}
int is_abundant(int n)
{
int sum = 1;
int i, k = n;
for (i = 2; i <= k; i++) {
int p = 1;
while (k % i == 0) {
p *= i;
k /= i;
}
sum *= (p*i - 1)/(i-1);
}
return 2*n < sum;
}