Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit 52fec4f

Browse files
authored
Added code for Extended Euclid's GCd (#319)
1 parent faa49d5 commit 52fec4f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*Extended Euclid's GCD
2+
3+
An algorithm to compute integers x and y such that
4+
ax + by = gcd(a,b) for given a and b.
5+
*/
6+
7+
#include <iostream>
8+
using namespace std;
9+
int gcd(int a, int b, int &x, int &y)
10+
{
11+
if (a == 0)
12+
{
13+
x = 0;
14+
y = 1;
15+
return b;
16+
}
17+
int x1, y1;
18+
int d = gcd(b % a, a, x1, y1);
19+
x = y1 - (b / a) * x1;
20+
y = x1;
21+
return d;
22+
}
23+
24+
int main()
25+
{
26+
int x = 0, y = 0, a, b;
27+
cout << "Enter two numbers: ";
28+
cin >> a >> b;
29+
cout << "GCD of " << a << " and " << b << ": ";
30+
cout << gcd(a, b, x, y);
31+
return 0;
32+
}

0 commit comments

Comments
 (0)