 Hello and welcome back, in the previous segment we discussed the FOR statement. In this next segment we are going to discuss a somewhat elaborate algorithm including we will derive the algorithm and it will be coded using the vile statement but it could not, it will not be possible to code it using the repeat. Of course it can be coded using the FOR statement because in some sense the FOR statement is as powerful as the vile statement. What I mean by that is whatever you can do using FOR you can do using vile and vice versa. So here we are going to use the vile because in this particular programming problem we will not have anything like a single control variable and therefore it will be much natural to use the vile statement. So we are going to derive the algorithm and argue its correctness because the correctness is not going to be too obvious. So what is the problem? So we need to find the greatest common divisor of two numbers and we are going to see or we are going to derive Euclid's algorithm. So what is the greatest common divisor of positive integers m and n? It is the largest positive integer p that divides both m and n. So for example the greatest common divisor of 36 and 24 is 12 because 12 divides both 36 and 24 and there is no larger number which divides both of them. There is a standard method for doing this which you probably learnt in primary school which is to factorize m and n and then multiply out the common factors. Euclid's algorithm is a very old algorithm and is different and actually it runs much faster and in fact the program based on Euclid's method will also run faster than programs based on factory. So Euclid's algorithm might have been developed for manual computation but it is a very commonly used method on computers. So what is Euclid's algorithm? So there is a basic observation which is actually fairly simple. So the basic observation says that if D divides both m and n then D divides m minus n also and here we are assuming that m is bigger than n. So why is this? Well the proof is just a single line. So because D divides m, m must be a times t for some a. D divides n so n must be b times t for some b. So m minus b, m minus n must be a minus b times t but that just shows that m minus n has d as a factor. Done. Also true is sort of a converse. It says that if D divides m minus n and n then D divides m as well. So why is that true? Again the argument is very similar. So D divides m minus n so m minus n must be c times t. Then D divides n so n must be some e times t and so m which is m minus n plus n must be c plus e times t. So we have written m as a multiple of c plus e times t so D is a factor of m as well. So together what does it mean? It means that so whatever divisors m and n have, m minus n and n also have. So if D is a common divisor of m, n it is a common divisor of m minus n, n as well. So it means that the largest divisor of m, n is also the largest divisor of m minus n, n because if m minus n had an even larger divisor that would have to be a divisor of m, n so that could not be larger than the largest divisor of m, n. So which means and this is the big insight is that instead of finding gcd of m, n we might as well find gcd of n and m minus n. Now this is useful in the following sense. If you look at the numbers m, n and n, m minus n and we are assuming m is bigger than n then we know that these two numbers are smaller than m, n at least one of the numbers is smaller. So that means if you are going to do this by hand we have gotten our numbers to be smaller and of course that helps when doing things by hand but interestingly it helps also while doing things on a computer and we will see the reasons for it. So let us take an example. So suppose I want the gcd of 3977 and 943. So if we apply the previous idea then we know that this must be the gcd of 3977 minus 943 or m minus n and n. But I can apply the idea and so if I do the subtraction I get gcd of 3034 and 943 but I can apply this idea one more time who is stopping me. So again I subtract 943 from that so I get 2091 but I can apply one more time and I supply 943 one more time so I get 1148 and 943. If I apply one more time I am going to get 1148 minus 943 gcd of 205943. Now if you think about this you will realize that 205 is just 3977 mod 943. So we did not have to go through subtracting 943 one at a time because I just subtracted as many times in one shot and just taken the remainder. So that is exactly what we should do instead of going through this several steps. So what should we do? We should write gcd of 3977, 943 is equal to gcd of 3977 mod 943 and 943 because that is what this process is going to get us to. But does that mean that gcd of m and n is gcd of m mod n and n? Well we have to be a little careful because if m mod n is 0 and since we have defined gcd only for positive integers then we cannot write this. But we could say that look let us define gcd for 0 as well or we could do something less dramatic and we could say maybe like what presumably Euclid did. So we will write a statement which we could call Euclid's theorem which is that let m and n be positive integers they are greater than 0. If n divides m then gcd of m and n is m. So if n divides m then clearly the largest number dividing both m and n must be m. But if n does not divide m then gcd of m and n must be equal to gcd of m mod n n. So yes so what we wrote at the top of the slide is almost true except when n divides m. And I am not going to prove this you can prove it I mean we almost have proved it but if you really want you can prove it again by writing m as some k times d and so on. So now we can go and do that problem again. So gcd of 3977943 is equal to 3977943 mod 943. So we have got gcd of 205 and 943. But now we can use the same idea again so gcd of 205 and 943 mod 205. So that is 205, 123 but again the same idea 205 mod 123, 123. So that is 82, 123, 82, 123 is 82 and 123 mod 82 and that is gcd of 82, 41. But now if we take 82 mod 41 we see that 82 is a multiple 41 divides 82. So that means 41 must be the gcd because 41 divides 82. So 41 must be the gcd of this but the gcd of this is the gcd of this, this, this all the way till this. So we have found the gcd of 3977943 and that happens to be 41. Now let me point it out as an aside that we have done some number of divisions over here but it will turn out that the number of divisions we do in this method is much smaller than the number of divisions you would have done if you actually did the factory. Forget the multiplications even. So that we are not going to prove but you can take examples and you can persuade yourself. So we have a manual algorithm now. Are we ready to write a program? Well before that I would like you to make sure that you understand this and I would like you to write, to find Euclid's gcd of say 26 and 42. So what is the algorithm? So input our values capital M and capital N which are stored in variables M and N, little M and little N. And in each iteration we will either discover the gcd of M and N, capital M capital N. So that will happen if we have that division and we have the larger number is perfectly divided by the smaller number or we will find smaller numbers whose gcd is the same as the gcd of M and N. So that is basically the idea of the iteration. So what are the details of the iteration? At the beginning we have the numbers stored in little M little N whose gcd is the same as the gcd of M and N. Well at the beginning we will store capital M capital N and M and N. So this statement is vacuously true. But in each iteration we will try to maintain the statement. So if N divides M well we have particular N to be the gcd. So that is by Euclid's theorem. But if it is not then we know that gcd of M and N or the gcd of little M and little N which is equal to the gcd of M and N must be equal to the gcd of N and M mod N. So as a result we have changed the numbers N and M mod N, we have changed the numbers which we have in our hand. But we know that their gcd is still the same as the gcd that we wanted. So what we can do is we can now store N and M mod N in M and N. So here we are effectively assuming that the larger number is always in M and the smaller number is in N. So that is what the algorithm is. So what is the program? So the main program is int M and N and we read values of M and N. So the values read are capital M and capital N. Now so we will check whether M mod N is equal to 0. So if it is not equal to 0 then we have something to do. So what is it that we have to do? We will calculate the next value of M which is going to be N, the next value of N which is going to be M mod N and then we simply set M and N to be the next values. And if this iteration finishes that is if at some point we discover M mod N to be equal to 0 then we know that N must be the divisor. So we print out N. So now there are some subtle points over here which I should note. So if I want to store N and M mod N into M and N I cannot just write M equal to N, N equal to M mod N. Why? Because if I write M equal to N then when I try to do this M mod N, this M would already have the value N. So then I would be doing N mod N so that would just give me 1. So that is not good. So that is why we need to do something elaborate like this. Just to be careful we are saying what is the next value of M and of N and we are doing that. I guess I already solved that for you. So next time and next time having defined for clarity we could have just used one variable instead of both and that is what is shown over here but you do not have to really worry about these details. You can just use two variables whose counting. Now intuitively it should be clear that we have followed Euclid's, the idea from Euclid's theorem. But we already said that there can be one mistake that we could make. We could just write M and N, the new values of M and N directly into M and N that could be one mistake. Then you might also worry is M always going to be larger than N? Are we taking care of that? Do we need to take care of that? So these are things that we need to worry about because if those things are not working out then maybe our program will not run correctly. So here is a wrong program which seems to be following exactly the idea that we have discussed. So it does all of those things but in this update mistakenly we put what we intended for M into M and what we intended for M into M, small mistake right? But as a result of this the program will not run. So you are asked to figure out what is the mistake or whether there is a mistake even. And maybe you can check that by exchanging the values of M and N we get the right answer. But this exercise makes the following point that once we write the program we claim that we are following Euclid's idea. But have we while doing this have we introduced some mistakes into it? So we need some ways of checking that. And so we will look at that in the next segment. So what did we discuss in this segment? We discussed Euclid's algorithm for determining the GCDF to positive integers. And we said that the program appears easy to write but there is room to make mistakes. And in the next segment we will check for correctness of our program and make sure that we have not made any mistakes. We will stop here.