 What's up guys today I'm going to go over the extended Euclidean algorithm. So what is the extended Euclidean algorithm. So remember back when the other video when I explained what the Euclidean algorithm does. Euclidean algorithm gets you the greatest common denominator GCD of A and B right given two numbers A and B it gets you the GCD. Now you could actually represent this GCD as a linear combination of two other numbers of X and Y the extended Euclidean algorithm is going to be kind of like the Euclidean algorithm except that instead now we are trying to find the coefficients X and Y such that if we multiply A by X and add it with B times Y we are going to get the GCD of A and B. So basically we're trying to find the coefficients that if you multiply them and add them together you'll get the GCD. So as an example the GCD of 55 and 80 is equal to 5. So in terms of 55 and 80 5 is actually equal to 55 times 3 plus 80 times negative 2. So basically we're trying to find the 3 and the negative 2 in this case. So guys if you remember the Euclidean algorithm ends with B equal to 0 and A is equal to the GCD of A and B. So let's call the GCD of A and B a variable called G. So then we could actually work backwards. Basically we could say that G times 1 plus 0 times 0 is equal to G. In this case A is going to equal to G. X is going to equal to 1. Y is going to equal to 0. B is going to equal to 0 because that's the end of the algorithm. And remember that G is the GCD of A and B. So now all we have to worry about is we have to go back up and keep track how X and Y changes whenever we transition from AB to BA mod B just like the Euclidean algorithm. So let's assume that we already found the new coefficients X1 and Y1 when we transition AB to BA mod B. And let's just substitute that into the equation. So substituting the values into the equation we get B times X1 plus A mod B times Y1 is going to equal to G. We're basically trying to get this equation back to the original equation of X and Y of A times X plus B times Y is equal to G. Since we know that the definition of modulus of A mod B is equal to A minus A divided by B floor times B, we are going to use this to substitute back into the equation of A mod B. So in the original equation the GCD is equal to B times X1 plus A mod B times Y1. Now after substituting the definition of A mod B into A mod B, this is going to equal to B times X1 plus parentheses A minus A divided by B floor times by B times by Y1. Okay so now if we were to foil and rearrange the terms, we get G is going to equal to A times Y1 plus B times parentheses X1 minus Y1 times A divided by B floor. So now let's compare this equation with AX plus BY. So if we were to compare the new equation of this new equation with our old equation, we would see that the X, this X is actually going to equal to Y1 and this Y is going to equal to this X1 minus Y times A over B floor. So this X is going to correspond to this Y and this Y actually corresponds to this. If you look at the equations, because of the coefficients A and A are the same and B and B are the same. Knowing this, we get this equation. X is going to equal to Y1 and Y is going to equal to X1 minus Y1 times floor of A divided by B. Here is the implementation code. Basically, when B is equal to 0, X is equal to 1 and Y is equal to 0 like we said before. And at this point, A is going to equal to the GCD so we just return A. Otherwise, we are going to create the two new variables X1 and Y1. Then what we're going to do is we are going to get the GCD of the values again, recursively calling the function, and pass in X1 and Y1 by reference as X and Y. So what we're going to get here is we are going to get entity is going to equal to GCD of now A transition to B. So now GCD of B and now B transition to A mod by B. So we have B as A mod by B recursively calling in this function. Now we pass in X1 and Y1 by reference so that this gets changed during the recursive call when we return out. So now once we have the GCD of these new values of B and A mod by B, we are going to set recursively change the values of X that was passed in is going to equal to Y1, which is the values that we returned earlier. And Y is going to equal to X1 minus Y1 times floor of A divided by B. And after that we just return D, which is the GCD. So at the end of each recursive call X and Y gets modified and X1 and Y1 are the new values returned when we have B and A mod by B. And that basically gets our X and Y function coefficients for the Euclidean algorithm. I hope you guys enjoy this video. Raycom subscribe. I'll check you guys later. Peace.