1 00:00:01,200 --> 00:00:08,809 In this session, we will write another algorithm to solve a mathematical problem. 2 00:00:08,809 --> 00:00:10,660 If you do not know this algorithm already, that 3 00:00:10,660 --> 00:00:16,770 is fine; it is more for the purpose of demonstrating, if you know a solution, how 4 00:00:16,770 --> 00:00:22,590 do you come up with the algorithm to tell a computer how to solve it. 5 00:00:22,590 --> 00:00:28,840 The algorithm is for finding the greatest common divisor or the highest common factor, 6 00:00:28,840 --> 00:00:33,111 this is known under two names of two positive integers: m and n. 7 00:00:33,111 --> 00:00:37,050 So, this is an algorithm you probably know. 8 00:00:37,050 --> 00:00:39,790 How do you solve this? 9 00:00:39,790 --> 00:00:43,210 Let us first try a naive solution. 10 00:00:43,210 --> 00:00:50,180 And before writing an algorithm, let us see what do I mean by the simple solution of GCD. 11 00:00:50,180 --> 00:00:57,790 So, you are asked to find the greatest common divisor of m and n; take the smaller number 12 00:00:57,790 --> 00:01:08,640 n; and now you start looking for each number k between 1 and n, remember that n is the 13 00:01:08,640 --> 00:01:14,530 smaller number; in descending order, do the following. 14 00:01:14,530 --> 00:01:21,579 What you do is if k divides m and n, then k is the greatest common divisor of m and 15 00:01:21,579 --> 00:01:22,579 n. 16 00:01:22,579 --> 00:01:31,749 And this is obvious by the definition of greatest common divisor; if k divides m and n, then 17 00:01:31,749 --> 00:01:34,549 it is obviously a divisor of m and n. 18 00:01:34,549 --> 00:01:39,009 Also we are coming in descending order; we start from n and go down to n. 19 00:01:39,009 --> 00:01:44,079 So, the first divisor that you hit when you go down is going to be the greatest common 20 00:01:44,079 --> 00:01:45,740 divisor of m and n. 21 00:01:45,740 --> 00:01:50,899 So, this algorithm obviously works. 22 00:01:50,899 --> 00:01:56,770 It will compute the GCD correctly, but it is very slow. 23 00:01:56,770 --> 00:02:05,560 And think about a very large numbers: m and n; and you will see that, it may go n steps 24 00:02:05,560 --> 00:02:08,220 before reaching the correct GCDs. 25 00:02:08,220 --> 00:02:14,510 So, compute the GCD of two very large numbers, which are relatively prime to each other; 26 00:02:14,510 --> 00:02:19,780 that means that the GCD of m and n are 1. 27 00:02:19,780 --> 00:02:25,810 Now, if you pick such a pair, this algorithm will compute the GCD correctly, but it will 28 00:02:25,810 --> 00:02:31,930 take n steps, because you have to go down all the way from n to 1 before you will hit 29 00:02:31,930 --> 00:02:32,930 the GCD. 30 00:02:32,930 --> 00:02:34,630 Can we do better? 31 00:02:34,630 --> 00:02:40,680 There is a faster way and it is a very old algorithm. 32 00:02:40,680 --> 00:02:42,689 The algorithm is due to Euclid. 33 00:02:42,689 --> 00:02:46,280 We will see a slightly modified version of that algorithm. 34 00:02:46,280 --> 00:02:52,470 So, before we go into Euclid’s algorithm for GCD, we will describe what it does and 35 00:02:52,470 --> 00:02:55,530 give you a slight intuition of why it works. 36 00:02:55,530 --> 00:03:00,030 So, consider the GCD of 8 and 6. 37 00:03:00,030 --> 00:03:11,180 Now, you can consider two rods: one of length 8, and another of length 6. 38 00:03:11,180 --> 00:03:20,980 Now, obviously, if a number divides 6 and 8, then I should be able to make a stick of 39 00:03:20,980 --> 00:03:29,260 that length, so that I can measure 6 exactly with that shorter rod; and I can measure 8 40 00:03:29,260 --> 00:03:30,790 exactly with that shorter rod. 41 00:03:30,790 --> 00:03:35,930 This is the meaning of a common divisor, and we have to find the greatest common divisor. 42 00:03:35,930 --> 00:03:44,599 So, first, what we will do is we will measure the longer rod using the shorter rod. 43 00:03:44,599 --> 00:03:49,250 Now, it may not measure the longer rod exactly. 44 00:03:49,250 --> 00:03:56,659 For example, in this case, 6 does not measure 8 exactly; there will be a small piece of 45 00:03:56,659 --> 00:03:58,599 length 2 left over. 46 00:03:58,599 --> 00:04:01,459 So, take that reminder. 47 00:04:01,459 --> 00:04:06,950 And now, repeat the process; now, 2 has become the shorter rod and 6 has become the longer 48 00:04:06,950 --> 00:04:08,690 rod. 49 00:04:08,690 --> 00:04:14,730 Now, see if 2 measures 6 exactly; it does. 50 00:04:14,730 --> 00:04:17,380 So, you are done. 51 00:04:17,380 --> 00:04:26,570 And then you can say that, 2 is the GCD of 8 and 6. 52 00:04:26,570 --> 00:04:31,440 The reason why this works is – by the nature of this algorithm, it is clear that 2 divides 53 00:04:31,440 --> 00:04:34,390 6, because that is why we stop the algorithm. 54 00:04:34,390 --> 00:04:38,440 And also, we know that, 8 is basically 6 plus 2. 55 00:04:38,440 --> 00:04:41,000 So, it is obviously, a multiple of 2. 56 00:04:41,000 --> 00:04:44,260 So, it is a common divisor. 57 00:04:44,260 --> 00:04:51,220 And with a slightly more elaborate argument, we can argue that, it is the greatest common 58 00:04:51,220 --> 00:04:52,340 divisor. 59 00:04:52,340 --> 00:04:56,770 So, this is an algorithm, which is essentially due to Euclid. 60 00:04:56,770 --> 00:05:00,520 So, it was known for at least 2000 years. 61 00:05:00,520 --> 00:05:05,640 Let us pick a slightly more elaborate example. 62 00:05:05,640 --> 00:05:12,480 Let us say we want to find the GCD of 102 and 21. 63 00:05:12,480 --> 00:05:19,630 The process of taking remainder is what is known as the modulo operator in mathematics. 64 00:05:19,630 --> 00:05:27,270 So, 102 modulo 21 is the remainder of integer division of 102 by 21. 65 00:05:27,270 --> 00:05:32,460 So, the remainder of when you divide 102 by 21 is 18. 66 00:05:32,460 --> 00:05:37,280 So, that is the shorter rod for the next stage. 67 00:05:37,280 --> 00:05:42,960 Now, 21 mod 18 is 3. 68 00:05:42,960 --> 00:05:48,600 And that becomes the rod for the next stage; the shorter rod for the next stage. 69 00:05:48,600 --> 00:05:52,300 And 18 mod 3 is 0; that is when you stop the algorithm. 70 00:05:52,300 --> 00:05:57,630 So, when the modulo operator gives you 0 result; that means that, the shorter number exactly 71 00:05:57,630 --> 00:06:02,950 divides the larger number; that means that, the shorter number is a divisor of the larger 72 00:06:02,950 --> 00:06:04,900 number and you stop the algorithm. 73 00:06:04,900 --> 00:06:11,840 Now, you say that, GCD of 102 and 21 is 3. 74 00:06:11,840 --> 00:06:19,440 So, this is a slight modification of the classical Euclid’s method for GCD. 75 00:06:19,440 --> 00:06:23,890 And so, it is based on the following simple fact, which we have described. 76 00:06:23,890 --> 00:06:26,150 And you can prove this mathematically as well. 77 00:06:26,150 --> 00:06:33,640 So, suppose you take two positive numbers: a and b; where, a is the larger number; then 78 00:06:33,640 --> 00:06:44,210 GCD of a and b is the same as GCD of b and the remainder when you divide a by b. 79 00:06:44,210 --> 00:06:53,320 So, it is written by the equation GCD of a, b is GCD of b comma a modulo b. 80 00:06:53,320 --> 00:06:58,650 The modulo operator is represented as the percentage sign, because this is the convention 81 00:06:58,650 --> 00:07:06,400 that we will use in C. And this equation can be seen by our previous slide; a was the bigger 82 00:07:06,400 --> 00:07:09,420 rod; b was the shorter rod. 83 00:07:09,420 --> 00:07:10,550 This was the first stage. 84 00:07:10,550 --> 00:07:13,510 The second stage was when b is the shorter rod. 85 00:07:13,510 --> 00:07:21,800 And the shorter rod for the next stage is modulo – is given by the modulo operator. 86 00:07:21,800 --> 00:07:28,740 To prove this, you can start by considering the division of a by b and writing a as b 87 00:07:28,740 --> 00:07:29,990 times q plus r. 88 00:07:29,990 --> 00:07:35,180 But, we will not go into the proof. 89 00:07:35,180 --> 00:07:39,670 From elementary properties of natural numbers, it is possible to prove that, Euclid’s method 90 00:07:39,670 --> 00:07:41,380 correctly computes the GCD. 91 00:07:41,380 --> 00:07:51,830 Right now, we will move into how do we write the GCD algorithm in the form of an input. 92 00:07:51,830 --> 00:07:55,720 So, here is a slightly abbreviated picture. 93 00:07:55,720 --> 00:08:02,380 I have skipped the start state; but the start state is there. 94 00:08:02,380 --> 00:08:05,440 Let us focus on what happens during the algorithm. 95 00:08:05,440 --> 00:08:10,820 You have two numbers: a and b. 96 00:08:10,820 --> 00:08:16,420 The first thing to ensure is that, a is the larger number. 97 00:08:16,420 --> 00:08:21,400 The reason we do that is that, if a is the larger number, then the modulo operator is 98 00:08:21,400 --> 00:08:23,100 properly defined. 99 00:08:23,100 --> 00:08:28,390 So, if a is the larger number, then we are fine; we can go into the GCD algorithm. 100 00:08:28,390 --> 00:08:34,380 If a is not the larger number, you merely swap a and b, so that whatever is the larger 101 00:08:34,380 --> 00:08:35,380 number, you called it a. 102 00:08:35,380 --> 00:08:43,800 So, exchange a and b; means that you say that, the value of a is stored in a temporary variable; 103 00:08:43,800 --> 00:08:49,680 then the value of b is stored in a; and then the value of b is stored in t with the value 104 00:08:49,680 --> 00:08:50,680 of t stored in b. 105 00:08:50,680 --> 00:08:54,570 So, here is a way to exchange the values of a and b. 106 00:08:54,570 --> 00:09:01,120 So, ensure at first that, a is the larger number. 107 00:09:01,120 --> 00:09:07,680 Once you do that, you get into the code for the proper utility in GCD. 108 00:09:07,680 --> 00:09:10,530 First you test whether b is 0. 109 00:09:10,530 --> 00:09:17,410 If b is 0, then there is nothing to do; a is the GCD of a and b; GCD of 4 comma 0 is 110 00:09:17,410 --> 00:09:22,610 4; GCD of 8 comma 0 is 8, and so on. 111 00:09:22,610 --> 00:09:26,530 So, if the smaller number is 0, then there is nothing do in the algorithm; the algorithm 112 00:09:26,530 --> 00:09:33,410 is over; and you say that, print a. 113 00:09:33,410 --> 00:09:41,180 If b is not 0, then we do the Euclidean equation. 114 00:09:41,180 --> 00:09:53,550 You take a modulo b; store it in a variable g; then assign the value of b to a and assign 115 00:09:53,550 --> 00:09:55,190 the value of g to b. 116 00:09:55,190 --> 00:10:06,620 So, this corresponds to the operation of taking b and a modulo b as the next step. 117 00:10:06,620 --> 00:10:11,710 After you do that, you again test the condition whether b has now become 0. 118 00:10:11,710 --> 00:10:19,020 If it is 0, then we are done and a is the GCD; otherwise, we do another round of taking 119 00:10:19,020 --> 00:10:24,610 a modulo b and setting a equal to b and b equal to g. 120 00:10:24,610 --> 00:10:29,630 So, a, b and g are what are known as variables. 121 00:10:29,630 --> 00:10:35,750 And variables are used in programming to store exactly one value at a time. 122 00:10:35,750 --> 00:10:40,410 So, at any particular time, it will have one value; then after the execution of another 123 00:10:40,410 --> 00:10:44,180 instruction, it will have a new value and so on. 124 00:10:44,180 --> 00:10:52,890 Now, for the purposes of describing an algorithm, imagine that, the variable is a box; and it 125 00:10:52,890 --> 00:10:56,840 is a name of a box; and the value is stored inside the box. 126 00:10:56,840 --> 00:11:02,070 For example, a, b and g are the variables that we have used in the program. 127 00:11:02,070 --> 00:11:05,890 And they are the names for these integer boxes. 128 00:11:05,890 --> 00:11:13,240 So, if we are computing, let us say the GCD of 5 and 3, then you might start with a equal 129 00:11:13,240 --> 00:11:17,270 to 5 and b equal to 3. 130 00:11:17,270 --> 00:11:21,680 The second operation that we have used in the code is the assignment operation. 131 00:11:21,680 --> 00:11:26,460 So, this is what an example of the assignment operator. 132 00:11:26,460 --> 00:11:34,190 And when we do an assignment, what we mean is that, you take the left variable, which 133 00:11:34,190 --> 00:11:41,120 is g in this case and assign it the value of what is the expression on the right-hand 134 00:11:41,120 --> 00:11:48,510 side, which is a modulo b . So, assignment a equal to b replaces whatever is stored in 135 00:11:48,510 --> 00:11:50,790 a by what is stored in b. 136 00:11:50,790 --> 00:11:55,940 So, take the right-hand side; take the value of that; and put it into the variable that 137 00:11:55,940 --> 00:12:01,779 the left-hand side represents. 138 00:12:01,779 --> 00:12:10,839 For example, if a was 5 and b is 3; after a equal to b, you would take the value of 139 00:12:10,839 --> 00:12:12,460 b and put it in a. 140 00:12:12,460 --> 00:12:18,500 So, a will now become 3 and b will remain 3. 141 00:12:18,500 --> 00:12:22,029 Another small thing that we have used in the code is sequential assignment. 142 00:12:22,029 --> 00:12:27,930 So, if you write a bunch of statements one after the other, let us say separated by semicolons; 143 00:12:27,930 --> 00:12:33,620 then this means that, the instructions are to be executed one after the other in sequence. 144 00:12:33,620 --> 00:12:40,430 So, first, you do g equal to a modulo b; then you do a equal to b; and after that you do 145 00:12:40,430 --> 00:12:42,380 b equal to g. 146 00:12:42,380 --> 00:12:47,290 So, initially, let us say that a is 10 and b is 6; g is undefined. 147 00:12:47,290 --> 00:12:57,220 After you run the statement g equal to a modulo b, you take 10 modulo 6; you will have 4. 148 00:12:57,220 --> 00:13:02,930 And then a equal to b; the value of b will be stored in a. 149 00:13:02,930 --> 00:13:04,810 So, a become 6. 150 00:13:04,810 --> 00:13:09,400 And then b equal to g; the value of g will be stored in b. 151 00:13:09,400 --> 00:13:12,200 So, b will become 4. 152 00:13:12,200 --> 00:13:24,089 Now, let us just dry run the program or the algorithm and see how it computes the GCD 153 00:13:24,089 --> 00:13:25,089 of two numbers. 154 00:13:25,089 --> 00:13:30,500 So, I will denote the currently executing statement with an icon and I will call this 155 00:13:30,500 --> 00:13:31,690 the program counter. 156 00:13:31,690 --> 00:13:35,970 So, this is at any point, it is the next step to be executed. 157 00:13:35,970 --> 00:13:41,440 Initially, it is at the beginning of the code; where, you take the input. 158 00:13:41,440 --> 00:13:49,850 And we will have three variables, which will represent the current state of a program. 159 00:13:49,850 --> 00:13:55,210 So, suppose you want to compute the GCD of 8 and 6. 160 00:13:55,210 --> 00:13:59,510 So, you have a equal to 8; b equal to 6. 161 00:13:59,510 --> 00:14:02,810 You know that a is greater than b. 162 00:14:02,810 --> 00:14:08,950 So, you proceed. 163 00:14:08,950 --> 00:14:13,650 Now, you test whether b is 0. 164 00:14:13,650 --> 00:14:22,290 So, since b is non zero, you go into the main body of the loop. 165 00:14:22,290 --> 00:14:29,300 So, you do g equal to a modulo b; a equal to b; b equal to g, this step once. 166 00:14:29,300 --> 00:14:37,700 So, you will end up with a is now 6; b is 2; and g is 2. 167 00:14:37,700 --> 00:14:44,200 You again comeback to the discussion and test whether b is 0 or not; b is not 0. 168 00:14:44,200 --> 00:14:49,790 So, you go back into the body of the loop again. 169 00:14:49,790 --> 00:14:55,520 So, you have g to be a modulo b. 170 00:14:55,520 --> 00:14:58,830 So, 6 modulo 2 should be 0. 171 00:14:58,830 --> 00:15:01,850 And then you do a equal to b and b equal to g. 172 00:15:01,850 --> 00:15:07,520 You will have a equal to 2; b equal to 0; and g equal to 0. 173 00:15:07,520 --> 00:15:09,770 At this point, b is now 0. 174 00:15:09,770 --> 00:15:22,450 So, you say that a is actually the GCD of three numbers – of the numbers 8 and 6. 175 00:15:22,450 --> 00:15:27,590 So, you can ensure that, it computes the GCD correctly.