-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
107 lines (93 loc) · 2.52 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int* getProperDivisors(int x, int* count){
int* divisors = (int*)malloc(x * sizeof(int));
if (divisors == NULL){
printf("Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
divisors[0] = 1;
int divisor_count = 1;
int root_of_x = (int)sqrt((double)x);
for(int i=2; i<=root_of_x; i++) {
if (x%i == 0) {
divisors[divisor_count] = i;
divisor_count++;
if (i != x / i) {
divisors[divisor_count] = x/i;
divisor_count++;
}
}
}
*count = divisor_count;
return divisors;
}
int aliquotSum(int x){
int count;
int* divisors = getProperDivisors(x, &count);
int sum = 0;
for(int i=0;i<count;i++){
sum += divisors[i];
}
free(divisors);
return sum;
}
typedef enum AliquotSumClass{
Perfect,
Deficient,
Abundent
} AliquotSumClass;
AliquotSumClass GetAliquotSumClass(int x) {
int sum = aliquotSum(x);
if (x == sum){
return Perfect;
} else if (x < sum){
return Abundent;
} else {
return Deficient;
}
}
int main(int argc, char *argv[]){
clock_t start, end;
double elapsed_time;
start = clock();
int* abundentNumbers = (int*)malloc(sizeof(int)*28123);
int abundentCount = 0;
/* int* results = (int*)malloc(sizeof(int)*28); */
/* int resultsCount = 0; */
long long int sumOfNonsummableByAbundantNumbers = 0;
for (int i=1; i<=28123; i++){
if(GetAliquotSumClass(i)==Abundent){
abundentNumbers[abundentCount] = i;
abundentCount++;
}
int break_outer_loop = 0;
for (int j=0;j<abundentCount;j++){
int difference = i - abundentNumbers[j];
for (int k=0; k<abundentCount;k++){
if(difference==abundentNumbers[k]){
break_outer_loop = 1;
break;
}
}
if (break_outer_loop==1){
break;
}
}
if (break_outer_loop==0){
sumOfNonsummableByAbundantNumbers += i;
printf("i=%d\n", i);
}
}
end = clock();
elapsed_time = ((double)end-start)/CLOCKS_PER_SEC;
printf(
"sum of nonsummable by abundant numbers = %lld (elapsed time=%f)\n",
sumOfNonsummableByAbundantNumbers,
elapsed_time
);
return EXIT_SUCCESS;
}