1 00:00:02,440 --> 00:00:03,220 Hi, everyone. 2 00:00:03,460 --> 00:00:05,920 So in this lecture, we will start education. 3 00:00:06,100 --> 00:00:09,180 OK, so recursion is one of my favorite topic. 4 00:00:09,190 --> 00:00:13,570 The best part of recursion is we can easily code the most complicated problems. 5 00:00:14,080 --> 00:00:20,300 OK, recursion is very important because going forward in many the structure, we use recursion only. 6 00:00:20,880 --> 00:00:25,720 OK, so we have to do recursion properly, otherwise we may face a lot of problems. 7 00:00:25,960 --> 00:00:28,000 OK, so what is recursion? 8 00:00:28,000 --> 00:00:29,440 What is the definition of recursion? 9 00:00:29,770 --> 00:00:33,430 So recursion is when a function is calling itself. 10 00:00:33,460 --> 00:00:39,130 OK, the definition of recursion is when a function is calling itself so function calling itself. 11 00:00:39,430 --> 00:00:40,840 OK, so this is a recursion. 12 00:00:41,180 --> 00:00:49,060 OK, so now what we have seen I have a function mean man is calling a function a function E is calling 13 00:00:49,060 --> 00:00:50,950 another function B and so on. 14 00:00:51,930 --> 00:00:58,210 OK, but here in recursion what I am trying to say here is a function will call itself, that is we 15 00:00:58,210 --> 00:01:06,220 have a function mean main function is calling a function E function E is again calling function and 16 00:01:06,220 --> 00:01:06,580 so on. 17 00:01:07,050 --> 00:01:12,190 OK, so it is a little weird when function E is calling function itself. 18 00:01:12,220 --> 00:01:15,640 OK, now let's try to think in a different manner. 19 00:01:15,970 --> 00:01:17,890 OK, then we will use recursion. 20 00:01:18,190 --> 00:01:25,210 So we will use recursion, we will use recursion whenever the solution of a problem depends upon the 21 00:01:25,210 --> 00:01:26,800 solution of a smaller problem. 22 00:01:27,220 --> 00:01:28,670 OK, let's take an example. 23 00:01:29,200 --> 00:01:32,290 So suppose I want to find an factorial. 24 00:01:33,160 --> 00:01:35,910 OK, we want to find out what is and factorial. 25 00:01:36,400 --> 00:01:44,620 So what we will do can we write like an factorial is this is N and minus one and minus two and so on 26 00:01:44,620 --> 00:01:45,090 Telvin. 27 00:01:45,370 --> 00:01:48,020 OK, so can I write this line. 28 00:01:48,400 --> 00:01:50,150 Can I write this part in another way. 29 00:01:50,170 --> 00:01:52,420 Can I write it like an factorial. 30 00:01:52,450 --> 00:01:56,230 Is np multiply and minus one factorial. 31 00:01:57,590 --> 00:01:59,370 OK, we can write it like this way. 32 00:01:59,690 --> 00:02:04,410 Now, suppose I have a function factorial, OK, so I have a factorial function. 33 00:02:04,910 --> 00:02:07,750 I will give it an and it will give me an factorial. 34 00:02:07,790 --> 00:02:13,700 OK, so often it is same as can I write like this and multiply. 35 00:02:14,880 --> 00:02:17,190 Factorial function and minus one. 36 00:02:18,060 --> 00:02:22,530 OK, so this is the function name, fact is the function name. 37 00:02:23,400 --> 00:02:30,660 OK, so this function takes Digitas argument and it will return me it will give me an factorial. 38 00:02:31,080 --> 00:02:36,270 Factorial often is nothing but and multiply factorial off and minus one. 39 00:02:36,890 --> 00:02:40,920 OK, so here we can see a function. 40 00:02:41,070 --> 00:02:46,080 The fact function is calling the fact function but with a smaller input. 41 00:02:46,620 --> 00:02:54,090 OK, so when we will use recursion, when a problem depends upon the problem of the same nature, but 42 00:02:54,090 --> 00:02:56,880 with a smaller size, I am repeating myself. 43 00:02:57,150 --> 00:03:00,090 We will use recursion when a problem. 44 00:03:02,240 --> 00:03:08,510 Depends on a problem of the same nature, but with a smaller input size. 45 00:03:09,770 --> 00:03:11,240 OK, so here we can see. 46 00:03:13,890 --> 00:03:16,140 I have a problem I want to find factotum. 47 00:03:16,200 --> 00:03:23,880 It depends upon the it depends upon a small problem of the same nature naturism, but with a smaller 48 00:03:23,880 --> 00:03:24,510 input site. 49 00:03:25,600 --> 00:03:30,700 OK, now what we are trying to do here is so we are trying to divide the problem. 50 00:03:30,710 --> 00:03:33,940 So I have a big problem, I have a big problem. 51 00:03:34,240 --> 00:03:37,390 I will divide I reconvert the problem into smaller problem. 52 00:03:38,050 --> 00:03:41,680 I will again convert it into a smaller problem. 53 00:03:41,950 --> 00:03:45,700 I will again convert into a smaller problem and it will go on. 54 00:03:45,980 --> 00:03:49,300 OK, so finally our problem will become very, very small. 55 00:03:50,340 --> 00:03:53,440 OK, so we are reducing the size of our problem. 56 00:03:53,460 --> 00:03:55,620 OK, our problem size is being reduced. 57 00:03:56,280 --> 00:04:01,620 OK, we can see here our problem now our problem becomes smaller. 58 00:04:01,680 --> 00:04:04,190 OK, so the question what the commission will do. 59 00:04:04,350 --> 00:04:05,480 I have a big problem. 60 00:04:05,490 --> 00:04:08,030 I will convert the big problem into smaller problem. 61 00:04:08,040 --> 00:04:11,220 I will again convert the smaller problem into smaller problem. 62 00:04:11,460 --> 00:04:15,720 Again, the smaller problem will be converted into smaller problem and so on. 63 00:04:16,600 --> 00:04:18,970 OK, now let's try to see it in gold. 64 00:04:20,839 --> 00:04:26,360 We will try to solve the same problem, so let us take the value of an input from the user. 65 00:04:28,340 --> 00:04:33,540 OK, so let's say I have a function fact, so this function effectively done me the answer. 66 00:04:34,010 --> 00:04:35,980 So I have this fact function. 67 00:04:36,020 --> 00:04:41,200 I will give it an factorial function and it will be done with the answer and let us try to bring to 68 00:04:41,210 --> 00:04:42,380 the end factorial, OK? 69 00:04:44,280 --> 00:04:49,160 Now, what we have discussed, so what we return type of the function, it will be integer, then the 70 00:04:49,170 --> 00:04:53,960 function is fact and it will take integer as argument, whose total we have to calculate. 71 00:04:54,620 --> 00:04:55,920 OK, so. 72 00:04:57,600 --> 00:05:02,520 What is the meaning of the occasion, what we will do, we will find out, we will convert the big problem 73 00:05:02,700 --> 00:05:03,920 into smaller problem. 74 00:05:04,170 --> 00:05:06,570 So in small answer. 75 00:05:09,070 --> 00:05:13,060 I'm calling the function Vectorial and I will give and minus one. 76 00:05:13,480 --> 00:05:18,020 OK, so now at number five I know the factory love and minus one. 77 00:05:18,370 --> 00:05:19,360 So what is the factory? 78 00:05:19,470 --> 00:05:20,470 Often it will be. 79 00:05:21,570 --> 00:05:25,260 What I will do, I just have to return and multiply, Smolan said. 80 00:05:26,490 --> 00:05:29,190 OK, so what I'm trying to do here is. 81 00:05:31,060 --> 00:05:37,150 I want to calculate the factorial often, so headline number five, what I'm doing, I'm calculating 82 00:05:37,150 --> 00:05:38,680 the factorial off and minus one. 83 00:05:38,710 --> 00:05:40,420 OK, what does fact function will do? 84 00:05:40,720 --> 00:05:46,070 This fact function takes an integer and it returns an integer, which is the fact of the number. 85 00:05:46,540 --> 00:05:54,640 So this fact will give me and minus one factory, OK, and then minus one factorial is stored in a small 86 00:05:54,640 --> 00:05:58,350 answer and finally I return and multiply and minus one factory. 87 00:05:58,430 --> 00:06:02,290 OK, now let's try to add another coda and let's see what will happen. 88 00:06:02,440 --> 00:06:02,800 OK. 89 00:06:07,340 --> 00:06:09,510 Now it is waiting for us to give input. 90 00:06:09,530 --> 00:06:11,410 Let's I want to calculate for Victoria. 91 00:06:11,450 --> 00:06:12,960 OK, so Ford and. 92 00:06:15,860 --> 00:06:21,380 So what will happen, what is happening here is basically we are getting segmentation fault, infinite 93 00:06:21,380 --> 00:06:21,640 loop. 94 00:06:21,650 --> 00:06:24,860 OK, so this is finite loop. 95 00:06:24,860 --> 00:06:26,430 We are getting segmentation fault. 96 00:06:26,600 --> 00:06:28,820 Now, let us try to it our code. 97 00:06:28,850 --> 00:06:29,660 What is happening? 98 00:06:30,080 --> 00:06:32,630 OK, let's try to try it another good. 99 00:06:33,500 --> 00:06:33,830 OK. 100 00:06:33,860 --> 00:06:38,120 So what is happening here is so I have this function mean. 101 00:06:39,130 --> 00:06:45,640 I have this function Manitas calling the function Vectorial of an OK, so Meedan is calling. 102 00:06:46,910 --> 00:06:50,220 Fact and let's say the value is for Anisfield. 103 00:06:50,240 --> 00:06:57,040 OK, so Anisfield now what is happening here is this federal function is again calling the federal function. 104 00:06:57,050 --> 00:07:01,520 So it is calling a federal function, but the value of industry. 105 00:07:01,760 --> 00:07:08,600 So the value of these three, we can understand it this in a different way, but actually it doesn't 106 00:07:08,600 --> 00:07:08,900 happen. 107 00:07:08,930 --> 00:07:12,340 OK, so let's try to understand it in a different manner. 108 00:07:12,770 --> 00:07:14,700 So I have mine now. 109 00:07:14,750 --> 00:07:17,390 Men is calling a function vectorial. 110 00:07:17,840 --> 00:07:18,260 OK. 111 00:07:19,330 --> 00:07:21,430 And the value of Anisfield. 112 00:07:22,410 --> 00:07:28,710 OK, now add this line, what is happening here is let's suppose we have another instance of de facto 113 00:07:28,710 --> 00:07:29,190 function. 114 00:07:29,580 --> 00:07:35,970 OK, we have another instance of de facto function and this is calling de facto, but the value of industry 115 00:07:36,420 --> 00:07:38,040 you can see here and minus one. 116 00:07:38,670 --> 00:07:41,640 OK, so this new instance is created. 117 00:07:41,650 --> 00:07:44,020 You can assume that, but it doesn't happen, OK? 118 00:07:44,370 --> 00:07:47,790 So now what is happening here is so what will happen? 119 00:07:47,790 --> 00:07:50,220 This fact function, it will call. 120 00:07:51,490 --> 00:07:52,260 Fact to. 121 00:07:53,320 --> 00:07:55,520 Again, the value of an asset to here. 122 00:07:55,930 --> 00:08:01,660 Now, what I want to say here is we know the local variables of one function is different from the local 123 00:08:01,660 --> 00:08:03,190 variables of another function. 124 00:08:03,700 --> 00:08:09,400 OK, so this and here the value of this forward and here the value of industry. 125 00:08:09,910 --> 00:08:12,850 So this end and this end, they both are different. 126 00:08:13,170 --> 00:08:14,630 OK, they are different. 127 00:08:14,980 --> 00:08:20,080 OK, very different because we know the local variables of one function is different than the local 128 00:08:20,080 --> 00:08:21,460 variables of another function. 129 00:08:21,790 --> 00:08:23,260 These are two different functions. 130 00:08:23,290 --> 00:08:24,370 OK, two instances. 131 00:08:24,820 --> 00:08:27,230 So this and this and they both are different. 132 00:08:27,370 --> 00:08:28,560 Now what will happen? 133 00:08:28,630 --> 00:08:32,809 This will call fact when now the value of and becomes one. 134 00:08:33,130 --> 00:08:37,720 So what is happening here is as soon as we enter this function, we are calling another function. 135 00:08:37,780 --> 00:08:41,919 OK, so now this one it will call fact zero. 136 00:08:42,100 --> 00:08:46,990 So the value of any zero then it will call fact minus one. 137 00:08:47,260 --> 00:08:50,590 It will call effect minus two and so on. 138 00:08:51,090 --> 00:08:54,610 OK, so basically it is we are doing infinite calls. 139 00:08:54,640 --> 00:08:56,940 OK, we are doing infinite calls. 140 00:08:57,160 --> 00:08:58,630 So this is infinite loop. 141 00:08:58,870 --> 00:09:00,070 This is infinite loop. 142 00:09:00,980 --> 00:09:03,220 So we are getting segmentation fault, OK. 143 00:09:03,250 --> 00:09:05,310 So while we are getting segmentation fault. 144 00:09:05,410 --> 00:09:10,600 So what is happening here is when I reach this line, all these four functions. 145 00:09:11,870 --> 00:09:17,150 When I am here, all these four functions are waiting for the factorial one function to complete its 146 00:09:17,150 --> 00:09:17,480 work. 147 00:09:17,680 --> 00:09:22,610 OK, all are waiting, all have the copies off and now it is taking some memory. 148 00:09:22,700 --> 00:09:24,470 OK, this is taking some memory. 149 00:09:24,500 --> 00:09:25,500 This is taking some memory. 150 00:09:25,500 --> 00:09:26,490 It is taking some memory. 151 00:09:26,490 --> 00:09:27,560 It is taking some memory. 152 00:09:27,830 --> 00:09:29,480 OK, so what will happen? 153 00:09:29,480 --> 00:09:31,630 We are we are doing infinite calls. 154 00:09:31,790 --> 00:09:37,610 So at some time it will not be able, the program will not be able to create more memory and we will 155 00:09:37,610 --> 00:09:38,810 get segmentation fault. 156 00:09:39,140 --> 00:09:42,990 OK, so when it is fact minus two, what will happen? 157 00:09:43,040 --> 00:09:49,010 All these functions are waiting for the miners to function to compute its work. 158 00:09:49,800 --> 00:09:52,550 OK, so everything will take memory. 159 00:09:53,120 --> 00:09:56,780 They are taking small, small memories, but we are doing infinite calls. 160 00:09:56,930 --> 00:10:00,910 So at some time what will happen, we will get out of memory, OK? 161 00:10:00,920 --> 00:10:05,420 We will not be able to create more memory and we will get segmentation fault due to infinite calls. 162 00:10:06,680 --> 00:10:14,660 OK, so why there are infinite calls because we started at an equals four, then three to one zero. 163 00:10:14,960 --> 00:10:16,670 So ideally what should happen? 164 00:10:16,670 --> 00:10:19,700 We should stop at zero or we should stop at one. 165 00:10:20,090 --> 00:10:22,730 OK, but here we are making infinite calls. 166 00:10:22,760 --> 00:10:27,590 OK, ideally we should stop at zero one, but we are making here in finite calls. 167 00:10:27,800 --> 00:10:29,000 I can prove it also. 168 00:10:29,360 --> 00:10:30,470 So how will I prove it. 169 00:10:30,470 --> 00:10:31,160 But I will do. 170 00:10:32,590 --> 00:10:39,400 As soon as I enter this function, I will print the value of an OK, I will print the value of and now 171 00:10:39,400 --> 00:10:40,690 let's try to run this file. 172 00:10:42,200 --> 00:10:46,400 Now, let's hear the value of Anisfield and as soon as we will bring tenter. 173 00:10:47,570 --> 00:10:50,610 So you can see here, these are the values of an OK. 174 00:10:50,870 --> 00:10:55,280 So these are all the values of these men number, of course, I am trying to make here. 175 00:10:55,840 --> 00:10:59,520 OK, so we are making many calls, but we could not get our answer. 176 00:10:59,750 --> 00:11:03,500 Finally, we will get the segmentation fault due to the lack of the memory. 177 00:11:04,130 --> 00:11:06,800 OK, so we are making many, many calls. 178 00:11:08,460 --> 00:11:09,960 I don't know where it will stop. 179 00:11:10,970 --> 00:11:16,940 OK, it will stop when we get out of memory and we will get segmentation fault due to lack of memory. 180 00:11:17,420 --> 00:11:21,570 OK, it is making these main number of calls and it is trying to find the answer. 181 00:11:21,570 --> 00:11:23,850 It is trying to find the Ford factory. 182 00:11:23,870 --> 00:11:29,270 OK, so at this moment, what is happening here is we have due to lack of memory, we are getting the 183 00:11:29,270 --> 00:11:30,260 segmentation fault. 184 00:11:30,890 --> 00:11:32,990 OK, now let's call it up. 185 00:11:34,540 --> 00:11:38,320 I'm not able to call it more, so what I've done here is. 186 00:11:39,700 --> 00:11:41,900 So this is the same gold you can see here. 187 00:11:42,370 --> 00:11:49,900 This is the same gold I've written this gold on radion and now let's say the inputs for and we will 188 00:11:49,900 --> 00:11:52,540 try to in the file will return the gold. 189 00:11:52,610 --> 00:11:52,960 OK. 190 00:11:54,280 --> 00:11:59,540 So you can see the output we are getting around Tamarrod, right, and Tamira because of infinite loop. 191 00:11:59,560 --> 00:12:05,860 OK, now you can see here, this is the output, four, three, two, one zero and then minus one, 192 00:12:05,860 --> 00:12:07,540 minus two, minus three and so on. 193 00:12:07,720 --> 00:12:11,470 OK, you can scroll to the bottom and you can see it yourself. 194 00:12:12,970 --> 00:12:19,810 OK, so four, three, two, one, and then zero minus and minus two, so ideally, what should happen? 195 00:12:19,810 --> 00:12:23,250 Ideally, our program should stop at some moment, OK? 196 00:12:23,710 --> 00:12:27,280 Our program should stop at some moment now where it should stop. 197 00:12:30,440 --> 00:12:39,200 OK, so what is happening here is Vectorial for function, this is calling Vectorial three, it is calling 198 00:12:39,200 --> 00:12:43,070 Vectorial two, it is calling factorial one. 199 00:12:43,640 --> 00:12:46,010 It is calling, let's say, factorial zero. 200 00:12:46,490 --> 00:12:49,700 OK, now it is calling factorial minus one. 201 00:12:51,250 --> 00:12:55,290 Now, according to me, there is no need of this call, why? 202 00:12:55,600 --> 00:12:57,970 Because this is very trivial problem. 203 00:12:58,000 --> 00:12:59,470 OK, what is zero factorial? 204 00:12:59,560 --> 00:13:00,670 Zero factor Lisburn? 205 00:13:00,940 --> 00:13:03,220 So I already know the answer of zero factorial. 206 00:13:03,220 --> 00:13:05,500 So why to make the call to minus one. 207 00:13:05,740 --> 00:13:07,630 OK, so what do factorial. 208 00:13:07,630 --> 00:13:11,140 Zero Willerton one because zero factorial is one. 209 00:13:11,440 --> 00:13:14,280 OK, so our recursion chain will stop here. 210 00:13:14,890 --> 00:13:16,660 We do not need to go any further. 211 00:13:16,830 --> 00:13:23,620 OK, so factorial zero will give this output will give its output to factorial one. 212 00:13:23,820 --> 00:13:26,100 OK, so we are returning one here. 213 00:13:26,800 --> 00:13:27,160 OK. 214 00:13:27,180 --> 00:13:31,870 Similarly factorial one will do its work and it will return the factorial of one and so on. 215 00:13:32,050 --> 00:13:38,770 OK, so in recursion we call the same function with the smaller input, says OK, we are calling the 216 00:13:38,770 --> 00:13:44,380 same function with the smaller inputs right now, with the help of the output of the smaller problem, 217 00:13:44,770 --> 00:13:46,690 we have to calculate our output. 218 00:13:47,230 --> 00:13:53,320 OK, so with the help of I'm repeating myself with the help of the output of a smaller problem, we 219 00:13:53,320 --> 00:13:56,530 have to calculate our output, let's say the output of three factorial. 220 00:13:56,560 --> 00:13:58,000 So this is bigger problem. 221 00:13:58,000 --> 00:14:03,370 And the smaller problem, let's say the output of factorial three is X. 222 00:14:03,970 --> 00:14:05,740 OK, so what factorial four will do? 223 00:14:06,010 --> 00:14:11,620 It will multiply four with X and then it will return the output to the mean. 224 00:14:13,080 --> 00:14:19,510 OK, so with the help of the output of the smaller problem, the function, we will calculate our bigger 225 00:14:19,510 --> 00:14:22,000 problem and then we will return our output to the mean. 226 00:14:22,880 --> 00:14:28,390 OK, so to stop the recursion chain, but we have to do we have to stop our recursion, Jane, here, 227 00:14:28,390 --> 00:14:30,790 because zero factorial is a very trivial problem. 228 00:14:30,790 --> 00:14:31,900 We already know the answer. 229 00:14:31,930 --> 00:14:35,110 So why do y to make call to minus one. 230 00:14:35,350 --> 00:14:37,450 OK, now let's see. 231 00:14:38,950 --> 00:14:41,380 So we have to call, we have to stop. 232 00:14:46,250 --> 00:14:49,550 We have to stop the engine now to stop the recognition. 233 00:14:50,150 --> 00:14:55,040 I know if the value of any zero I already know the answer is zero factorial is one. 234 00:14:55,040 --> 00:14:57,290 So I will attend one here, OK? 235 00:14:57,860 --> 00:14:58,820 Now this. 236 00:14:58,820 --> 00:14:59,150 We have it. 237 00:14:59,150 --> 00:14:59,400 It goes. 238 00:14:59,410 --> 00:15:00,320 It will stop. 239 00:15:00,800 --> 00:15:02,510 OK, let's end this file. 240 00:15:05,400 --> 00:15:07,140 So let's say the this for. 241 00:15:09,110 --> 00:15:14,780 OK, so what is happening here is the value of fairness for then it becomes then it becomes to, then 242 00:15:14,780 --> 00:15:16,910 it becomes one, then it becomes zero. 243 00:15:17,330 --> 00:15:23,780 Now, let's see here we are not making equal to minus one, OK, either our function of the James Dobson 244 00:15:23,810 --> 00:15:26,450 zero, OK, we didn't call on minus one. 245 00:15:26,450 --> 00:15:27,410 We stopped at zero. 246 00:15:27,620 --> 00:15:29,800 And this is our answer, 24. 247 00:15:30,020 --> 00:15:32,210 OK, so 24 is the fourth factorial. 248 00:15:32,630 --> 00:15:34,750 OK, so our output is right now. 249 00:15:34,760 --> 00:15:36,360 Let's try another record. 250 00:15:36,380 --> 00:15:37,310 How it is working. 251 00:15:37,610 --> 00:15:38,000 OK. 252 00:15:41,260 --> 00:15:43,030 So this is our main. 253 00:15:45,350 --> 00:15:48,080 OK, so Main is calling factorial for. 254 00:15:50,870 --> 00:15:57,890 OK, so the value of and is forward here and what is happening here is when mean calls for oil for function. 255 00:15:58,110 --> 00:16:02,900 So the main function, it is waiting for the oil for function to return its answer. 256 00:16:03,230 --> 00:16:08,660 OK, so this main function, we are waiting at line number 16. 257 00:16:08,980 --> 00:16:12,330 OK, I am also writing the line number at which the function is waiting. 258 00:16:12,530 --> 00:16:19,340 So this main function is waiting at line number 16 for the factorial function to do its work to complete 259 00:16:19,340 --> 00:16:19,820 its work. 260 00:16:20,570 --> 00:16:25,390 OK, so I will enter the factorial function and then we are again making the call. 261 00:16:25,400 --> 00:16:26,960 The value of N is not zero. 262 00:16:27,020 --> 00:16:28,210 OK, the final four. 263 00:16:28,670 --> 00:16:30,790 So I'm making the call to fact function. 264 00:16:31,520 --> 00:16:33,170 So this is calling. 265 00:16:35,450 --> 00:16:42,530 Factorial to function, the value of industry, and this is waiting at line number, if at for function 266 00:16:42,530 --> 00:16:43,780 is waiting at line number eight. 267 00:16:44,960 --> 00:16:48,370 Now, again, three is not close to zero. 268 00:16:48,710 --> 00:16:51,740 So it is it will call factorial function. 269 00:16:52,100 --> 00:16:55,730 And this is also waiting at line number eight, the value of an. 270 00:16:56,570 --> 00:16:58,910 Now, again, two is not zero. 271 00:16:59,330 --> 00:17:00,440 So it will call. 272 00:17:01,640 --> 00:17:08,119 Factor one function, the value of N is one, and it is waiting at line, number it again, it will 273 00:17:08,119 --> 00:17:12,560 be red number because one is not zero, so it will call for zero function. 274 00:17:12,980 --> 00:17:15,190 OK, the value of N is zero now. 275 00:17:15,800 --> 00:17:18,710 OK, so the value of any zero. 276 00:17:20,480 --> 00:17:24,230 OK, so this condition is true, what I'm doing here is I am returning one. 277 00:17:25,390 --> 00:17:26,810 So what what will happen? 278 00:17:27,099 --> 00:17:31,360 I am returning one, so this factor zero function, it will return one. 279 00:17:32,690 --> 00:17:39,200 It is returning when and we are headline number eight, so headline number eight, small answer, so 280 00:17:39,200 --> 00:17:40,250 the small answer. 281 00:17:41,320 --> 00:17:48,880 It is one here, OK, now and multiply SMOLAN So so what is the value of N one and it's my answer is 282 00:17:48,880 --> 00:17:49,120 one. 283 00:17:49,120 --> 00:17:52,350 So it will return when multiply one, which is two. 284 00:17:52,540 --> 00:17:57,180 So it is returning to and the two will be stored in this. 285 00:17:57,180 --> 00:18:02,920 Smolan So so small answer becomes two now factorial two will do its work. 286 00:18:03,310 --> 00:18:05,710 OK, so, so it will be one. 287 00:18:07,720 --> 00:18:14,860 I tell you another factor, do will do its work, so to multiply small answer, so small answer will 288 00:18:14,860 --> 00:18:16,310 now be comes to here. 289 00:18:16,380 --> 00:18:17,530 OK, now. 290 00:18:18,700 --> 00:18:26,470 Three, multiply two and multiply Mylanta, so three, multiply two, so it will return six to the all 291 00:18:26,470 --> 00:18:33,730 four function, so small answer becomes six here now and multiply Smolensky and it's for smaller answers. 292 00:18:33,740 --> 00:18:38,470 Six So it will return 16 to 24 to the mean. 293 00:18:38,890 --> 00:18:40,690 OK, so that line number 16. 294 00:18:42,110 --> 00:18:46,970 I'm returning to India for so answer contains 24 and I am Bentinck Grandiflora. 295 00:18:48,490 --> 00:18:55,300 OK, so now what happened here is when I reach the place, when I'm reaching this function, when I 296 00:18:55,300 --> 00:19:00,350 use this function, all these functions, they are out of memory, OK? 297 00:19:00,370 --> 00:19:02,110 They do not exist anymore. 298 00:19:02,260 --> 00:19:04,320 OK, so their wait is over. 299 00:19:04,780 --> 00:19:05,590 They are out of memory. 300 00:19:05,590 --> 00:19:06,730 So what is happening? 301 00:19:06,730 --> 00:19:12,610 It is as soon as this function is returning one, as soon as this function is that anyone now dysfunction 302 00:19:12,610 --> 00:19:13,420 does not exist. 303 00:19:13,720 --> 00:19:18,970 As soon as this function returns, its answer now dysfunction does not exist. 304 00:19:19,830 --> 00:19:27,540 OK, so as soon as the to function gets down from the factorial function, its weight is over. 305 00:19:27,790 --> 00:19:33,670 Similarly, as soon as the all three function gets the output of the factorial to function, its weight 306 00:19:33,670 --> 00:19:34,720 is over and so on. 307 00:19:35,380 --> 00:19:37,930 OK, so we call it Goldstrike. 308 00:19:38,150 --> 00:19:39,970 OK, this is Kostic. 309 00:19:39,970 --> 00:19:42,370 I think we have already discussed it in functions. 310 00:19:43,030 --> 00:19:43,960 So what is a call? 311 00:19:43,960 --> 00:19:44,380 Steck. 312 00:19:44,920 --> 00:19:47,130 So Koscheck is like a glass. 313 00:19:47,470 --> 00:19:50,090 So at the bottom we have the main function. 314 00:19:50,920 --> 00:19:53,170 OK, so we have the main function. 315 00:19:53,800 --> 00:20:01,450 Main function is calling the factorial for function factorial for function is calling factorial three 316 00:20:01,450 --> 00:20:08,500 function factorial, three function calls, factorial to function, factorial to function calls for 317 00:20:08,830 --> 00:20:15,940 one function and only one function is calling factorial zero function ok and factorial function is not 318 00:20:15,940 --> 00:20:16,750 calling anything. 319 00:20:17,050 --> 00:20:18,390 OK, so what is the cost. 320 00:20:18,850 --> 00:20:19,800 So what will happen. 321 00:20:19,810 --> 00:20:26,050 So Fatto Zero will do its work, it will complete its work and it will return the answer to de facto 322 00:20:26,050 --> 00:20:28,690 one function and then it will goes out of the memory. 323 00:20:29,070 --> 00:20:32,050 OK, it will goes out, it will go out effectively. 324 00:20:32,050 --> 00:20:35,290 Function will complete it work and it will return. 325 00:20:35,290 --> 00:20:40,450 The answer to the function then effectively won't function, will go out OK. 326 00:20:40,660 --> 00:20:43,770 And this way everything will go out ok. 327 00:20:44,290 --> 00:20:45,990 Everything will go out of the stack. 328 00:20:46,420 --> 00:20:48,550 OK, so we call it Kostic. 329 00:20:48,550 --> 00:20:50,590 We have already discussed it in functions. 330 00:20:51,140 --> 00:20:53,700 OK, so the order is reversed. 331 00:20:54,100 --> 00:20:55,810 OK, factorial zero function. 332 00:20:55,990 --> 00:20:58,030 So the factorial zero function. 333 00:20:59,180 --> 00:21:02,120 It comes at last, but it goes out first. 334 00:21:02,970 --> 00:21:08,990 OK, dysfunction mean so mean comes at first, but it will go last, OK? 335 00:21:09,000 --> 00:21:11,880 It will wait for the other function to compute their work. 336 00:21:12,110 --> 00:21:14,180 We do not need to go in so much depth. 337 00:21:14,430 --> 00:21:18,920 We will discuss something in the next session which is going to make things very easy for us. 338 00:21:19,060 --> 00:21:22,580 OK, but this is how that equation works internally. 339 00:21:22,880 --> 00:21:25,420 OK, this is how the equation works internally. 340 00:21:26,000 --> 00:21:29,120 Now, I hope you got the idea how the equation is working. 341 00:21:29,730 --> 00:21:32,030 OK, if you want. 342 00:21:33,330 --> 00:21:39,060 If you want, you can optimize the score also how we can optimize so first of all, this is not required. 343 00:21:40,380 --> 00:21:46,740 Now, there is one small problem with discord, what the value of and is minus one, OK, what if the 344 00:21:46,740 --> 00:21:50,670 value of ND is minus one, then it will never stop again. 345 00:21:50,670 --> 00:21:51,720 It will become infinite loop. 346 00:21:51,750 --> 00:21:52,390 Let me show you. 347 00:21:53,310 --> 00:21:57,820 So let the value of N is minus one a negative value, then our good. 348 00:21:57,840 --> 00:22:00,150 Then again it will run into infinite loop. 349 00:22:00,360 --> 00:22:00,750 OK. 350 00:22:02,110 --> 00:22:04,240 OK, so let's come and get our first. 351 00:22:05,280 --> 00:22:10,830 Now, let's see the value of an S minus, I will give the value of N negative, so minus one. 352 00:22:11,430 --> 00:22:16,830 So again, we are getting infinite loop y infinite loop because this is the beginning condition. 353 00:22:17,340 --> 00:22:20,050 OK, now the value of N will never be zero. 354 00:22:20,220 --> 00:22:21,590 It will keep on reducing. 355 00:22:21,600 --> 00:22:22,320 So that's why. 356 00:22:22,560 --> 00:22:24,690 So that's why this will run into infinite loop. 357 00:22:24,720 --> 00:22:25,820 Now to fix this problem. 358 00:22:26,760 --> 00:22:34,590 But we can do here is we can we can write a condition like if the value of it is negative, I will return 359 00:22:34,590 --> 00:22:35,160 minus one. 360 00:22:36,690 --> 00:22:37,170 OK. 361 00:22:37,830 --> 00:22:39,480 All for negative numbers are not defined. 362 00:22:39,510 --> 00:22:39,840 OK. 363 00:22:40,850 --> 00:22:44,270 So in this case, I will give the input a negative number. 364 00:22:45,480 --> 00:22:47,620 Let's say minus five. 365 00:22:48,000 --> 00:22:48,910 So what will happen? 366 00:22:49,290 --> 00:22:53,760 So we are getting minus one, OK, because factor for negative numbers not defined. 367 00:22:55,460 --> 00:22:56,610 We can comment out this. 368 00:22:57,020 --> 00:22:59,630 OK, so the value of. 369 00:23:00,360 --> 00:23:06,120 And is minus five and minus one because factor for negative numbers is not defined. 370 00:23:06,150 --> 00:23:08,530 OK, now this is the correct word. 371 00:23:08,550 --> 00:23:10,410 OK, we are using recursion here. 372 00:23:10,800 --> 00:23:11,610 OK, thank you.