-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob23.c
61 lines (53 loc) · 1.01 KB
/
prob23.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
#include <stdio.h>
#include <math.h>
/* No, the program is not buggy. Just atrociously slow. Let it complete */
int
get_sum_of_divisors(int num)
{
double t = num;
int sq_root = ceil(sqrt(t));
int i, j, sum_of_divisors = 1;
for (i = sq_root; i > 1 && i != num; i--) {
if ((num % i) == 0) {
j = num / i;
if (j <= sq_root) {
/* lest we account for these numbers twice */
sum_of_divisors += i;
} else {
sum_of_divisors += (i + j);
}
}
}
return (sum_of_divisors);
}
int
main()
{
int i, j, sa, sb;
int sum = 0;
int found;
for (i = 1; i < 24; i++) {
sum += i;
}
for (; i < 28123; i++) {
found = 0;
for (j = 1; j <= i/2; j++) {
sa = get_sum_of_divisors(j);
sb = get_sum_of_divisors(i-j);
if ((sa > j) && (sb > (i-j))) {
/*
* Found a number which can be expressed as a
* sum of two abundant numbers;
*/
found = 1;
//printf("%d: %d + %d\n", i, j, (i-j));
break;
}
}
if (!found) {
sum += i;
}
}
printf("%d\n", sum);
return (0);
}