This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcd_middle_school_method.c
140 lines (98 loc) · 2.35 KB
/
gcd_middle_school_method.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Write a program to find GCD using middle school method and analyze its time efficiency.
*/
#include <stdio.h>
#include <stdlib.h>
int iter = 0;
int *generate_sieve(int n) {
int *is_prime = malloc((n + 1) * sizeof(int));
is_prime[0] = is_prime[1] = 0;
for (int i = 2; i < n; i++) {
is_prime[i] = 1;
iter += 1;
}
for (int i = 2; i <= n / 2; i++) {
for (int j = 2; i * j <= n; j++) {
is_prime[i * j] = 0;
iter += 1;
}
}
return is_prime;
}
int middle_school_gcd(int x, int y) {
int n = x < y ? x : y;
int *is_prime = generate_sieve(n); // size (n + 1)
int gcd = 1;
for (int i = 2; i <= x && i <= y; i++) {
iter += 1;
if (!is_prime[i])
continue;
if (x % i == 0 && y % i == 0) {
x /= i;
y /= i;
gcd *= i;
i -= 1; // perhaps i will divide x and y again
}
}
return gcd;
}
int main() {
int x, y;
printf("Enter a and b: ");
scanf("%d %d", &x, &y);
iter = 0;
printf("\nmiddle_school_gcd(%d, %d) = %d\n", x, y, middle_school_gcd(x, y));
printf("n_iter = %d\n\n", iter);
}
/*
Enter a and b: 12 18
middle_school_gcd(12, 18) = 6
n_iter = 25
consecutive_integer_gcd(12, 18) = 6
n_iter = 7
Enter a and b: 45 60
middle_school_gcd(45, 60) = 15
n_iter = 141
consecutive_integer_gcd(45, 60) = 15
n_iter = 31
Enter a and b: 101 103
middle_school_gcd(101, 103) = 1
n_iter = 482
consecutive_integer_gcd(101, 103) = 1
n_iter = 100
Enter a and b: 56 72
middle_school_gcd(56, 72) = 8
n_iter = 191
consecutive_integer_gcd(56, 72) = 8
n_iter = 49
Enter a and b: 81 27
middle_school_gcd(81, 27) = 27
n_iter = 71
consecutive_integer_gcd(81, 27) = 27
n_iter = 1
Enter a and b: 35 49
middle_school_gcd(35, 49) = 7
n_iter = 101
consecutive_integer_gcd(35, 49) = 7
n_iter = 29
Enter a and b: 24 36
middle_school_gcd(24, 36) = 12
n_iter = 63
consecutive_integer_gcd(24, 36) = 12
n_iter = 13
Enter a and b: 66 99
middle_school_gcd(66, 99) = 33
n_iter = 236
consecutive_integer_gcd(66, 99) = 33
n_iter = 34
Enter a and b: 100 200
middle_school_gcd(100, 200) = 100
n_iter = 388
consecutive_integer_gcd(100, 200) = 100
n_iter = 1
Enter a and b: 88 176
middle_school_gcd(88, 176) = 88
n_iter = 335
consecutive_integer_gcd(88, 176) = 88
n_iter = 1
*/