 One of the things we'll want to be able to do is to be able to find the greatest common divisor of two numbers. And that's what the Euclidean algorithm is for. So, here's how this works. Suppose I have two numbers, integers m and n. The greatest common divisor is, of course, the largest whole number that divides both of them. And the hardest way of finding this is probably the way you were taught in school, which is to factor both numbers and then construct the greatest common divisor from the two factors. Now, this might seem to be an easy task, but the problem is that it's only easy because the problems that you were given using this method are all the easy problems. In general, if I have two numbers randomly chosen, I will have a very, very, very, very, very hard time factoring them. This is actually one of the bases for cryptography. So, how can we find the greatest common divisor? Well, we could use something called the Euclidean algorithm. And that works as follows. In our warning, this is actually harder to describe than it is to implement. What we're going to do is we're going to divide the larger number by the smaller number, and that's going to give us some quotient and remainder. And then we're going to let the previous divisor become the new dividend, and the previous remainder is going to become the new divisor. And we'll repeat the process. We'll divide by the divisor, and we'll get a new quotient and a new remainder, and we'll continue. And each time what's going to happen is our previous divisor is going to become the new dividend, and the previous remainder is going to become the new divisor. And we will repeat this process, and the last non-zero remainder is going to be the greatest common divisor of the two original numbers. For example, let's take the numbers 1,739 and 3,589. And you might want to play around with the factor and figure out greatest common divisor method. These are two numbers that do factor, but the factorization is non-trivial. It'll take you a bit of time to find it. But let's go ahead and apply our Euclidean algorithm. So we'll take the larger number as our first dividend and the smaller number as our first divisor. So I'm going to divide larger by smaller, and I get the quotient and the remainder. Now here's the useful thing to note about the Euclidean algorithm. You don't actually need that quotient. That quotient serves no useful purpose in the Euclidean algorithm. But I can continue at this point. The old divisor, 1739, becomes the new dividend. The old remainder becomes the new divisor. And so now I find old dividend divided by old remainder. And that's going to give me a quotient with a remainder. And once again, the old divisor, 111, is going to become my new dividend. My old remainder is going to become the new divisor. And I'll divide 111 by 74. I'll get one with remainder 37. And once more, the old dividend, 74, becomes the new dividend. The old remainder becomes the new divisor. I divide, and I get remainder zero, which tells me that I'm done because I can't divide by zero among other things. But the important thing here is that the last non-zero remainder is going to be the greatest common divisor of the original two numbers. Now it's probably not a bad idea to verify that this actually is the greatest common divisor. And remember the GCD has to divide both numbers, so let's go ahead and do that division. And so we find that 37 does in fact divide both numbers. And incidentally, this also gives us the factorization of the two numbers as a product of 37, which is prime, and some other number, which in this case also happens to be prime. So if you had tried the factor and collect GCD method, you'd have to try out all the primes up to 37 before you get your initial factorization. Now although the Euclidean algorithm is actually an algorithm, it's worth thinking about a heuristic approach. Remember that the greatest common divisor of the two numbers will divide every remainder produced by the Euclidean algorithm. So it's possible because we are human beings that we could spot the greatest common divisor before the algorithm terminates. And if we do that, we might as well and save ourselves the extra effort. For example, let's find the greatest common divisor of 1,513 and 731. But again, if you want to, you can try your hand at factoring these two numbers. They're not easy to factor. But I can start out the Euclidean algorithm dividing the larger number by the smaller, and I get a remainder of 51. Now if I was a computer incapable of thinking, I could just continue the Euclidean algorithm. Old divisor, old remainder, and my next step will be 731 divided by 51. Find new quotient, which we'll ignore, and new remainder, which will be our next step. However, because we are not computers, we note the following. Whatever the greatest common divisor is, has to also divide this remainder 51. And what makes this useful is that 51 doesn't have too many factors. It actually factors as the product of primes 3 and 17, and this gives us a listing of candidates for the greatest common divisor, namely 1, which is always a possibility, 3, 17, and any products of the primes that we have, which in this case is just going to be 51. And at this point, we can do a direct division and find that 17 actually works as a divisor of both, and so because no larger number, 51 in this case, divides them, then we know that 17 is going to be the greatest common divisor. Again, if I was a computer incapable of thinking, I could just continue the Euclidean algorithm. Again, old divisor becomes the new dividend, old remainder becomes the new divisor, and my next step is going to be finding that quotient. And again, my next step is going to be taking the old divisor as the new dividend, the old remainder as my new divisor, and finding the quotient. 51 divided by 17 is 3, and remainder 0 tells me I'm done with the problem, and the last non-zero remainder is going to be my greatest common divisor. So I can either get the answer in one step with a little bit of thought, or I can get the answer in several steps with a little bit less thought but more work.