What is Extended Euclidean Algorithm
Hey, Guys in this part we are going to know about what is Extended Euclidean Algorithm and how we can apply this concept in our programs.
In our last blog we know about what is Euclidean Algorithm , now in this part we exlore more about extended Euclidean Algorithm.The extended Euclidean algorithm is an algorithm to compute integers and such that
a.x + b.y = gcd(a,b) ,for given and .
The existence of such integers is guaranteed by Bézout's lemma .
Explanation
(Recursive version)
Let , gcd=gcd(a,b) & x,y be the co-efficient of a,b in x.a + y.b = gcd equation.----(i)
If we recall the Eculidean algorithm, we can see that the algorithm ends with b and a=gcd in such condition x should be 1 and y be 0 so, 1.a+ 0.0 = gcd. So starting coefficient of (x,y)=(1,0).
Now we have to calculate futher coefficient for (b,a%b) , which also satisfy equation(i) , x1.b + y1.(a%b) = gcd---------(ii)
we can write (a%b) = remainder = a - quotient.b ,
put the value of (a%b) in equation (ii)
we get, x1.b + y1.(a - quotient.b)=gcd
y1.a + (x1 - quotient).b = gcd which also equal to gcd = a.x + b.y
By comparing coefficient we get the value of x = y1 & y = (x1 - quotient).
Let see code in c++,
Iterative version
This approach is similar to recursive one but only diffrence, it avoids recursion, the code will run a little bit faster than the recursive one.
Here we use one property only that : xc=x-quotient*x1 & yc=x-quotient*y1
Practice problem
May these Information will be useful for you,
Have a Nice Day😀😀😀.
Comments
Post a Comment