-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathextended-euclidean-algorithm.c
71 lines (58 loc) · 1.38 KB
/
extended-euclidean-algorithm.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
/**
* extended-euclidean-algorithm.c
*
* @author Wang Guibao (https://github.com/wangguibao/)
* @brief Calculates Bezout's coefficents, along with greatest common divisor,
* of two positive integers, using extended Euclidean Algorithm.
* https://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity
* https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
*
* Note this algorithm relates to Chinese Remainder Theorem
*/
#include <stdio.h>
int s0 = 1;
int s1 = 0;
int t0 = 0;
int t1 = 1;
int extended_euclidean(int a, int b)
{
int r = 0;
int q = 0;
int tmp = 0;
if (a == 0 || b == 0) {
return 0;
}
if (a < b) {
return extended_euclidean(b, a);
}
while (b != 0) {
r = a % b;
q = a / b;
a = b;
b = r;
tmp = s0 - q * s1;
s0 = s1;
s1 = tmp;
tmp = t0 - q * t1;
t0 = t1;
t1 = tmp;
}
return a;
}
int main()
{
int a = 0;
int b = 0;
fprintf(stdout, "Two integers: ");
scanf("%d %d", &a, &b);
if (a < 0 || b < 0) {
fprintf(stderr, "Negative numbers not acceptable\n");
return -1;
}
extended_euclidean(a, b);
fprintf(stdout, "Greatest common divisor is: %u, with s=%d t=%d",
extended_euclidean(a, b),
a >= b ? s1 : t1,
a >= b ? t1 : s1);
return 0;
}