Hello everyone.
So in this video we will try to solve one more equation problem.
So I want to calculate power.
OK so I want to calculate extra the power.
OK so x is any number and and is a whole number that is 0 1 2 and so on.
Okay so suppose I want to calculate 5 to the power tree.
So this will be 5 to 5 and 2 5 which is 125.
Okay.
So I want to calculate power.
Okay.
So what I want to do I want to I want to write the power function and I want to find extra the power.
Okay so X is a any number and n is a whole number.
Okay.
So how we can solve this problem.
So first we have to think of the recursive case.
Okay so what is extra the power and so can we write it like this X Multiply extra the power N minus
one.
Okay.
So we can write it like this extra the power and is calculate excited about N minus one and then multiply
x.
Okay.
So we have two.
So I've learned to convert bigger problem than I have to calculate the smaller problem so this will
be our recursive case.
Okay.
So if I want to find x to the power n what I will do first of all I will find X to the power end minus
one and then I will multiply x.
Okay.
So this is our recursive case this will be our recursive case.
Okay.
So what will we our base case.
So base case is the smallest problem whose solution we already know.
So suppose if the value of minus zero.
If the value of any is it also that if the power is zero then the output will be 1.
So this will be the smallest.
This is the base case.
You can also take one as one also base case if X did about 1 will be X only.
Okay so it is our choice you can pick this one and you can also take this one.
Okay.
You can take both our choice.
Okay so let's write the code for it.
So what I want to do I want to calculate.
So there that I will be in danger the name of the function is powered it will take 2 in digit.
I want to find extra the power in now what is the base case.
So first of all base case.
So base case will be the smallest problem whose solution we already know.
So the smallest problem will be if the value of any 0 because and is a whole number.
Okay so the smallest really often will be zero only.
So if the value of any 0.
What I have to do I have to return 1.
Okay.
And now our recursive case so recursive case we already decided what we will do.
I will call on and minus 1.
Okay.
So our small output.
Okay so the small output it will be I want to find extra the power in minus one.
So add this line.
We have extra about N minus one.
Now after we have extra the power and minus one we have to do other conclusion part so what is our conclusion
but basically I will return X Multiply small output Okay and let's call let's test our function.
Suppose I want to find 5 to the power tree.
Okay so five cube so five cube will be 125 okay.
So let's say our file so our output is 125 solver code is working fine.
Okay now we will write in our code after writing the code.
So we think and learn.
We think only in terms of PMI.
And then after writing the code what do we do.
We will dial in our code.
We will make the diagram.
Okay.
So I want to find 5 2 about three.
So what will happen this is 5 and this is 3 okay.
So the value of x is not changing only the value of end is changing so I am writing only 3 here.
Okay so this is 3 so 3 is not close to zero.
So I will come at this line so and minus when we are calling 1 and minus 1.
If you want you can also write 5 or so okay so 5.
Commentary.
Now this is 5 comma 2.
Again we will call on 5 comma 1.
Now we will call on 5 comma 0.
Okay.
If the value of end is it overtime returning I am returning 1 so this will return 1.
Okay so our small output this is 1 then I am returning X Multiply small output so the value of x is
5 so 5 multiply 1.
So this will be 5.
So our small output is 5 then I am returning X Multiply small output so 5 multiply 5.
So I will return 25.
So our small output is 25 then I am returning X Multiply small output so 5 multiplied 25.
So it will return when 25 and 5 cube is 125 so I will our it is working fine.
Okay so this will be their diagram okay.
So I hope by this time we know the procedure first we have to write the code and after writing the code
and when.
And while we are writing the code will think only in terms of PMI.
After writing the code we will draw this diagram.
Okay too so that we can understand it better be.
Okay so this is our code so base case our small output which is really recursive case and then our calculation
part.
So this is how this code is working this is how we calculate power function this is how we can read
about using recursion.
Okay.
Thank you.