-
Notifications
You must be signed in to change notification settings - Fork 0
/
021.c
58 lines (45 loc) · 1.25 KB
/
021.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
// Evaluate the sum of all amicable pairs under 10000.
#include <stdio.h>
#include <math.h>
int main () {
int sumOfDivisors(int n, int sum_table[]);
int sum_table[10001] = {0};
int amicable1, amicable2;
int sum_of_amicables = 0;
for (int i = 3; i <= 10000; i++) {
amicable1 = sumOfDivisors(i, sum_table);
amicable2 = (amicable1 <= 10000) ? sumOfDivisors(amicable1, sum_table) : 0;
if (i == amicable2 && amicable1 != amicable2) {
sum_of_amicables += amicable1;
printf("Pair: %i, %i\n", amicable1, amicable2);
}
}
printf("Sum of amicable numbers: %i\n", sum_of_amicables);
return 0;
}
// Returns the sum of proper divisors of n.
// Saves and reads already-calculated values from sum_table
int sumOfDivisors(int n, int sum_table[]) {
int sum = 1; // always divisible by at least 1
int sqrt;
if (n <= 3) {
return 1;
}
if (sum_table[n]) {
return sum_table[n];
}
sqrt = sqrtf(n);
if (n % sqrt == 0) {
sum += sqrt;
}
// Test all possible divisors, counting down from sqrt(n) to 2.
// If evenly divisible, add divisor pair to sum.
for (int i = sqrt; i > 1; i--) {
if (n % i == 0) {
sum += i;
sum += n / i;
}
}
sum_table[n] = sum;
return sum;
}