 Welcome back, in the previous segment we looked at how to solve GCD using brute force search. We said that this is slower than Euclid's algorithm, but we said that brute force search will be useful if we do not have clever algorithms for some problem provided the number of possible candidate solutions that we need to consider are finite in number. Now we are going to take another example of this and this example is finding Pythagorean triples. So the problem as we discussed is find x, y, z such that x squared plus y squared equal to z squared and z is smaller than 20 and of course x, y, z are positive integers. The idea we mentioned was that let us try out all possible triples x, y, z where x, y, z lie between 1 and 20. So how do you generate such triples? That is the natural question and that is actually very easily done. So we are going to have a loop which is going to be, which is going to have a control variable or index variable x which will go between 1 and 20. Nested inside that will have a loop with control variable 1, control variable y which will go between 1 and 20 as well and then will have a loop with control variable z again ranging between 1 and 20. So now whatever we put over here we can use the variables x, y, z and in different iterations they will assume all the possible 8000 triples of the form x, y, z where x, y, z are integers between 1 and 20. So if you want to check for all possible triples the check has to be put over here and that is the check. We want to know whether x squared plus y squared is equal to z squared and if it is we want to print out that triple. So that is it, that is the program. So let me show you a demo of it. I have called it knife Pythagoras.cpp and you will see the reason for it. So this is our program exactly what we had written earlier. So let me compile and run it. So it has printed all the triples. Let me move it out a little. It has printed all the triples 3, 4, 5, 4, 3, 5, 5, 12, 13. These are all the triples that it found satisfied the property that the sum of the first number squared plus the second number squared equals the third number squared. You can check it out. So first of course we have the familiar triple 3 squared plus 4 squared equal to 5 squared. Now is this a satisfactory list? Well it is not. Why? Because this triple and this triple we would like to think of as the same triple. I mean if you want to send this to somebody they will say look why are you sending me this twice. I know I mean these numbers the order is not really important in these numbers. So and of course that has happened in many places. So 8, 15, 17 is there, 15, 8, 17 is there. So the question is can we avoid this and yes we can and we are going to do that in just a minute. So we are going to look at this problem again and make a modification which will allow us to generate similar triples only once. So this is our problem again and this was our idea and this was our problem. So we want 3, 4, 5 to be printed and not 4, 3, 5 or maybe the other way around but we could say that look why do not we print this in ascending order rather than this looks a little funny. So we only want one of these things to be printed. So how can we enforce that? Well here is the idea. So in addition to the constraints which are already there in the problem we will additionally assert that x should be less than y. What does this do? So this will allow this solution because this is x and this is y and x is indeed less than y but this solution will be avoided because this is not less than this. So that is the idea. So we are going to have the previous program but in the previous program we are somehow going to enforce the condition that x is less than y. There are various ways of enforcing this condition but we are going to pick a particularly nice one. And by the way note that x equal to y does not have to be considered. Why? Because if we look at x squared plus y squared then that if they are equal would be 2 times x squared and that cannot be a perfect square. So it cannot equal any z squared. So x equal to y does not have to be considered. We do not want x greater than y and therefore we should only be asserting x less than y. And in fact you can see that the conditions really could be something like this because we know we are asserting that y is bigger than x and of course z has to be bigger than y because its square is strictly more than the square of y and x. So let us see how we can assert these conditions somewhat indirectly. So let us look at the code. So here is the code. So the first loop is exactly as before. x goes from 1 to 20. The next loop however has y skipping all the values including whatever the value x is currently taking and smaller values. So x, so y only starts at x plus 1 and it goes on till 20. And z similarly skips all the even all the smaller values because we know that z has to be bigger than y and so it starts off at y plus 1 and goes on till 20. And the condition is still the same. If x squared plus y squared equal to z squared then we print it out. So let us see this. So let us compile it and run it. So that is it. It has indeed done the right thing. It has printed every triple just once and so 345 is printed as 345 but not as 435 which was happening earlier. So let us get back to our presentation. So I want to give you some exercises. So the first exercise is dealing with something called perfect numbers. A number is said to be perfect if it is the sum of all its divisors smaller than itself. So 6 for example has divisors 1, 2, 3 smaller than itself and 6 is indeed the sum of those divisors. So therefore 6 is said to be perfect. The problem that I am asking you to write a program for is to find or print all the perfect numbers between say 1 and 10,000. So you have to go through all the numbers between 1 and 10,000 and check if they are perfect and if you find that a particular number is perfect just print it. A harder exercise relates to something that the famous mathematician Raman Rishan is supposed to have said. So he said sometime that 1729 is the smallest number that can be expressed as sum of cubes of 2 integers in 2 ways. In particular 1 cube plus 12 cube is 1729 because 1 cube is 1, 12 cube is 1728, 9 cube is 729 and 10 cube is 1000 and therefore 9 cube plus 10 cube is also 1729. So this is an interesting fact and you may wonder, suppose I did not know this, could I have figured it out. So that is where whatever problem is going to be about. So I am going to assume that suppose you did not know this and you were told however that such a number exists and is smaller than 2000. Can you find it? And in fact you could ask look I want the smallest such number. So that is the problem. So write a program which finds the smallest number and you know and assume you are assuming that it is smaller than 2000. So the smallest numbers which can be expressed as sum of cubes of 2 integers in 2 different ways. So you are expected to do brute force search. So you will have to figure out exactly how to organize that brute force search. So you have to express it as a constraint satisfaction problem. So think about it, it will as I said it is a slightly harder exercise. So the number that you want to find is certainly one of the variables that you want but what about those numbers 1 cube, 12 cube, 9 cube, 10 cube. So how do we exactly get them into our constraints? So that is what you need to figure out. I want to list out an even harder exercise but which may challenge some of you and which some of you may like and this is the so called 8 queens problem. So this problem asks you to place 8 queens on a chessboard such that no queen captures another. So for those of you who do not know chess here is a quick explanation. A chessboard has 64 squares arranged in 8 rows and 8 columns. So it looks like this. So there are going to be 8 rows and 8 columns. Whatever, this is not 8 but there should be 8 rows and 8 columns. So how many is this? 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6. So if you had 7 and 8 and 7 and 8 then it would be a chessboard. Now that is what a chessboard is and this says if 2 queens are in the same row or in the same column then they will capture each other. So if you have a queen over here and a queen over here then they will capture each other. So if you place a queen over here then you should not be placing a queen over here. That is what the constraint tells you. And similarly if you place a queen over here you cannot be placing a queen in any square in the same row. So you cannot place a queen over here because they would capture each other and you want to place queens such that they do not capture each other. But this is not the only way queens capture each other. So queens capture each other even if they are sort of in the same diagonal or since the notion of diagonal only refers to the principal diagonal which is from corner to corner we can say the same thing in a different manner. So suppose there are two queens so these two queens and if you draw the line joining them then this line is at 45 degrees angle with respect to the rows and the columns. So even then they can capture each other. So this is also not allowed. So for example you could have placed if you want to place three queens you could place one over here, another one over here, a third one over here maybe. So now no queen is capturing each other maybe even a fourth one over here. So in this manner you could have placed the queens. And it turns out that in this case you can place the sixth queen over here. So you have placed six queens on a 6 by 6 chessboard. But you want to do this on an 8 by 8 chessboard. Well that is the original problem but we do not want to get carried away with having too many variables and writing too much code and in particular the spirit of the problem is still the same even if you are looking at a 4 by 4 board. So if you want to place four queens on a 4 by 4 board how will you do it? You want to write a program which will do it for you. So basically the idea is that you try out all possible placements of the queens on the board. So you will have to figure out how do you represent this problem using numbers. And suppose you say that this queen is placed on this square and this queen is other queen is placed on this square. What does it mean for the queens to not capture each other or capture each other? So the constraints here are expressed in terms of this geometry but you have to get them into arithmetic. So you want the constraints to be in terms of numbers. So that is what makes this exercise harder. But once you do that again after that you are not expected to be clever. After that you are just supposed to try out all possible ways of placing the queens. All right. So what did we discuss in this segment? Well these two segments actually. So what we discussed was that for many problems we may not know any clever ways of solving them. But if there are only a candidate, only a finite number of candidate solutions then we can go through them systematically. We can check which candidate solutions satisfy, candidate solution satisfies all constraints and we report that as the real solution. This can take very long but it is better than not having an algorithm. But of course there is room for cleverness. We may have to do things like let that enforce additional constraints say x is less than y or something like that. And notice by the way that that actually also reduces the amount of searching we have to do. So any kind of cleverness that we are capable of in reducing the number of possibilities always helps. It allows you to solve bigger problems in the same amount of time. So that finishes this topic of brute force search and now I am going to take a break and after the break we will discuss how to write programs that model a physical system.