Friday, March 29, 2013

a to the power b using log n

Generate A to power B

int power(int a, int b) {
if (b == 0)
return 1;
if (b == 1)
return a;
if (b<0)
return 1/power(a, -b); // a should not be 0
int x = power(a, b/2);
return b&1? x*x*a:x*x;
}

No comments: