Extended Euclidean algorithm (#25148)

* Extended Euclidean algorithm

* fix: made header smaller
pull/30093/head^2
hariom Choudhary 2019-02-14 03:04:52 +05:30 committed by Randell Dawson
parent 0dc0edcded
commit f4e264e49d
1 changed files with 38 additions and 0 deletions

View File

@ -72,3 +72,41 @@ Calculate the GCD of the first two numbers, then find GCD of the result and the
Example- `GCD(203,91,77) == GCD(GCD(203,91),77) == GCD(7, 77) == 7`
You can find GCD of `n` numbers in the same way.
### Extended Euclidean algorithm
This is an extension of Euclidean algorithm. It also calculates the coefficients x, y such that
ax+by = gcd(a,b)
x and y are also known as coefficients of Bézout's identity.
c code for Extended Euclidean algorithm
```c
struct Triplet{
int gcd;
int x;
int y;
};
Triplet gcdExtendedEuclid(int a,int b){
//Base Case
if(b==0){
Triplet myAns;
myAns.gcd = a;
myAns.x = 1;
myAns.y = 0;
return myAns;
}
Triplet smallAns = gcdExtendedEuclid(b,a%b);
//Extended euclid says
Triplet myAns;
myAns.gcd = smallAns.gcd;
myAns.x = smallAns.y;
myAns.y = (smallAns.x - ((a/b)*(smallAns.y)));
return myAns;
}
```