Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So now we have to write a function that calculates the factorial or for a given number.
And we know that a factorial of a given number equals two Nahm multiplied by NUM minus one multiplied
by other numbers up on to you two multiplied by one.
So all the numbers multiplied.
One of the other from one out.
Two given.
Now.
OK.
So there's the factorial of.
Not that what it does.
OK.
And you may seed your math exams as something like this.
So what we want to do is to write a function that will be able to calculate these factorial and also
to add some functionality to our cool calculator that we are developing throughout these course.
And it will be very similar to the previous recursive example that we've seen.
We know that the function will return an integer type.
Right.
Because an end multiplied by end and so on probably is going also to be an integer.
So the type of the function is going to begin.
The name of the function is going to be factorial.
So in factorial and these function will get just one number.
Often in the jury type.
So, um.
Okay.
And now let's specify what these function does.
So we talked about in one of our previous sections that a factorial can be found just by using R and
integrative.
And iterating approach just by using some four loops or while loops.
And we can find the factorial.
And now what we want to do is to find it in some recursive manner.
So what will be the first step?
The first step, we said, is that we need to find what is the recursive call?
What is the rule?
What is the similarity?
How can we divide the problem?
The factorial of NARM to some sub problems.
And one of the ways that we can see here that will help us is that we can see that the factorial of
num, the factorial of num equals two num.
Right.
Multiplied.
Multiplied by what.
Multiplied by all of these.
And all of this part is simply a factorial of num minus one.
So that's just a simple math and some rules that we can see here.
We know that a factorial is the multiplication of one by two.
By whatever comes next.
Up until a given a given number.
And in this case, it's up until num minus one.
So these part this part equals do this part.
And we know that thanks to that, we know that a factorial of a given number, these bigger problem
equals just the num multiplied by these kind of sub problems.
So that will be our recursive function call.
So the function will return.
Right.
We know that a factorial for NUM equals two num multiplied by factorial of num minus one.
So we just copied here.
So num multiplied by a factorial of num minus one.
And as previously, if we use some number here.
Right.
If we use and let's say num num num equals two, three.
And we call these function.
Okay.
And we will create a variable called resolve.
Result equals two factorial of three of num.
Okay.
Of num.
And we want to print out the result.
So result equals two percentage.
And we specify the result.
Here we specify it like this and we know that if we run, if we will build and running 10 and now we
will see that a program also gets stuck.
And the reason for that is B being is because there is no there is no stopping condition.
There is no base case.
Their function is going to call itself over and over again also for negative numbers, because nothing
stops that.
And that's definitely something that you don't want.
You want to stop your recursive calls.
Once you know the base case.
And what will be the base case in this example?
Basically, if we take a look, we know that a factorial of three is one multiplied by two, multiplied
by three and two is one multiplied by two.
And we know that a factorial of one factorial of one is simply one.
OK.
We do not need to multiply it by anything else.
So the stopping condition will be let's just make sure we can use if NUM equals two one, then return
one.
So that's the stopping condition.
And that's only valid.
OK.
Basically we said even in the previous video.
But that's only valid if we assume that all the numbers will be positive.
So if one of the numbers will be.
Negative.
We also want to kind of stop this function, OK?
So even by mistake there, the user or the function that calls these factorial gives here a negative
number.
If we use this condition in this way, we just check if NAM equals equals to one and the user specify,
for example, minus five.
Then this condition will never happen and we are going to get into an infinite recursive calls.
So it's probably going given to be better to specify here if Nahm is less or equals than one.
Then we turn one.
So in this case, we are going to see what happens.
So let's just go over this code like by line by line.
And what we can see here is simply a first of all, we create ngom equals two, three.
Okay.
We can just initialize it like I've done here.
Or we can read this number from the user.
It doesn't matter.
So let's see what what we do here in line 19, we call the function factorial for three.
So function factorial.
That's the first time we would call it factorial.
Factorial for three.
And these function, what he does, edes returns.
It returns right to this result.
He decides the result of the return, the result, this result variable.
So it returns three.
Right.
Because nominals two three multiplied by factorial.
Factorial of two.
Factorial of two.
But we cannot still return this result because we haven't found out of these factorial of two.
There is a recursive call.
So now we need to find what is that?
We turn result for factorial of two.
And we can say that the result is return to right.
Because factorial of two is not equal as the two is just two multiplied by factorial of one.
OK.
So now we know that we are going to call another instance of this function.
So we are going to call factorial of one.
So factorial if one, we know how to find the result.
Four factorial of one because it's trivial and it meets our base case or a stopping condition.
So factorial if one simply returns one and it does not make any further recursive calls.
So return one.
OK.
So return one.
And we know that this one.
OK.
These function.
These factorial one.
Return one.
So instead of these factorial one here, we can specify that their result here is going to be one.
And thus we can calculate the factorial of two, which will be just two.
Right.
Two multiplied by one.
And instead of factorial two, we build their way up.
And so instead of this value here, we will specify just two.
And now we know that these factorial three can return a final result, which is the multiplication of
three by two, which is a total of six.
Right.
And this function, these factorial factorial for three is that.
That's how we called it factorial for three.
Because Nahm here equals two three.
So we see the result of six instead of this line.
So the result is six.
And we can print it out to the screen.
So that's how you do a recursive calls.
And solve the factorial question or problem in a recursive manner.
And if we build and run it, let's just make sure that everything works here so we can see that the
result is six.
And if we modify that a little bit to let's say five.
And we expect that it will be one hundred and twenty year.
Yeah.
That's that's a factorial of five, meaning one multiplied by two, multiplied by three.
Then you just take and multiply it by four and five and you know that six multiplied by four.
We'll give you twenty four and if you multiply it by five it will give you one hundred and twenty.
So this is it for this video guys.
It was very similar to our previous example that we've solved together.
Now I think you're ready to move on to some challenges that I suggest then really recommend you doing
on your own, trying them and only then to see the solutions.
And there's always guys.
Thank you so much for watching.
Continue in your progress study.
More practice makes progress.
And I'll see you in the next video by.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.