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
|
#include <u.h>
#include <libc.h>
enum {
Tgcd,
Tmod,
Te
};
vlong t[Te];
vlong t0;
void
swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
int
gcd(int u, int v)
{
while(u > 0){
if(u < v)
swap(&u, &v);
u = u-v;
}
return v;
}
int
modgcd(int u, int v)
{
while(u > 0){
if(u < v)
swap(&u, &v);
u = u % v;
}
return v;
}
void
usage(void)
{
fprint(2, "usage: gcd num den\n");
exits("usage");
}
void
main(int argc, char *argv[])
{
int n, m;
if(argc != 3)
sysfatal("not enough arguments");
n = strtol(argv[1], nil, 0);
m = strtol(argv[2], nil, 0);
t0 = nsec();
print("GCD %d %d = %d\n", n, m, gcd(n, m));
t[Tgcd] = nsec()-t0;
t0 = nsec();
print("MODGCD %d %d = %d\n", n, m, modgcd(n, m));
t[Tmod] = nsec()-t0;
print("\tgcd: %lld | mod: %lld\n", t[Tgcd], t[Tmod]);
exits(nil);
}
|