 Welcome back. In the last segment, we discussed recursive function to calculate virahanka numbers. And we concluded that it was taking too much time and that was because we were seeming to repeat work. In this segment, we are going to see how to avoid that. So what exactly were we doing? And what should we do? Well, we should really not calculate something more than once. So to begin with, our function knows v of 1 is 1 and v of 2 is 2. So our function was v of 1 and v of 2, it then calculates v of 3 and v of 3 is calculated using v of 1 plus v of 2. So after that, you can calculate v of 4 is v of 3 plus v of 2. But notice that when we issue this call v of 4, we do not really need to recalculate v of 3. If we could somehow remember that we calculated v of 3 earlier, then we could just use that value. Of course we know. So in general, we could think of doing this in a loop. So in iteration i, where i goes from 3 to d, we will calculate v of i because for i equals 1 and 2, there is nothing to be done. Those values are already known. And how do we calculate v of i? Well, v of i requires us to take v of i minus 1, which we must have calculated in the previous iteration and v of i minus 2, which we must have calculated in the iteration previous to that and add up these two values. So which means these values, we could print out if we wish, but we do not really need to print them out if we are only interested in the final v of d. But in any case, in the i-th iteration, we should be remembering these values. These values should be there so that we can add them up and we can generate v of i. So to keep these values, v of i minus 1 and v of i minus 2, we will have two variables. So we will have a loop. We are now going to have a loop and in that loop, we are going to have two variables and at the beginning of the i-th iteration, these variables will contain v of i minus 1 and v of i minus 2. And we will also use a variable v of i in which we will construct v of i. So v i is a variable, v i minus 1, v i minus 2 are going to be our variables and the important point is that at the beginning of iteration i, we want v i minus 1 and v i minus 2 to contain v of i minus 1 and v of i minus 2, which were calculated earlier. So based on this, we can create this program. So this program is going to calculate v of d. So as we said, we are going to have iterations going from 3 to d and at the beginning, v of i minus 1 and v of i minus 2 must have the values v of i minus 1, v bracket i minus 1 and v bracket i minus 2. So those values must be there and then we add them up to get the value of v i. Now I have to fill up the rest of the code over here. So let us see, so in iteration 3, when we first enter this loop, what do we need? So we need, so we are going to calculate v of 3, i is going to be 3, so we want to calculate v of 3 over here. At that point, we need this to be v of 2 and we need this to be v 1. So these values will get set only above outside the loop, so therefore we should initialize these variables to 2 and 1 respectively. And then when we go to the next iteration of the loop, what happens? So first of all in this iteration, what happens? And we beginning, we calculate this, which is as per the plan we were talking about on the last slide and then we have to prepare for the next iteration. So for the next iteration, what is it that we want? Well, the value that was v i minus 1 in the current iteration, so is the value of calculated in the previous iteration. But now for the next iteration, that will be the value calculated in the second previous iteration. So that should really go to v i minus 2. And v i minus 1 for the next iteration is the value that was calculated in the current iteration and therefore that must become v i. So our loop must be this. And finally what should we return? So at the end of this loop, v of i will contain v d and therefore we should return v of i over here. Now if you look at this code, you will see that this code works only for d greater than 2. Basically if d is equal to 1, what happens? This v i is not even defined. So but for other d it works fine and so let us just put a comment to that effect. So let us see, let us run that program and let us see what happens with it. So this is the program. So again the function is what I just showed you and in the main program I am going to have the same thing again. I am going to go from 2 to 50 as before. I did not go from, go with 1 because we know that this program does not work for 1. And as you can see again to accommodate the large numbers I have had long ends. d does not have to be long because d does not contain long numbers. So the arguments to v do not contain long numbers. So that does not have to be long. So let us compile this. So this time it produced an answer instantaneously essentially. So as soon as I hit return the answer was there. There was no waiting at all and that is to be expected because in each iteration of the loop we are doing really very little work. So there is this repeat and in this, oh yeah so there is a slight change over here we did not really need those i's and therefore we can also use a repeat over here. So you can compare these two codes and see that they are really the same. But I could have put a for here as well, does not really matter. But the point is that we are having iterations of this loop and the time required for each value, for each vi is exactly the same and therefore the loop runs really fast. So we have done this and we were able to get v of d to be calculated very fast. So what do we learn from this? So we can see that recursion is a very powerful tool for solving problems. Recursion helps us solve problems and discover algorithms but the natural recursive algorithm might be very slow. By examining the algorithm, by seeing what it does we might be able to discover a better algorithm. Then we also saw that recursion trees are a nice way of seeing how a recursive algorithm works and I will leave you with an exercise, write the program which draws out the recursion tree. So for this you need to take the ideas from the basic recursive Virahanka algorithm and also the tree drawing algorithm and you sort of have to fuse these two programs together. Now this sequence, these Virahanka numbers v of 1, v of 2, v of 3 which are 1, 2, 3, 5, 8, 13 is actually a famous sequence and it is well known as the Fibonacci sequence named after the Italian mathematician Fibonacci who lived, Fibonacci or Fibonacci who lived in 12th century AD but Virahanka discovered it much earlier and we probably should be calling it the Virahanka sequence or call these numbers the Virahanka numbers is not it. So with that we will conclude this lecture sequence noting some rather interesting facts about recursion. Thank you.