-
Notifications
You must be signed in to change notification settings - Fork 0
/
023.c
89 lines (70 loc) · 1.84 KB
/
023.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int main () {
void getAbundantNums(int a, const int b, bool arr[]);
bool isSumOfTwoAbNums(const unsigned int n, bool arr[]);
unsigned int end = 28123;
bool ab_num_arr[28123];
long int sum = 0;
int count = 0;
getAbundantNums(12, end, ab_num_arr);
for (int i = 0; i < end; i++) {
if (ab_num_arr[i] == true) {
}
}
for (unsigned int i = 1; i < end; i++) {
if (! isSumOfTwoAbNums(i, ab_num_arr)) {
sum += i;
count++;
}
}
printf("Count: %i\n", count);
printf("Sum: %li\n", sum);
return 0;
}
// Returns the sum of proper divisors of n.
unsigned int sumOfDivisors(const int n) {
unsigned int sum = 1; // always divisible by at least 1
int sqrt, i;
if (n <= 3) {
return 1;
}
sqrt = sqrtf(n);
if (sqrtf(n) == sqrt /* int */) {
sum += sqrt;
i = sqrt - 1;
} else {
i = sqrt;
}
// Test all possible divisors, counting down from sqrt(n) to 2.
// If evenly divisible, add divisor pair to sum.
while (i > 1) {
if (n % i == 0) {
sum += i;
sum += n / i;
}
i--;
}
return sum;
}
// Populates an array from index a to b whether that index is an abundant num.
void getAbundantNums(int a, const int b, bool arr[]) {
unsigned int sumOfDivisors(const int n);
while (a <= b) {
arr[a] = (sumOfDivisors(a) > a) ? true : false;
a++;
}
}
// Returns whether n can be written as the sum of two abundant nums,
// given an boolean array of abundant nums
bool isSumOfTwoAbNums(const unsigned int n, bool arr[]) {
int limit = n / 2;
for (unsigned int i = 12; i <= limit; i++) {
if (arr[i] == true && arr[n - i] == true) {
return true;
}
}
return false;
}