From f4e264e49ddcb05c4a89d0a4e28db6c0712e6ece Mon Sep 17 00:00:00 2001 From: hariom Choudhary Date: Thu, 14 Feb 2019 03:04:52 +0530 Subject: [PATCH] Extended Euclidean algorithm (#25148) * Extended Euclidean algorithm * fix: made header smaller --- .../index.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/guide/english/algorithms/greatest-common-divisor-euclidean/index.md b/guide/english/algorithms/greatest-common-divisor-euclidean/index.md index 3ace3db34d..a81f133a99 100644 --- a/guide/english/algorithms/greatest-common-divisor-euclidean/index.md +++ b/guide/english/algorithms/greatest-common-divisor-euclidean/index.md @@ -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; +} +``` + +