Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,390 --> 00:00:06,280
So now we have to write a function that calculates the factorial or for a given number.
2
00:00:06,300 --> 00:00:15,420
And we know that a factorial of a given number equals two Nahm multiplied by NUM minus one multiplied
3
00:00:16,920 --> 00:00:20,580
by other numbers up on to you two multiplied by one.
4
00:00:20,610 --> 00:00:22,620
So all the numbers multiplied.
5
00:00:22,620 --> 00:00:25,230
One of the other from one out.
6
00:00:25,260 --> 00:00:26,130
Two given.
7
00:00:26,190 --> 00:00:26,520
Now.
8
00:00:26,580 --> 00:00:26,740
OK.
9
00:00:26,820 --> 00:00:28,050
So there's the factorial of.
10
00:00:28,050 --> 00:00:29,460
Not that what it does.
11
00:00:29,490 --> 00:00:29,790
OK.
12
00:00:30,150 --> 00:00:34,980
And you may seed your math exams as something like this.
13
00:00:35,130 --> 00:00:42,120
So what we want to do is to write a function that will be able to calculate these factorial and also
14
00:00:42,120 --> 00:00:48,360
to add some functionality to our cool calculator that we are developing throughout these course.
15
00:00:48,510 --> 00:00:53,010
And it will be very similar to the previous recursive example that we've seen.
16
00:00:53,250 --> 00:00:56,740
We know that the function will return an integer type.
17
00:00:56,780 --> 00:00:56,970
Right.
18
00:00:57,000 --> 00:01:02,760
Because an end multiplied by end and so on probably is going also to be an integer.
19
00:01:02,820 --> 00:01:05,410
So the type of the function is going to begin.
20
00:01:05,740 --> 00:01:08,700
The name of the function is going to be factorial.
21
00:01:08,730 --> 00:01:13,580
So in factorial and these function will get just one number.
22
00:01:13,620 --> 00:01:14,970
Often in the jury type.
23
00:01:15,020 --> 00:01:15,680
So, um.
24
00:01:16,270 --> 00:01:16,560
Okay.
25
00:01:17,250 --> 00:01:19,800
And now let's specify what these function does.
26
00:01:19,890 --> 00:01:28,860
So we talked about in one of our previous sections that a factorial can be found just by using R and
27
00:01:28,890 --> 00:01:29,850
integrative.
28
00:01:30,520 --> 00:01:34,920
And iterating approach just by using some four loops or while loops.
29
00:01:34,920 --> 00:01:36,630
And we can find the factorial.
30
00:01:36,850 --> 00:01:40,290
And now what we want to do is to find it in some recursive manner.
31
00:01:40,320 --> 00:01:42,840
So what will be the first step?
32
00:01:43,320 --> 00:01:49,930
The first step, we said, is that we need to find what is the recursive call?
33
00:01:49,950 --> 00:01:52,080
What is the rule?
34
00:01:52,110 --> 00:01:53,250
What is the similarity?
35
00:01:53,280 --> 00:01:55,320
How can we divide the problem?
36
00:01:55,340 --> 00:01:58,200
The factorial of NARM to some sub problems.
37
00:01:58,530 --> 00:02:06,300
And one of the ways that we can see here that will help us is that we can see that the factorial of
38
00:02:06,300 --> 00:02:10,650
num, the factorial of num equals two num.
39
00:02:10,860 --> 00:02:11,190
Right.
40
00:02:11,220 --> 00:02:12,030
Multiplied.
41
00:02:12,030 --> 00:02:13,290
Multiplied by what.
42
00:02:13,740 --> 00:02:15,150
Multiplied by all of these.
43
00:02:15,180 --> 00:02:21,390
And all of this part is simply a factorial of num minus one.
44
00:02:21,870 --> 00:02:25,830
So that's just a simple math and some rules that we can see here.
45
00:02:26,400 --> 00:02:30,270
We know that a factorial is the multiplication of one by two.
46
00:02:30,300 --> 00:02:32,310
By whatever comes next.
47
00:02:33,390 --> 00:02:36,080
Up until a given a given number.
48
00:02:36,110 --> 00:02:38,910
And in this case, it's up until num minus one.
49
00:02:39,150 --> 00:02:42,610
So these part this part equals do this part.
50
00:02:42,660 --> 00:02:49,470
And we know that thanks to that, we know that a factorial of a given number, these bigger problem
51
00:02:49,740 --> 00:02:55,020
equals just the num multiplied by these kind of sub problems.
52
00:02:55,050 --> 00:02:58,690
So that will be our recursive function call.
53
00:02:59,130 --> 00:03:00,930
So the function will return.
54
00:03:01,410 --> 00:03:01,650
Right.
55
00:03:01,680 --> 00:03:07,230
We know that a factorial for NUM equals two num multiplied by factorial of num minus one.
56
00:03:07,250 --> 00:03:08,530
So we just copied here.
57
00:03:08,600 --> 00:03:14,100
So num multiplied by a factorial of num minus one.
58
00:03:14,670 --> 00:03:18,210
And as previously, if we use some number here.
59
00:03:18,270 --> 00:03:18,540
Right.
60
00:03:18,570 --> 00:03:23,960
If we use and let's say num num num equals two, three.
61
00:03:24,270 --> 00:03:25,680
And we call these function.
62
00:03:25,730 --> 00:03:25,970
Okay.
63
00:03:25,990 --> 00:03:28,240
And we will create a variable called resolve.
64
00:03:28,570 --> 00:03:33,600
Result equals two factorial of three of num.
65
00:03:33,690 --> 00:03:33,990
Okay.
66
00:03:34,020 --> 00:03:34,530
Of num.
67
00:03:34,950 --> 00:03:36,630
And we want to print out the result.
68
00:03:36,630 --> 00:03:39,080
So result equals two percentage.
69
00:03:39,930 --> 00:03:41,430
And we specify the result.
70
00:03:41,460 --> 00:03:49,380
Here we specify it like this and we know that if we run, if we will build and running 10 and now we
71
00:03:49,380 --> 00:03:51,630
will see that a program also gets stuck.
72
00:03:51,930 --> 00:03:57,840
And the reason for that is B being is because there is no there is no stopping condition.
73
00:03:57,870 --> 00:03:59,250
There is no base case.
74
00:03:59,590 --> 00:04:05,850
Their function is going to call itself over and over again also for negative numbers, because nothing
75
00:04:05,850 --> 00:04:06,680
stops that.
76
00:04:06,780 --> 00:04:09,390
And that's definitely something that you don't want.
77
00:04:09,420 --> 00:04:11,850
You want to stop your recursive calls.
78
00:04:12,120 --> 00:04:14,220
Once you know the base case.
79
00:04:14,370 --> 00:04:17,100
And what will be the base case in this example?
80
00:04:18,210 --> 00:04:24,360
Basically, if we take a look, we know that a factorial of three is one multiplied by two, multiplied
81
00:04:24,360 --> 00:04:27,060
by three and two is one multiplied by two.
82
00:04:27,330 --> 00:04:32,010
And we know that a factorial of one factorial of one is simply one.
83
00:04:32,040 --> 00:04:32,240
OK.
84
00:04:32,310 --> 00:04:34,860
We do not need to multiply it by anything else.
85
00:04:35,220 --> 00:04:44,370
So the stopping condition will be let's just make sure we can use if NUM equals two one, then return
86
00:04:44,490 --> 00:04:44,910
one.
87
00:04:44,970 --> 00:04:46,860
So that's the stopping condition.
88
00:04:47,170 --> 00:04:48,750
And that's only valid.
89
00:04:48,810 --> 00:04:49,140
OK.
90
00:04:49,620 --> 00:04:51,360
Basically we said even in the previous video.
91
00:04:51,390 --> 00:04:56,220
But that's only valid if we assume that all the numbers will be positive.
92
00:04:56,250 --> 00:04:59,250
So if one of the numbers will be.
93
00:05:00,030 --> 00:05:00,660
Negative.
94
00:05:00,720 --> 00:05:04,470
We also want to kind of stop this function, OK?
95
00:05:04,620 --> 00:05:10,920
So even by mistake there, the user or the function that calls these factorial gives here a negative
96
00:05:10,920 --> 00:05:11,360
number.
97
00:05:11,610 --> 00:05:18,610
If we use this condition in this way, we just check if NAM equals equals to one and the user specify,
98
00:05:18,670 --> 00:05:20,010
for example, minus five.
99
00:05:20,070 --> 00:05:26,010
Then this condition will never happen and we are going to get into an infinite recursive calls.
100
00:05:26,370 --> 00:05:32,520
So it's probably going given to be better to specify here if Nahm is less or equals than one.
101
00:05:33,810 --> 00:05:34,980
Then we turn one.
102
00:05:35,520 --> 00:05:38,970
So in this case, we are going to see what happens.
103
00:05:39,060 --> 00:05:42,510
So let's just go over this code like by line by line.
104
00:05:42,780 --> 00:05:47,880
And what we can see here is simply a first of all, we create ngom equals two, three.
105
00:05:47,880 --> 00:05:48,040
Okay.
106
00:05:48,120 --> 00:05:50,700
We can just initialize it like I've done here.
107
00:05:51,030 --> 00:05:53,560
Or we can read this number from the user.
108
00:05:53,580 --> 00:05:54,330
It doesn't matter.
109
00:05:54,630 --> 00:06:01,980
So let's see what what we do here in line 19, we call the function factorial for three.
110
00:06:02,280 --> 00:06:03,890
So function factorial.
111
00:06:03,930 --> 00:06:06,030
That's the first time we would call it factorial.
112
00:06:06,060 --> 00:06:07,350
Factorial for three.
113
00:06:07,830 --> 00:06:10,740
And these function, what he does, edes returns.
114
00:06:10,800 --> 00:06:13,800
It returns right to this result.
115
00:06:13,810 --> 00:06:18,890
He decides the result of the return, the result, this result variable.
116
00:06:19,410 --> 00:06:21,250
So it returns three.
117
00:06:21,330 --> 00:06:21,540
Right.
118
00:06:21,570 --> 00:06:25,650
Because nominals two three multiplied by factorial.
119
00:06:26,580 --> 00:06:27,810
Factorial of two.
120
00:06:28,230 --> 00:06:29,070
Factorial of two.
121
00:06:29,100 --> 00:06:35,370
But we cannot still return this result because we haven't found out of these factorial of two.
122
00:06:35,400 --> 00:06:37,050
There is a recursive call.
123
00:06:37,680 --> 00:06:39,900
So now we need to find what is that?
124
00:06:39,900 --> 00:06:43,080
We turn result for factorial of two.
125
00:06:43,470 --> 00:06:47,180
And we can say that the result is return to right.
126
00:06:47,190 --> 00:06:54,210
Because factorial of two is not equal as the two is just two multiplied by factorial of one.
127
00:06:54,520 --> 00:06:54,830
OK.
128
00:06:55,470 --> 00:07:00,000
So now we know that we are going to call another instance of this function.
129
00:07:00,020 --> 00:07:02,610
So we are going to call factorial of one.
130
00:07:02,670 --> 00:07:06,180
So factorial if one, we know how to find the result.
131
00:07:06,180 --> 00:07:11,970
Four factorial of one because it's trivial and it meets our base case or a stopping condition.
132
00:07:12,300 --> 00:07:19,500
So factorial if one simply returns one and it does not make any further recursive calls.
133
00:07:19,650 --> 00:07:20,950
So return one.
134
00:07:21,270 --> 00:07:21,500
OK.
135
00:07:21,570 --> 00:07:22,530
So return one.
136
00:07:22,820 --> 00:07:24,190
And we know that this one.
137
00:07:24,270 --> 00:07:24,540
OK.
138
00:07:24,600 --> 00:07:25,260
These function.
139
00:07:25,290 --> 00:07:26,280
These factorial one.
140
00:07:26,280 --> 00:07:27,120
Return one.
141
00:07:27,360 --> 00:07:32,760
So instead of these factorial one here, we can specify that their result here is going to be one.
142
00:07:33,120 --> 00:07:37,440
And thus we can calculate the factorial of two, which will be just two.
143
00:07:37,450 --> 00:07:37,800
Right.
144
00:07:38,010 --> 00:07:39,190
Two multiplied by one.
145
00:07:39,210 --> 00:07:41,850
And instead of factorial two, we build their way up.
146
00:07:42,120 --> 00:07:47,130
And so instead of this value here, we will specify just two.
147
00:07:47,580 --> 00:07:55,050
And now we know that these factorial three can return a final result, which is the multiplication of
148
00:07:55,050 --> 00:07:58,020
three by two, which is a total of six.
149
00:07:58,110 --> 00:07:58,440
Right.
150
00:07:58,830 --> 00:08:02,650
And this function, these factorial factorial for three is that.
151
00:08:02,880 --> 00:08:06,170
That's how we called it factorial for three.
152
00:08:06,200 --> 00:08:08,730
Because Nahm here equals two three.
153
00:08:09,120 --> 00:08:13,950
So we see the result of six instead of this line.
154
00:08:14,790 --> 00:08:16,020
So the result is six.
155
00:08:16,320 --> 00:08:18,300
And we can print it out to the screen.
156
00:08:18,900 --> 00:08:21,240
So that's how you do a recursive calls.
157
00:08:21,300 --> 00:08:26,130
And solve the factorial question or problem in a recursive manner.
158
00:08:26,250 --> 00:08:32,070
And if we build and run it, let's just make sure that everything works here so we can see that the
159
00:08:32,070 --> 00:08:33,090
result is six.
160
00:08:33,150 --> 00:08:37,050
And if we modify that a little bit to let's say five.
161
00:08:37,170 --> 00:08:39,900
And we expect that it will be one hundred and twenty year.
162
00:08:39,920 --> 00:08:40,160
Yeah.
163
00:08:40,890 --> 00:08:46,200
That's that's a factorial of five, meaning one multiplied by two, multiplied by three.
164
00:08:46,680 --> 00:08:52,900
Then you just take and multiply it by four and five and you know that six multiplied by four.
165
00:08:53,100 --> 00:08:59,640
We'll give you twenty four and if you multiply it by five it will give you one hundred and twenty.
166
00:09:00,570 --> 00:09:02,700
So this is it for this video guys.
167
00:09:02,940 --> 00:09:07,770
It was very similar to our previous example that we've solved together.
168
00:09:08,000 --> 00:09:15,240
Now I think you're ready to move on to some challenges that I suggest then really recommend you doing
169
00:09:15,240 --> 00:09:19,620
on your own, trying them and only then to see the solutions.
170
00:09:20,370 --> 00:09:21,460
And there's always guys.
171
00:09:21,540 --> 00:09:22,890
Thank you so much for watching.
172
00:09:23,010 --> 00:09:25,950
Continue in your progress study.
173
00:09:25,950 --> 00:09:28,170
More practice makes progress.
174
00:09:28,260 --> 00:09:30,240
And I'll see you in the next video by.
14543
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.