 What's up guys today? We are going to go over the Euclidean algorithm, so sometimes you need to find the greatest common divisor of a tune of two numbers And what is the greatest common divisor? It's the GCD. All right What is the GCD? It's basically the largest not possible number that divides both and B, right? And when I say and B, it's just like both of your two numbers. So this is your a this is your B, right? So the GCD of these two numbers a and b is just the largest possible number that divides both of them, right? So there you could divide both of them and it's be fine, okay? You could find the mathematical like this, but I don't think it really matters in this case. Basically, it's just the maximum of Largest possible number from 1 to infinity, okay, right and chaos divide both a and chaos divide B So this is the divide symbol, right a and this is the end. This is an and symbol. So this just means that Largest possible number from 1 to infinity that divides both a and it divides B So that's what the mathematical notation. This means So if a and b are zero, so like if this this number is zero and this is number zero, right? The GCD of zero and zero is undefined okay, because Yeah, you just zero zero. There's really no no other way. So that's why it's undefined if one of the values It's zero then it's the other number. So like if a is zero then the GCD of Zero and B. It's it would just be B, right? If B is zero then the GCD of a and zero B being zero is going to be a right. So that's what this that's what this bottom part means But yeah, that's basically the what GCD means and now we're gonna talk about the Euclidean Algorithm, okay, so the Euclidean algorithm is basically an algorithm that allows you to find the greatest common divisor of a and B in log of minimum of a B time. So basically that what it means is that if there's like It would just be the It would find the time The time it would take to compute the greatest common divisor of a and B is going to be the smallest number of a and B The small number of a and B the smaller number of a and B and it'll be the logarithm base 2 of that So if a and B was like, I don't know if a was 2 and B was 5 it would be the log base 2 of 2 Because 2 is a smaller number of 5, right? GCD of 2 and 5, right? So it'll be it'll take log base 2 of 2 time Okay, we technically don't need to knew that know that for competitive programming. That's just a fun fact to know Okay, so here's the gist of the Euclidean algorithm. So basically GCD of a and B What it's gonna do is that? If B is equal to 0 so if this B value is equal to 0 We know we just end the Euclidean algorithm because the answer would just be a right because if one of them is 0 That means the GCD is just gonna be the other number, right? The greatest common divisor of a number and 0 is just going to be that other number, right? Because that other number divides by 0 yet, right? So Otherwise if it's not what we're gonna do is we are going to call GCD again But we're instead of calling Just a and B what we're gonna do is we are going to Call it with the GCD of B and then we find the remainder of a divided by B, right? And we call that with that so like it's basically like I'm gonna keep calling the same function over and over Until with the remainder of what you just divided a by B by so and then you pass that in into the function And keep calling over and over again, and I'll basically get you the greatest common divisor For a and B because you get the remainder and you keep the you keep dividing it by itself And then you compare those two so that's what that's basically the Euclidean algorithm So let's actually show you the code for it right now and then later on we'll show you the correctness Okay, guys, so to do the Euclidean algorithm, what you're gonna do is Let's write the function integer GCD integer a integer B These are the two parameters that you want to find so what you're gonna do is you're gonna first check if B is equal to 0 Then you just return a because that's just the Euclidean algorithm otherwise you return the GCD of B and then you're gonna take the remainder of A divided by B, so that's a mod B and that's basically the whole Euclidean algorithm recursively so that's basically the gist of the code if you want to type it up you guys could type it up and Check it out yourself. It actually does work But this is this is pretty much the whole gist of the code and you'll be able to Solve the problems yourself if you want to use this Euclidean algorithm so in C++ you could actually make this into a one-liner and Where we're gonna do that now, so if you want to convert the C++ code into one-liner You would just do this return Use this turn area operator if B is equal to zero then we just return a Otherwise we return GCD of B mod by a mod B and Then we could get rid of both of these if statements, so this is just one line If B is equal to Z zero Then what we're gonna do is we're return a Otherwise we return GCD of B and a mod by B. So that's the whole gist of the one line Okay, so now if you want to do this non recursively You kind of do the same thing. So what you do is you take You do while B is greater than zero and what you're gonna do is you're gonna take a and you mod it by B Right a is equal to a mod by B. This is what the this is what it's doing So it's getting the remainder of a divided by B That's what this is doing and then what you're gonna do is you're gonna actually just swap and B so then the values are now swapped and the reason why we do that is because that's going to be the same thing as Doing this instead of passing in a mod by B, right? You're gonna swap and B. So then what's gonna happen is that B takes a place of a Like in our Euclidean code here. If you look at recursively B takes a place of a your B takes place of a and A mod B is equal to B, right? So that's what you do here and you swap them and then at the end you just return a So that's the gist of the Euclidean algorithm Okay, so now what we're gonna do is we need we're gonna show the proof of correctness So why this actually works? So basically we have to show that the greatest common divisor between a and B is equal to the greatest common divisor of B mod and Mod of a mod B right when you take the remainder of a mod B when you take the remainder of a and B That's equal to the left side equals the right side We have to show this works for all the values of a greater than equal to zero and B is greater than zero So this that's what um We need to show this proof of correctness why this works. So we're gonna show this now So how do you show this that the left side is equal to the right side? So generally what you're gonna do is you're going to Assume that it works and then later on you prove the left side equals the right side So let's actually show that okay, so to show let's show what the left side actually means So what does this mean? So let's say I take the greatest common divisor of a and B right and let's say that's just a variable Let's say it's some number. Let's call it D right so D is equal to the greatest common divisor of a and B So what does this mean? This means that D divides a and D divides B Right, so it means that D is divisible by a and D is divisible by B So now that you have this what does this mean? Okay, so let's go back to what remainder means so when you take the remainder of something when you take a made by Mod by B right so whenever you take a remainder of something. This is actually equal to a minus B times by the lower value of a divided by B and We could actually show you an example why this is the case. So like if I whenever let's say I want to take the remainder of five Divided by two right so five divided by two so five the remainder five divided by two is actually equal to five minus two times by the lower value of Rounded downwards the value rounded downwards of five divided by two right, and this is actually equal to Five minus two times and what's five divided by two? Rounded down. So like if I take five by two two four one right It is two right, so it's two. So that's equal to two so this is equal to five minus four, which is equal to one and We know this is the case because remember if you take the remainder of something right? Let's say I go back to basic division Let's say I take five and I try to divide it by two in this case What I'm going to do to find the remainder of this value Right, what you want to do is you find like the value that you could Multiply by so you take two times two which gets you four and then if you take five minus four you get one And that's your remainder. So that's basically what you're doing Whenever you take a remainder right you take five you take the value dividing it by Right a if you take a mod by B. Let's say a mod by B You want to take find the remainder a mod by B you take a you subtract by B And you multiply it by the value of a divided by B rounded down So that's the same thing as whenever you take a remainder and we saw that here in this case, right? Because here five by by two two times two is four. So that's the lower value of Rounded downwards whenever you take five divided by two which gets you four and then you do five minus four equal one So that's how you would get remainder. So that's this is like the definition of remainder a mod by B is equal to a minus B times a Rounded downwards of a divided by B. This floor function means round downwards by the way So this means round down Right this floor function it means rounded downwards. Okay, so this is the definition of a mod by B So using this definition, what could we say about the values of D? So knowing that Let's backtrack back now We know that D is equal to the greatest common divisor of a and B, right? D is equal to that so that means that D is divisible by a and D is divisible by B, right? And because we know that when you take the remainder of a mod by B is equal to a subtracted by B times by the lower value Rounded bound lower bound of a divided by B We know that this value of a mod by B That D must be divisible by a mod by B At this case because we know is because of based on this value how divisibility works from here We a is just subtracted by this value time by this so we know that a must of D must be divisible by a mod by B So because we know that D is the greatest common divisor is divisible by a mod by B So it basically shows that D is divisible by B and D is divisible by a mod by B So there's a theorem in math. It's kind of like the transitive property But it actually makes sense though if you think about it So if you take to this is the theorem So if you take a number and it's divisible by another number Right Q and you take that same number and it's divisible by another number say it's R Then this number has to be divisible by the greatest common divisor of both the two numbers Right, like if you think about it logically it makes sense, right? If you have a number that's divisible by another number and this other number is divisible by another number That same number must be divisible by the greatest common divisor of both of those two numbers Right because like it when the greatest common divisor is a number that divides by both of those two numbers So this number must be divisible by this like do you guys don't understand what I mean? This is like a theorem so using this theorem you could say that Because D is the divisible by B and D is divisible by a mod by B this means that D is divisible by the GCD of B and a mod by B Okay, so that's what this means. That's what this means Okay, so so if you go back to the proof basically So this means that D is divisible by GCD of B and B a mod by B so this means that If you substitute back what our D was originally it is equal to the GCD of a and B Right this GCD of a and B is divisible by the GCD of B and a mod by B So because of this is the case then yeah because of this is equal to that So that means D is equal to the GCD of a which is odd by B with divisible by GCD of B a mod by so there's also another thing that you could know about which is the least common multiple least common multiple is just if you take the Multiplication of the two numbers and you divide it by the greatest common divisor of the two numbers You're gonna get the least common multiple right and that's a fun fact. You know like and it makes sense right if you take Two numbers multiply them together and you divide it by the greatest common number that is divisible by both of the two numbers You're gonna get the least common multiple Right, that's just how it works. It's like the definition of the least common multiple, right? But yeah, we could also even code this right now. Actually, we could code up the least common multiple So if I do like LCM of a and B There's just gonna be return a times B divided by GCD of a and B And yeah, that's pretty much it. Hope you guys enjoy this video raycom subscribe. I'll check you guys later. Peace