-
Notifications
You must be signed in to change notification settings - Fork 6
/
egcd3.c
83 lines (68 loc) · 1.3 KB
/
egcd3.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
#include <stdio.h>
#include <stdlib.h>
void vassume(int b) {}
void vtraces1(int a, int b, int y, int r, int x, int p, int q, int s) {}
void vtraces2(int a, int b, int y, int r, int x, int p, int q, int s, int k, int c) {}
void vtraces3(int a, int b, int y, int r, int x, int p, int q, int s, int d, int v, int k, int c) {}
void vtraces4(int a, int b, int y, int r, int x, int p, int q, int s) {}
int mainQ(int x, int y)
{
vassume(x >= 1);
vassume(y >= 1);
int a, b, p, q, r, s;
a = x;
b = y;
p = 1;
q = 0;
r = 0;
s = 1;
//assert(a==y*r+x*p);
//assert(b==x*q+y*s);
while (1)
{
vtraces1(a, b, y, r, x, p, q, s);
if (!(b != 0))
break;
int c, k;
c = a;
k = 0;
while (1)
{
vtraces2(a, b, y, r, x, p, q, s, k, c);
if (!(c >= b))
break;
int d, v;
d = 1;
v = b;
while (1)
{
// assert(a == y*r+x*p);
// assert(b == x*q+y*s);
// assert(a == k*b+c);
// assert(v == b*d);
vtraces3(a, b, y, r, x, p, q, s, d, v, k, c);
if (!(c >= 2 * v))
break;
d = 2 * d;
v = 2 * v;
}
c = c - v;
k = k + d;
}
a = b;
b = c;
int temp;
temp = p;
p = q;
q = temp - q * k;
temp = r;
r = s;
s = temp - s * k;
}
vtraces4(a, b, y, r, x, p, q, s);
return a;
}
void main(int argc, char **argv)
{
mainQ(atoi(argv[1]), atoi(argv[2]));
}