-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path029.c
70 lines (56 loc) · 1.39 KB
/
029.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
// How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?
// NOT A VERY GOOD IMPLEMENTATION
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int main() {
bool existsInArr(int n, int *arr);
void resetArr(int *arr);
int arr[9801] = {0};
int root_arr[9801] = {0};
int next_arr_i = 0;
int next_root_arr_i = 0;
int this_root;
int this_prod;
int dupe_count = 0;
const int min = 2;
const int max = 100;
const int combos = pow((max - min) + 1, 2);
for (int x = min; x <= max; x++) {
for (int exp1 = 1; (this_root = pow(x, exp1)) <= max; exp1++) {
if (existsInArr(this_root, root_arr)) {
break;
} else {
root_arr[next_root_arr_i++] = this_root;
}
for (int exp2 = min; exp2 <= max; exp2++) {
this_prod = exp1 * exp2;
if (existsInArr(this_prod, arr)) {
dupe_count++;
printf("%i ^ %i ^ %i\n", x, exp1, exp2); // TEST
} else {
arr[next_arr_i++] = this_prod;
}
}
}
resetArr(arr);
next_arr_i = 0;
}
printf("%i - %i = %i\n", combos, dupe_count, combos - dupe_count);
return 0;
}
bool existsInArr(int n, int *arr) {
while (*arr != 0) {
if (*arr == n) {
return true;
}
arr++;
}
return false;
}
void resetArr(int *arr) {
while (*arr != 0) {
*arr = 0;
arr++;
}
}