Skip to content

Commit 98371f4

Browse files
authored
Added Extended Euclidean Algorithm.
1 parent e1d8b59 commit 98371f4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
int d, x, y;
2+
3+
void Extended_Euclid(int A, int B) // a given equation of form Ax+By, extended euclid finds the gcd of A and B as well as the co-efficients of A and B
4+
{
5+
if(B == 0)
6+
{
7+
d = A;
8+
x = 1;
9+
y = 0;
10+
}
11+
else
12+
{
13+
Extended_Euclid(B, A%B);
14+
int temp = x;
15+
x = y;
16+
y = temp - (A/B)*y;
17+
}
18+
}
19+
20+
int main( )
21+
{
22+
int m,n;
23+
cin>>m>>n;
24+
extendedEuclid(m, n);
25+
cout << "The GCD of m and n is " << d << endl;
26+
cout << "Coefficient x and y are: "<< x << "and " << y << endl;
27+
return 0;
28+
}

0 commit comments

Comments
 (0)