This algo is log(n) algo for finding power. However, this also has a
problem of overflow. How do we control this.
int power(int x, int n) {
int expo =1;
int even=x;
while (n>0)
{
while (n & 0x1==0) {
n>>=1; /*divide by 2*/
even*=even;
}
expo = expo*even;
n>>=1; /*n will be odd here always*/
}
return expo;
}
this is utilizing the fact that n is a binary number and can be written as
x*xE when odd or xE otherwise.
Best Regards
Ashish Goel
"Think positive and find fuel in failure"
+919985813081
+919966006652
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.