int fastPow(int a, int b)
{
if(b==0)
return a;
int smallAns = fastPow(a, b/2);
smallAns *= smallAns;
if(b & 1)
return smallAns*a;
return smallAns;
}
what is this doing?
int fastPow(int a, int b)
{
if(b==0)
return a;
int smallAns = fastPow(a, b/2);
smallAns *= smallAns;
if(b & 1)
return smallAns*a;
return smallAns;
}
what is this doing?
hey,
this is a faster method to calculate power by recursion in this method you have to just calculate square of given number and it recursively give u the desired power of that number,like you can calculate 4^5=4*(4^4)=4*(4^2)*(4^2),in this your progra, will just calculate 4^2 which is basically multiplication of two numbers.there is a small change in solution given by editorial the function will return 1 when (b==0).hope it helps,thanx
on 4,5 answer i am getting is 67108864 which is wrong according to your explanation
sorry i didn’t get in what you are getting wrong answer
ohhh i get it,i told you to change the solution a little bit by returning 1 in the fastPow function when you get get(b==0) it has written to return a when b==0 which is a mistake actually
but in editorial it is ‘a’
@Sahilgohrisg bro i think by mistake it was written ‘a’ in editorial please you go with what i told you,don’t return ‘a’ return 1.