 Hello, and welcome to the NPTEL course on an introduction to programming through C++. I am Abhiram Ranade. The topic for this lecture sequence is loops in various applications. So we are going to study three themes today. One theme is brute force search. So often we find that we may not have any clear clever algorithm to solve a certain problem. However, we can identify potential solutions and we can just examine all of them. On a computer this can happen very fast even if the number of solutions is possible candidate solutions is large but finitely many. So this turns out to be an interesting way of solving problems. So we are going to see two examples of this. Then we are going to talk about modeling a system. So in this we will have some kind of a system which will respond to stimulation from the outside world. The first system we consider will be rather simple. It will respond to the outside world but it will not really have any state. So between responses it does not have to remember much. The second system we talk about will be affected by what the world does to it and its responses will therefore depend upon what has happened earlier. So modeling such systems is a very important use of computers and we will see that just by using a single loop you can sort of do a lot. You can model systems very nicely. And then we will consider a problem relating to computer arithmetic. So we will talk about arithmetic on integers which have many, many digits. So as you know such integers cannot be easily processed. So we will see what kinds of things need to happen for us to be able to process such large integers. So let me begin with this idea of brute force search. So to tell you that I need to tell you about constraint satisfaction problems on finite domains. So a constraint satisfaction problem looks like this. It asks find x, y, z such that they satisfy some set of constraints. You can have constraint satisfaction together with optimization. So this problem looks like find x, y, z such that they satisfy these constraints and in addition something should be as small as possible or maybe as large as possible. So here are a few examples. So we have seen the GCD problem earlier. So the problem is given to integers a and p we want to find their greatest common divisor. Now we can think of this as a constraint satisfaction problem. How? Well x is what we want to find and the constraints that x has to satisfy are that x must divide a and b both. So it must be a common divisor. It must divide a and b both without leaving a remainder and there is also an optimization angle we want x to be as large as possible satisfying these constraints. So earlier we have seen algorithms for solving this problem. Now those algorithms in particular Euclid's algorithm was a very clever algorithm. What we are going to do today is solve this using a very simple algorithm but it will take a long time. But that is okay. Suppose we are not clever enough and suppose or suppose in some problems we are not able to have any clever algorithms. Then the approach that we are going to talk about today will still be useful. So that is the point of discussing GCD. So we want to, we have formulated GCD as a constraint satisfaction and optimization problem and then we can use the idea of brute force search as we will see soon. The second problem is that of discovering or that of listing out Pythagorean triples. So specifically we want to find x, y, z integers such that x squared plus y squared equal to z squared and we want these numbers to be distinct and we have put down a condition additional condition that these numbers should be smaller than 20. Why are these things called Pythagorean triples? Well, you know the theorem of Pythagoras. So it says that if you have a right angle triangle then the sum of the squares of the two sides, so let us say x and y are the side lengths then the squares are x squared and y squared. And the sum of the squares is equal to the square of the hypotenuse. So z squared equal to x squared plus y squared. You know that you can have a Pythagoras, an right angle triangle of side length 5, 4 and 3 because 5 squared is equal to 4 squared plus 3 squared and because of the theorem of Pythagoras these numbers 5, 4, 3 are called constitute what is called a Pythagorean triplet. So people have been curious about such numbers and what we are doing over here is we are going to look for them. We will not do anything clever but still we will be able to find such numbers reasonably easily. So here is the simple way of finding GCD that I was talking about. So let me make the first observation which is that the GCD itself the greatest common divisor of any two numbers must be at least one, clearly. And the common divisor cannot be larger than the smaller of the two numbers than the minimum of the two numbers. So what does this mean? So this means that x can potentially be one of these numbers, 1, 2 all the way till minimum of A and B. It cannot be a number beyond this, it cannot be a number smaller than this. So here is the strategy. So we are going to try out each integer between one and then A, B and we are going to check does that integer perfectly divide both A and B? If it does of those integers we will pick the one that is the largest. Simple enough? Notice that the strategy is much, much simpler than Euclid's algorithm for which you needed a fair amount of cleverness. Notice on the other hand is very simple. It will take more time than Euclid's algorithm, it will require more arithmetic operations. But maybe, maybe you do not care or maybe you did not, you are not clever enough to discover a good algorithm for finding GCD. Of course there is the high school algorithm as well. So there are several choices and this is just one way of doing things and as I said I am showing you this so that you get a feel for this notion of constraint satisfaction and searching through a set of possible candidates. Now here is a way to find Pythagorean triples, again it is a very similar way. So we can observe that x, y, z, all the three numbers must be at least one and at most 20. Well we said that z must be smaller than or equal to 20. So clearly these numbers must be at most 20 and why should they be at most one? Well if they are 0 and we want them to be integers, if they are 0 then what happens? Then we will get a trivial solution that y squared equal to z squared. So we do not want such trivial solutions. We want these numbers to be non-zero and therefore they are expected to be distinct as well. It follows that they have to be distinct. Well if the numbers lie between 1 and 20 or another way of saying that is that the domain of each has to be this set mentioned over here, the numbers 1 through 20. So if this is the domain for each number, each x, y and z then what can we say about all possible triples? Well there are just 20 times 20 times 20, 8000 triples and we know that one of these triples, so we need to know which one of these triples have satisfy the Pythagorean property. So the algorithm naturally is that we are going to try out, we are going to generate and try out all possible triples and for each triple we are going to check does it satisfy this equation. If it does we will print it out. So again we are not being very clever over here, we are just doing something really brute force. Trying out everything, but computers are fast and so therefore sometimes it is okay if we want to try out everything, computers may do it fast enough. So let us see in more detail how we solve this GCD problem. So the problem again is find x such that x divides a and b and is as large as possible. So the idea we mentioned was try out all possible values of x between 1 and minimum of a and b and pick the largest which divides both. Now we have to examine the values between 1 and minimum of a and b but we could, we have a choice of examining these numbers in whatever order we want. So let us consider the natural order first. So we first try out x equal to 1, c if divides, of course it divides, x equal to 2, c if it divides, x equal to 3 and so on. And then we list out all such, whether or not any of these numbers divides a and b perfectly. So we consider x in this order 1 through minimum of a and b and we remember which number which x was dividing both and now from those we pick out the largest. So that is one way of doing things. So this will work but here is another order. So in this we are going to start with the minimum of a and b. We are going to consider the largest possible candidate. We check if that divides both a and b. What if it divides both a and b? Do we need to check the next smaller candidate? Well we do not because the next smaller candidate will be smaller and we want the largest solution. So this is just going to be a better way of doing things. So as we go down this list, once we find an x that divides both a and b we can stop. So indeed we should consider these numbers in this order. So any common factor that we discover later can only be smaller than common factors which we have discovered earlier. So therefore we go in this order and stop as soon as we find an x which is a common factor of both a and b. So now the program is immediate. So here is the program fragment. We will use variables a and b and into it we will read the numbers whose GCD we want to find. Then we are going to start with x equals minimum of a and b and we are going to keep on decrementing it and so long as it is bigger than 0. And what do we do in each iteration? Well we check whether a is divisible by x perfectly and b is divisible by x perfectly. If it is then we are going to print it out but we can also stop the loop right here. So we are going to break. That is it. So that is the program. So some remarks. This turns out to be much slower than Euclid's algorithm. I mentioned that earlier and in general brute force algorithms are slower than cleverer algorithms. That should not be a surprise but the point is that you should consider brute force if you cannot think of anything else. You should not give up because computers are fast enough and even if you have to examine a large number of candidates, you may be surprised how quickly computers can go through all such choices. What are the requirements? Well the candidates that you want to search over, the possible values that you want to search over must be finitely many. You could be a large number but they have to be finitely many. And we should be able to generate each possible value. In this case the generation was very easy. We just had to count, count down, count up, does not really matter. And given a candidate we should be able to check the constraints. Now if we generate the candidates or if we generate the values largest to smallest then the first solution we find will be the largest possible. If we generate values smallest to largest, if the candidates we generate are in the order smallest to largest then the first solution we find will be the smallest possible. So depending upon whether you want the smallest possible solution or the largest possible solution you should pick one of these orders. We are going to take a break now but when we come back we will have a more involved example the Pythagorean triples which we will again solve using brute force search. Thank you.