forked from ycshu/110_Fast_Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_gcd.c
More file actions
75 lines (72 loc) · 1.32 KB
/
4_gcd.c
File metadata and controls
75 lines (72 loc) · 1.32 KB
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
#include <stdio.h>
#include <time.h> // for random seed
unsigned int GCD(unsigned int a, unsigned int b);
int main() {
unsigned int d, k, m, n, a, b, r, t, s, N=1 << 25;
time_t t1, t2;
srand( time(NULL) );
m = (rand()/2)*(rand()/2)+1;
n = (rand()/2)*(rand()/2)+1;
printf("%d, %d\n",m,n);
if (n>m) {
t = m;
m = n;
n = t;
}
// Method 1
t1 = clock();
for (k=1;k<N;++k) {
a = m; b = n;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
}
t1 = clock()-t1;
printf("Method 1: %d ms\n",t1);
// Method 2
a = m; b = n;
t1 = clock();
for(k=1;k<N;++k){
d = GCD(a,b);
}
t1 = clock()-t1;
printf("Method 2: %d ms\n",t1);
// Method 3
t2 = clock();
for (k=1;k<N;++k) {
a = m; b = n; d = 1;
while (b != 0) {
// If a and b are both even, take 2 from them and sent to gcd
while((a % 2 == 0) && (b % 2 == 0)) {
d = d * 2;
a = a / 2;
b = b / 2;
}
while((a % 2 == 0) && (b % 2 == 1)) {
a = a / 2;
}
while((a % 2 == 1) && (b % 2 == 0)) {
b = b/2;
}
r = a % b;
a = b;
b = r;
}
a = a * d;
//printf("2: GCD of %d and %d is %d\n",m,n,a);
}
t2 = clock()-t2;
printf("Method 3: %d ms\n", t2);
return 100;
}
unsigned int GCD(unsigned int a, unsigned int b) {
if (b == 0) return a;
unsigned int r = a % b;
if (r == 0) {
return b;
} else {
return GCD(b,r);
}
}