 Good morning and welcome back to the third lecture. In the last session we saw how C programs could be written to execute instructions conditionally and thereby we could solve many problems which required alternate execution of either one group of instructions or another group of instructions depending upon a certain condition. We of course also saw the details about the data types, the computations, the precedences of operators and also the kind of comparisons that can be made. You will remember that we looked at a problem of finding out the maximum of given numbers. At that time I had suggested that this is a precursor to introduce the notion of iteration. I have always found it useful to introduce a new notion through a need which is exemplified by some example which we have attempted to solve using the concepts learned so far. So, we will in this session discuss the problem of finding maximum of given numbers. In the process we will discuss instructions for specifying iteration using for and using while. More specifically we will consider the problem of estimating value of the natural logarithm of a given number x and if time permits we will discuss the general problem of finding roots of a polynomial. However, I believe we might terminate today's session by just looking at the estimation problem for logarithm x. Essentially throughout this session we are going to emphasize the use of iteration in different forms. So, here we go we relook at the problem which we had earlier seen namely finding out maximum of given numbers. I have reproduced the program that we had written or portioned the program that we had written to find out the maximum of five numbers and I have said here that I can read the five values as a, b, c, d and e and then starting with the first number and artificially calling it maximum because that is the only number that we have seen and clearly that is maximum of the number that we have seen so far. We then compare subsequent numbers b, c, d and e with the max and whenever we find any number to be greater than max we replace the present value of max by that number. This is the common sense algorithm most people will understand it we output the maximum value there. The question was what do we do if the number of numbers is very large? We actually asked two questions last time one in the number of numbers is very large. For example, two hundred numbers clearly it is not very proper to expect somebody to write a program with two hundred variables or thousand variables. More importantly if the number of numbers itself is different every time the program is executed then there is no clear way as to how we could write a program to satisfy that requirement. This is where we now say that we will study this particular pattern of giving instructions and see what else we can do in order to modify our instruction giving mechanism and then we will see whether the programming language that we use namely C programming language does it have any features which permit us to control the execution of instruction other than the simple sequence and other than the simple conditional execution. So first we look at a possible strategy going back to the previous slide. We notice that there is a very similar instruction that is being executed again and again and again. Here it is B greater than max, max equal to B, C greater than max, max equal to C, D greater than max, max equal to D. We notice that B, C, D, E, etc. are nothing but the numbers which we are evaluating to find out which is the maximum. Consequently I can generalize this by saying that I observe or I notice a repetitive pattern in our program which says if number is greater than max then max equal to number. This number is a different number every time. In short what I am looking for is if I could force a repetitive execution of this instruction with different value for a number every time this instruction is executed then perhaps I would be able to find out the maximum of all the numbers that have been given to me. So this is the observation. The possible strategies if we could execute this instruction repeatedly every time with a different value of number we will get the desired result. Now how do we ensure that the comparison is made with a new value every time? Well in the earlier program we had read all possible values given to us at one time. Let us go back to the previous slide. Notice that here we had set C in A, B, C, D, E and then we were comparing each of these values with the current maximum and so on. Instead we now observe that if we want to repeatedly execute some pattern then perhaps a better pattern is I read a number and then I compare it with the current value of max. If this number is greater than the current value of max I replace the maximum current value by this number and you will notice that if I can repeatedly execute these two statements every time get a new number find out if it is larger than the current max if it is replaced the value of max. So if I execute it say ten times then up to then I would have read ten numbers and the current maximum of those ten would have been stored in max. As I continue to read more numbers every time the max will be updated provided I find a number which is larger than the current max. In short repeating this particular pattern of instructions again and again and again till the desired number of numbers have been compared and I have found the maximum seems to be a sure short way. At this stage I would like to voice this strategy in words in what we call an algorithm description not necessarily in the programming language. Of course we will use some constructs from C programming language that we understand but effectively if we repeat this block five times we will find the maximum of five numbers. There is a problem there if I go back to the previous slide I can execute this any number of times but imagine when I am executing for the first time for the first time I come here I read a number fair enough then I come to the next instruction it says is number greater than max well subsequently max will have some value but for the first time max will have no value. Now to make this comparison meaningful I must have some initial value for max that is what I have noted on the next slide here that I need an initial value for max. Now the initial value could actually be any arbitrary value which is lower than all actual numbers that I will subsequently give for example if we assume that all the given numbers are positive that means zero or more then we could assign an arbitrary negative initial value to max because the first very first number that comes from the real life as input will be larger than any negative number and that would become the value of max at that point and subsequently the comparisons will proceed as we desire. So expanding our strategy in words what we wish to say is something like this max is equal to say minus 999 this is actually an artificial arbitrary value and this presupposes that all given numbers are positive then I want to say something like this repeat the following block five times see in greater greater number if number greater than max max equal to number do I have an instruction which can make this particular block of instructions repeat five times it so happens that C programming language like most other programming languages provide for such iterative execution of identified block of instructions effectively we need a counting mechanism every time we execute this block we want to increment the count by one so let us say we want to count this as one two three four five and whenever fifth time the block is executed we want to terminate our execution there is an instruction in C called for instruction which provides such mechanism this is the syntax of the for statement which we can explain to our students in fact when you explain you might want to draw a flow chart but what I have found is that the previous statement in the words which I have given repeat this block five times is often adequate for students to understand here we say for count equal to one count less than equal to five count plus plus see in greater greater number if number greater than max max equal to number this block is being executed repeatedly is the purpose of the for statement at this stage we can explain to our students there were sort of counting the number of times the block is executed so there is an initial value to the count which is one then there is an increment to the count after every execution so count is set to count plus one and finally this will continue as many times till the condition count less than equal to five is reached we are very briefly explaining the semantics or meaning of the for statement so effectively inverse we will say that the computer will start with count equal to one execute this block once add one to the count count will become to execute it again count with three execute it again count is for execute it again at every re execution it will keep comparing the current value of count with the final value that we are given when count is equal to five since it satisfies less than equal to five the block will still be executed but when the count is incremented later count will become six this condition will not be valid and we will come out of this so called for loop essentially then this for statement of the programming language permits me an extremely easy way to set up a finitely countable number of times that I want to repeat the execution of a block there are other considerations for example I may say that my values could be either positive or negative in which case clearly the initial value of max equal to minus nine nine nine is not correct why is it not correct suppose I want to find out the maximum of five numbers all of which are negative and all of which are smaller than minus nine nine so one value is minus ten thousand minus twenty thousand minus five thousand minus seven thousand etc. the maximum of them is clearly the least negative value but minus nine nine nine turns out to be larger than all of them then what will happen I may read all those five numbers all of which are large negative numbers as I said but since max is initially said to minus nine nine nine this program will produce a result where the max will be still minus nine nine which is not a correct value how do I account for this particular problem the suggestion here is that the artificial value is not a good idea anyway as I said what if we are given negative numbers also or in fact only negative numbers so a better thing would be that since the maximum is to be found amongst the given numbers and since numbers are being given to me one by one the best thing is take the first number and call it the maximum after all that is what we did in our earlier version of the program we set max to a which was the first number that we read and then compared max with b c d e etc. etc. why not we do the same strategy we read one number separately assign it to max now we read second number onwards all the remaining numbers and continue with our repeated execution of that comparison consequently I will have to iterate the loop if I am finding out the maximum five numbers only four times because the first number has already been taken care of so here is the general instruction then c in number max equal to number the first number comes into the location for number and it is assigned to max now I set up the iteration for count equal to one two and including four so this execute this iteration will be executed four times every time a number will be read compared with the max and max will be reset if we want to this is a very clean mechanism because the maximum number that we so find will always be amongst the numbers which have been given to me since there is no artificial initialization of max at the beginning we can extend this to write a program which will find maximum of n given numbers where n itself is variable observe that earlier we said that is impossible to write a program by using explicit variables to represent individual numbers but once we have a single variable representing a number and we keep reading a new value or a new number in the same variable then we can handle any number of numbers so look at the program which saw results program says program four to find maximum of n given numbers in count number n max and I read the value of n now somebody executing the program might want to find maximum of 200 numbers somebody else might want to find maximum of 100 numbers somebody else still might want to find maximum of 7 numbers whatever is the purpose when this program is executed the number of numbers will be read as input so if I want to find out the maximum of 100 numbers I will give n as 100 now observe how the algorithm works I will first read the first number which is as per my previous strategy and assign it to max now I will run the repeated execution of this block which I had already identified not n times but n minus one time because the first number I have already read consequently I read the first number assign it to max then I read the remaining n minus one numbers one by one see in greater greater number and every time I compare that with the current max reassign the max if I so wish if the comparison so indicates and when I come out therefore I would have found out the maximum of n numbers this are very similar example is useful to emphasize to the students that without an iterative control structure we will be unable to solve many real life problems of this and similar time observe that from a teaching perspective an important aspect of teaching this is not just the syntax and semantics of foreign wide statement but to lead to the need for such a statement by showing an example that I took for example finding out maximum of 5 numbers and 7 numbers and what happens when I want to find out maximum larger numbers thus identifying a repeatable block of instructions and then decide designing or constructing an algorithm which actually takes that into account of course you can construct any other similar example but my submission is please try to take an example which can be solved partially using the constructs of conditional but if you want to really extend it or generalize it you will not be able to do that without having an iterative control this is one example which I found to be very straightforward simple and meaningful and therefore I use this to introduce the iteration of course having said this now we can go ahead and describe other details about the iteration for example this is the general format and semantics of the forced it so there are four components of the forced statement I have called them just arbitrarily as xxx yyy zzz and ww w so what are these xxx is a initialization thing so we say it must be an assignment statement we call it loop initialization yyy is the terminal condition so the condition is evaluated every time the loop is executed loop test is what is the name that we give to yyy what is zzz zzz is the loop increment and ww w is the set of statements which we have identified which need to be repeatedly executed what we have tried to do here incidentally this slide comes from the slides that are prepared for our CS11 course by a colleague of mine there have been many excellent teachers who preceded me teaching this CS11 and in IIT we usually use the better ideas and slides of our colleagues I mentioned to you yesterday that the objective here is not only these slides but such slides and other learning teaching material that any one of you has created out of passion and you have tested it could be put together and all of us could use them more specifically I would like to put on record the use of material prepared by professors Sudarshan professor Sharad Chandran notably professor Abhiram Lanade who was our head till last year and professor Mellon Soni so look at this simple explanation of the general format and semantics of the first statement it gives steps 0 1 2 3 4 the 0 8 step is execute xxx it must be an assignment statement so this says this is first initialization of the loop is carried out by executing this statement then it evaluates the condition y y y is the condition valid is it true then continue with the next step otherwise the first statement is terminated please note that first statement can therefore terminate without even once executing the body of the loop of course if the condition is true I will go down to w w w and the loop body is executed once after executing it I don't fall through but I go back and execute z z z which is the loop increment and after executing the loop increment what do I do go back and repeat from one so this is the iteration in this iteration xxx initialization is outside the iteration it is done beforehand much like assigning the initial value to max outside my iteration but this is implicitly done by the first statement itself remember the statement like count equal to one or I equal to one or whatever may be the initialize so this then is the general general explanation of how a false statement works instantly we can also tell our people that loop initialization xxx need not initialize only one variable it can actually initialize more variables subsequently when we discuss variations of the iteration specification we will see some very interesting possibilities in which the iteration could be prescribed in a C program we can extend our previous idea of finding out maximum of n numbers to a something to do something very similar find out the sum of first n natural numbers please note that the first n natural numbers are very easily defined as 1 2 3 4 5 6 7 8 up to l consequently I don't have to read a number here I want to keep adding the numbers one by one into a sum such that I accumulate the total number total sum of numbers up to n I do this repetitively and I form my algorithm exactly the way I formed the algorithm earlier so here I have int i n sum equal to zero incidentally an initialization is perfectly possible and valid while you define the data type and the existence of a variable so this is a valid statement now I read in the value of n and what is the loop that I set up for i equal to 1 i less than n i plus i equal to i plus 1 or i plus plus sum is equal to sum plus what is i i represents actually the number so first it is 1 then it is 2 then it is 3 then it is 4 etc etc etc etc it finds out actually the sum of numbers from 1 to n minus 1 because it will last time it will execute when i is less than n and then see out is sum of n numbers where we just output this it is interesting that even in this simple program to execute this with different values of n and to try to determine whether you actually get the correct value of not usually there is some problem or the other in the loop test condition or the increment or the initialization and testing a program with different values is a short way of ensuring that we have not missed out on any extreme value there in fact I would definitely like you to of course you can find out what is the problem with this program but it is best to run this just like that and see exactly what happens next I would like to quickly discuss another application for iterative execution of instructions in fact without iterative execution we may not be able to solve such problems this is a problem plumbly in the domain of numerical analysis traditionally numerical analysis courses which are conducted separately will discuss these kind of problems. I believe that it is important for us when we teach computer programming to also discuss these problems because it is only in the context of these problems that computer programming is actually useful to most of our students and as a matter of fact to all of us. So let us spend some time in discussing how natural logarithm is computed first of all we start with a definition natural logarithm of x is nothing but a definite integral from 1 to a 1 upon x dx this is well known to our students all of all of them know this this is the formula for log x but since the computer cannot integrate it must use arithmetic operations to evaluate this definite integral how do we evaluate a definite integral first of all what is an integral so we go back to the basics and say integral is nothing but area under the curve and if it is an integral between 1 and a then the area under the curve 1 by x from 1 to a will be the value of this integral from 1 to a and therefore we can estimate it if we can estimate the integral correctly so essentially I have a function f x equal to 1 by x and I want to estimate the area under this curve from 1 to a now the curve itself is not a straight line curve or something obviously 1 upon x is a is a different kind of curve. So can we how do we estimate the area well what we do is we break the entire area into small rectangles now for a rectangle we can easily calculate the area of that rectangle and if we have large number of rectangles approximately representing the area of the curve under consideration we should be able to get a good enough estimate of the final value which will be very accurate. So here is some diagrammatic representation of the situation this process by the way is known as Riemann integral so most of you would of course be familiar with Riemann integrals I am not sure whether our undergraduate students in first year would be familiar but never the less there is no harm in introducing these terms here incidently most of you would have read Wikipedia for some purpose of the other Wikipedia happens to be one of the best resources on technical terms and things as well Riemann integral for example is described extremely well in the Wikipedia. So those students have not heard of it but are interested you can refer them to Wikipedia you can just open up Wikipedia search for Riemann and they will find out exactly this kind of curve and method. So calculating some of areas of all rectangles between 1 and a here is a example that is shown here I have tried to draw rectangles like this so this is first rectangle second rectangle third rectangle notice that as the curve 1 upon x becomes flatter the error or the difference between the area of rectangle and the area in the under this curve reduces on this side you will notice that this much area is counted wrongly if I take the area under the curve as the area of this rectangle this rectangle but if the rectangle itself is smaller then obviously the error will be less and less and less that is the idea behind computing the integral by estimating the sum of the areas of all such rectangles and more the number of rectangles the better it is. So how do I do that how do I write a program to do that let us see what I want to do I want to calculate the area of this rectangle then add it to the area of this rectangle then add it to the area of this rectangle etc etc as many rectangles as I decide to form between 1 and a because that is the definition of log x. So if I want to find out logarithm of the value then I need to find out the sum of all rectangles between 1 and a and now I will take an arbitrary rectangle how do I estimate the area of that rectangle here is the observation consider ith rectangle say this is ith now what is the width of that rectangle well width is exactly same because I am dividing the area under this curve under a certain number of rectangles of equal width this is by definition and therefore the width is simply a minus 1 by n where n is the number of rectangles what is a minus 1 a minus 1 is nothing but this complete interval if n is thousand the width of each rectangle will be a minus 1 by thousand if n is hundred the width will be a minus 1 by hundred etc etc now I want to calculate the area of this rectangle I have shaded this area here how do I calculate the area I need to know the width which I already found out I need to find out the height now height of this rectangle how will I know I need to calculate the value of this function f of x is equal to 1 upon x at this value of x I first determine what is this x so what is the x coordinate of the beginning of the ith rectangle this is my ith rectangle clearly if I look at the numbers here x is equal to 1 plus i minus 1 by w so this is my first rectangle the x coordinate of the beginning of the first rectangle is 1 itself so it is 1 plus 0 so if i is 1 1 minus 1 into w is 0 I get the value correct what is the beginning point of the second rectangle since i is equal to 2 the formula will give me 1 plus i minus 1 which is 1 again into w that is 1 plus w which is correct one is this point I add w to it and I get the x coordinate of the second rectangle why we are discussing all this and why we should discuss all of this is to convince the students who may have understood the definition of log x but may never have seen a numerical analysis procedure like this to estimate the value of log x by calculating the sum of areas and of the rectangle so it is worth why to explain this so we calculate x coordinate of ith rectangle in this fashion having found the x coordinate it is easy to find out the height because height is nothing but the value of this function at this value of x is explained in the next slide the height h of ith rectangle is nothing but 1 upon x why because the function is 1 upon x so if x is this coordinate this height is 1 by x because that is the value of this function at that point and what is 1 by x it is nothing but 1 upon 1 plus i minus 1 into w remember that x was originally calculated and was shown to be equal to 1 plus i minus 1 w consequently for every rectangle i th rectangle I have very clearly the width which is w and the height which is h available in terms of the known quantities now I look at the actual problem how many rectangle should I have well the observation is more than a year so let me arbitrarily start with thousand rectangle the total width of rectangle is a minus 1 that is the whole range and the width of each rectangle is simply a minus 1 upon thousand the x coordinate of the left side of ith rectangle I am now recalling all the formulae that we have worked out is x equal to 1 plus i minus 1 into w and height of ith rectangle is 1 upon x is equal to 1 upon 1 plus i minus 1 into w what I have done here I have consolidated all the formulations that we have explained to our students to recap very briefly I will go back four slides I am trying to compute natural logarithm my computer cannot calculate integrals so we make it compute areas of rectangles which are under the curve and if I divide the area under the curve in large number of rectangles I guess that my estimate of the sum of those rectangles should be quite close to the final integral value this is my breakup of rectangles I identify how will I calculate the area of the ith rectangle first I need to know its width which is fixed then I need to know its height for which I calculate the x coordinate of the left corner of the rectangle then I calculate the height by just using the function definition because it is nothing but the function value at that x coordinate and finally I conclude that these are the values that I have for ith rectangle why am I saying ith rectangle ith rectangle well we can now tell our students that look we have a strong iterative mechanism thereby I can choose some variable like I or count and I can automatically vary that value by 1 2 3 4 etc etc and execute the kind of instruction that you want me to execute consequently a pattern of instructions which need to be repeatedly executed can be specified very simply in my computer program and consequently to actually implement such an iteration I need a variable which is equivalent to a counting variable and I naturally have this I as the counting variable consequently I can write a program to compute logarithm of a given value in this fashion let us see what this program does it defines integer defines floating point x it says area is 0 and it defines w as a variable which will represent the width I read the value of x remember in the diagrams whatever we are showing a 0 1 to a is actually log of a so 1 to any number x basically whatever number we give we are calculating the value of log x so we give the value of x here now what we do we calculate the width as x minus 1 by thousand where does this thousand come from well we have decided that we are dividing the entire area into thousand rectangles please note that would be a good idea to put a comment here that we are dividing this area into thousand rectangles now the fun starts having calculated the w I want to calculate the areas of each one of the thousand rectangles so I vary the value of I from 1 to thousand including thousand in steps of one which is what this first it when does and for each value of what do I do I simply calculate the area of ith rectangle which is w into 1 upon 1 plus I minus 1 star w you can confirm that this entire thing is nothing but the area of ith rectangle I add it to the previous value of area so as I go from rectangle to rectangle to rectangle first I will calculate the area of first rectangle to put it in the value of area then I calculate the area of second rectangle by this formula and add it to the area so area is now a cumulative variable which you show me the cumulative value of all the area finally when I complete this entire for loop I have actually got the value of the total area which is my estimate of the logarithm of x I simply print out a string called log of x and put the value of x and say is so much area so you can see how short and sweet a program can be to do this significant amount of computation incidentally if I am not comfortable with the accuracy that I achieve I can actually divide this into ten thousand twenty thousand what could be the largest number instead of speculating we can ask our students to experiment with this program running it differently you can actually in the lab assignment today you have a sample program which calculates log x it's a c program where you are required to quickly compile it and run it it actually calculates the method of the value of logarithm in three different ways one it calculates the areas in this fashion the other it calculates the areas backward third it calculates the logarithm value by using a standard mathematical library function where the logarithm value is computed internally by c most probably using a very similar method exactly the same concept extended can be used to iteratively compute the factorial of a number after all just as we calculated growing some we can find out a growing product in the previous example let's go back to the previous slide we had area equal to zero and we were constantly adding to that area in every iteration so it was a growing area or growing some for a factorial where we had to calculate not some of numbers but product of numbers one two three four five so we can calculate a growing product here is the program for that I define in factorial as a integer variable n as the number whose factorial I want to calculate and I as usual is my counting variable I read the value of n I set n factorial to one this is the initial value observe that factorial zero is also one so whatever be the value of n the starting point is correctly one now for I equal to one to n I do the following iteratively I multiply the current value of n factorial by a value of I and assign that as the new value to n factor consequently first time n factorial be one when I is to n factorial become 2 then when I is 3 2 will be multiplied by 3 it will become 6 when I is 4 6 will be multiplied by 4 it will become 24 and so on so when I terminate this loop I would have actually calculated the n factorial correctly notice that this particular way of computation what is the care we have to take factorial value increases very rapidly iteration is an extremely powerful concept what we have studied is iteration for a finite number of times there could be iterations which I might want to continue till a certain condition is made apart from the for statement there is another construct in C programming language called while another still called repeat and all of these permit very generalized forms of iteration set up before going to discuss the why I want to discuss another problem which is a very interesting problem all of you are familiar with that problem actually but the way it was introduced by my colleague professor run a day makes it far more interesting and I thought I will share that with you I have already discussed this problem in this particular way with the students of programming here and they all like the special peculiar nature of this later on you will find that you actually know this problem but in a different problem presented here is called the Hemchandra's problem Hemchandra was actually a 12th century poet think 1115 to 1150 or something but in those days you could be a poet a mathematician and a philosopher and a doctor all simultaneously he was one of those intellectual joints who was not just a poet but also as a great mathematician this problem I have stated in fact professor run and himself did this restatement in a slightly different way so that all of us can understand what is the problem here this is not Hemchandra's problem by the way but it's a similar statement it says I want to build a wall of length 8 feet and I have bricks which are 2 feet long and also I have bricks which are 1 foot long in how many ways can I lay the bricks so that I fill the 8 feet length there is a very generic question obviously more than one way in fact many more ways than one here are some examples I can take 4 bricks of 2 feet each okay I complete 8 feet I can take 8 bricks of 1 feet 1 foot each and I can complete the leg I can take 3 bricks of 2 feet each and 2 bricks of 1 foot each making it 8 and this way there are umpteen possibility what is the reason what is our interest in knowing how many different ways I can fill in the 8 feet this brings us to the original problem which Hemchandra was tackling which was a slightly different problem since he was a poet the actual problem was designing a poetic meter those of us who are not well versed with music and I assure you I am one of them may not understand the intricacies of the musical poetic meter systems etc but suffice it to say that these poetic meters have a short syllabus and a long syllabus roughly a short syllable represents one beat and a long syllable represents two beats this is very common in the poetic meters all that Hemchandra is asking is how many ways are there of filling 8 beats here this peculiar problem he wanted to know in how many different ways I can put together short and long syllables such that I can make 8 beats here is just a arbitrary example of a poetic meter in fact these lines that you see here are from a very well-known Sanskrit poem and these are the representative long beats and short beats as you see there coming back to the mathematical problem Hemchandra solved this problem in a very peculiar way he said by the method of Pingal Pingal was a Rushi many years many centuries before Hemchandra incident and Pingal had said that to solve this kind of problems it is enough to observe that the last beat is longer short so no note what he's saying let's go back two slides again to our brick thing what he's saying is to figure out how many such ways I can fill up the 8 feet wall if I want to find out how many such ways I only need to observe the last brick whether it is long or short if I can observe that and concentrate that there is an easy way of counting all these number of ways well how do you do that we'll see that solution but it is worth why to note that Pingala was a mathematician and poet from 580 so what is important here and I want to emphasize this and I want you to emphasize this to students is Hemchandra is giving credit to someone who lived hundreds of years before him contrast it with the problems that we face where our students routinely copy programs from either books or others and do not even mention it the moral of the Hemchandra solution in my opinion is that it says this message copy if necessary and if permitted but always give credit in fact why I emphasize this point is that in our endeavor to create open source contents through collaborative communities there will be occasions when each of us will be submitting something there is a possibility that we see that some material in some particular place is extremely useful and it is perfectly all right for us to say that we submit that material in open source after taking due permission of course however it is also equally important to say that we acknowledge the creator of that particular piece of information or knowledge so copy if necessary and if permitted but always give credit is a non numerical moral of the Hemchandra solution of this problem let us of course continue with the discussion on the numerical aspect of the solution so here is a some description of the solution please note the original problem was find out in how many different ways I can construct a 8 bit meter so Hemchandra says that using method of Pingla I divide the 8 bit patterns into two classes one class which has a short last bit another class which has a long shot last bit the Pingla says just observe the last bit now I do not know how many such patterns will be there with one short bit and how many such patterns will be there with one long bit but I divide my problem into two classes I say S is the class of 8 bit patterns with short last bit and L is the class of 8 bit patterns with long last bit and now I conclude a very important observation each 8 bit pattern is either in class L or in classes there cannot be anything else because I have only two types of bit short and long and any pattern can either end in a short bit or can end in a large bit so therefore long or short are the only two possible classes so while I do not know how many patterns are in L and how many patterns are in S I know for sure that the total patterns in L plus total patterns in S together is the total 8 bit patterns that I can have but now I look further and I see something very interesting what is S S is after all 8 bit patterns with short last bit consequently I take all possible 7 bit patterns and put a short bit at the end I will get the classes classes cannot have anything else because short bit appended at the end means all 7 bit patterns can only take a short bit because total bits have to be 8 now I see what is L L has a long last bit that means it has a value 2 so obviously if I want 8 bit patterns with long last bit then if I take all 6 bit patterns and for each of those I append a long bit then I will get the total number of patterns in the class L so notice that what is being done is I first define the possible number of patterns in S and L class and I know that each 8 bit pattern is either in L class or classes then I further define what is S and what is L in terms of 7 and 6 bit patterns it is very obvious that you can continue this to define 7 bit patterns in terms of 6 and 5 bit patterns 6 bit patterns in terms of 5 and 4 bit patterns and so on to conclude so far since S plus L the class S is number of patterns with 7 bits please note that all 7 bit patterns plus short bit attended is S and therefore all 7 bit patterns is the number of patterns in classes so the number of patterns in classes is number of patterns with 7 bits number of patterns in class L is number of patterns with 6 bits the 8 bit patterns number is nothing but equal to the number of patterns in S and number of patterns in L and therefore number of 7 bit patterns plus number of 6 bit patterns. This is in fact the phenomenal recurrence relation as you and I know it has been determined by Pingala's method and this can be continued till you reach a level where you are talking about 1 bit patterns and 2 bit patterns. Algebraically Hemchandra says that the number of patterns with n bits can be represented as h8 equal to h7 plus h6 where h7 is 7 bit bit patterns and h6 is pattern with 6 bits. In general hn is equal to hn minus 1 plus hn minus 2. All of us are familiar with this recurrence relation. I will come back to you later. But does this help us to compute h8? In order to compute h8 we must know h7 and h6. In order to compute h7 we must know h6 and h5. In order to compute h6 we must know h5 and h4 etc. How can we reconcile to this situation and put the computations in some form where we can use the powerful instructions of iteration, conditional etc. And we have learned. So here is one way. The idea of the algorithm is I start Ulta. I don't come down from 8 bits, 7 bits, 6 bits. But I say let me see 1. So how many patterns are there with just 1 bit? Number of patterns with 1 bit is only 1 because only short bit can come there. Remember long bit occupies 2 slots. How many patterns are there in h2? That number with 2 bits. There are 2. Why? I can have either 2 short bits or 1 long bit. So there are 2 possibilities. I continue the same way h3. According to my recursion formula h3 is equal to h2 plus h1. That means the number of patterns possible with 3 bits has to be exactly 2 plus 1 which is 3. I can indeed verify that. Consider quite independently how many 3 bit patterns you can have. You can have 3 bits of SSS. You can have 3 bits of LS or you can have 3 bits of SNL. And there is no other possibility. So this indeed is correct. Continuing this way h4 is computed as h3 plus h2 which is 5. h5 is computed as 5 plus 3 8 and so on and so forth when h8 is 34. What is interesting is to see the sequence of numbers which emerges. 1, 2, 3, 5, 8, 13, 21, 34. First we discuss the technical information program to calculate the number of bits. So I take n. I want to calculate hn. What I do is I set initial value of one term called h previous. Initial value for another term called h current. So these two represent the two past as h previous and h current 1 and 2. And I want to calculate h next which I have defined as integer. Now please note that h next will always be given as h current plus h previous. So I can set up an iteration. Observe the way the iteration is set up. For int i equal to 3, i less than equal to n i plus plus. There is a small squiggle and at the cost of some digression I would like to mention this. Ordinarily I would have written this for loop as i equal to 3 i less than equal to n i plus plus. When I say int i equal to 3 what I am doing is I am not only initializing i to 3 but I am also defining i as an integer variable. Why am I defining here? Could I not have defined here? Of course I could have defined it here. The difference which we shall emphasize at some point it could be stated here or at a later time. I personally prefer to describe the scoping of variable names at a later time. Effectively what we have to tell our students is that if I define i here the value of i will be known or will be usable throughout my program. However if I define a variable inside an iteration like this or for that matter inside any block then the existence of that variable logically is limited only to that block. That means whatever the value of i here that value is not observable or manipulatable outside this unless of course I have declared another i. Indeed I can have two variables called i. One declared here, one declared here could have values here but the moment another declaration comes inside that block the value of i is determined by what happens inside. Anyway that is a slight digression. Coming back to this iteration all that I am doing is I am calculating the sum of h previous plus h current to calculate h next. Observe that for i equal to 3 h next will give me h 3. But in the next iteration I want to calculate h 4 for which I need to push h next to h current and h current to h previous. This is what we call preparation for next iteration and this is what we are doing. We take the current value of h and push it to h previous and we take the h next value and push it to h current. So we are actually having a moving window. At the starting point I had h previous and h current. Using this I found out next. Now I push the h next to h current and h current to h previous and go over to the next value of r. When I execute this iteration n times the then value of h next will represent h of n or the Hemchandra number of the nth odd. I output that value. This is incidentally a very very interesting example of solving a recurrence relation by putting it into an iterative form in this fashion. In the earlier examples that we saw we were we had identified a block which could be repeatedly executed and we were having something new every time that block was executed. Either a new value was read from input which was being compared with the current maximum or we were using the iterative count i to multiply it with some product so that we could find factorial. Here we have two sort of values which both of which change in every iteration leading to this kind of result. Here are comments which professor Ranade had made and I entirely agree with this. This is mathematics from poetry. The series indeed is very interesting. It represents the number of petals in many flowers. It represents the ratio of consecutive terms which tends to a limit. All of you would recognize this is called what golden ratio and these numbers are commonly known as Fibonacci numbers. So what we we learnt in our younger days as Fibonacci series was actually is in fact called Hemchandra Gopala series and you can again you can refer to either Wikipedia or n number of other literature where it is conclusively conclusively shown that Fibonacci himself learnt possibly about these things when he was learning maths from some Arab scholars who had learnt it from India. Clearly Hemchandra lived before Fibonacci so the origin of the series which we now call Fibonacci series has come from our land where 1000 of you are attending to this course. It is a matter to be proud of but as far as we are concerned in this course the objective of this discussion was to show that how well known mathematical problems involving recurrence relations could be implemented using iteration. I will briefly go back to the previous slide which indicates that this kind of iteration with two terms successively being pushed back and back and a new value computed will eventually give me the correct calculation for the recurrence relation and I can calculate h n for any n. h n does not increase as rapidly as factorial increases but it also increases fast. Consequently if I want to find out h n for some very large value of n I may land up into a problem of not having the ability to store the final result which comes up after successive summations. This is the problem that we will keep reminding ourselves about and there will be some exercise that we will put where you will actually work out things on the errors both errors of rounding off for floating point numbers and errors of having numbers going way past the capability of or the limit of representation for integer numbers. Last we will discuss the Newton-Raphson method. This is the method which is used to calculate roots of a function any function f of x a general function. I want to briefly discuss this what is the root? The root is nothing but a value of x such that f of x is 0. The Newton-Raphson method there are multiple ways of finding out the roots for example. Incidentally roots of a quadratic a x square plus b x plus c which is a problem so often dealt with in the introductory programming courses but it is computed differently because there are explicit formulae to calculate the roots of a x square plus b x plus c. However if you have a general function it is extremely difficult to calculate roots in such arbitrary manner because there may not be an exact analytical solution. So, we have a numerical solution and what is the numerical solution? Well the numerical solution depends upon our ability to find out f of x and also f dash of x that is the derivative of. So, if both these functions are known and defined and if an initial good guess is available then it is possible to find the root of f of x using a numerical method. We will illustrate the Newton-Raphson method by choosing a function arbitrarily as the square root of some value k. So, for example, we define the function f of x as x square minus k. Notice that x square minus k equal to 0 means x square is equal to k and x is equal to under root of k that is how we will find out the square root of k. So, we have defined this function f of x accordingly. Having defined this function we know that its derivative f dash x is simply 2 x. So, for once we have a well defined function f of x and a well defined derivative function f dash of x. Further we observe that f x and f dash x can be calculated very easily that is if I give a value of x say 5.3 I can calculate 5.3 square minus k and I can also calculate 2 into 5.3. So, calculations is very trivial and simple, but that is not the point. The point is while I can do 2 or 3 arithmetic operations to calculate this what I want to find out is the root of this equation and it is not very clear to me how will I find a root. The root is a point on x axis where the function crosses x axis assuming that the function has at least one real root and it is always true for this function by the way. For any constant k where the equation x square equal to k always has a real value. In fact, there is exactly one root. So, how can you calculate that? The Newton Raphson method says that first start with some initial guess. The initial guess could be arbitrary because the real line stretches from minus infinity to plus infinity. It is well known in the case of this function that the initial guess of x 0 equal to 1 always works. This has in fact been proven. We do not go into the proofs and all, but we now illustrate the Newton Raphson method to some details. So, the question is going back here if I have the function f x defined if I have f dash x defined and if I have an initial guess x 0 equal to 1. I want to set up an iterative method by which I can constantly improve the initial guess value and bring it closer to the real root. This is the starting point where we denote it by x i. Our idea is that if this is the function this is an arbitrary starting point. I would like this starting point or initial guess to come as close as possible to this function because this is where the root is. So, in short the point a is known point b which is here can be calculated easily because that is nothing but the value of the function at f x. Now, I want to get a better estimate of the root. So, I approximate the function by tangent at the point b. In short this is my representational function at this point. What is tangent? Tangent can be easily calculated because I know f dash x as well and wherever this tangent intercepts x x s I will treat this to be a point closer to my root which is obviously so. So, original estimate was this. I would like to get this estimate and at this point I will again like to do that again like to do that etcetera. You can very clearly see that such iterations will eventually lead me to the desired result. This is a formulation. For example, I approximate f by the tangent c which is intercept on x axis. The point c let us say the point c has coordinates x i plus 1 comma 0 because this is on x axis. My ambition is to calculate x i plus 1 given x i given the value of f x and given the value of f dash x. What is f dash x i which is the derivative is nothing but the slope a b by a c because that defines the slope here which is f of x i divided by x i minus x i plus 1. Consequently, we can calculate x i plus 1 as equal to x i minus f of x i divided by f dash x i. Observe that what we have done is we had a well defined function and a well defined derivative of that function. We took an initial guess and from that we improved that guess to calculate x i plus 1. Since x i plus 1 is also a guess and since the value of the function is not 0 at this point I have not yet found the root. But what I can do? I can do exactly what I did with x i with x i plus 1 and this is what renders the problem completely amenable to an iterative solution. I can keep finding out a new x i plus 1 from a given x i the x i plus 2 x i plus 3 and so on and iteratively I can approach the root in the diagram the root is here. Of course, it should be very obvious to you that initially my movement towards the root may be very rapid afterwards I will move very slowly but eventually I will always reach the root. This is generally known as the Newton Raphson method. Why I am introducing it so elaborately is because you will find that your first year students would not have heard of Newton Raphson method at least not all of them. So it is useful to introduce Newton Raphson method in a computational context like this and later on teach your students to write a computer program to implement these iterations. Thank you very much.