Best way to do powerOf(int x, int n)?
Posted
by Mike
on Stack Overflow
See other posts from Stack Overflow
or by Mike
Published on 2010-03-30T02:29:15Z
Indexed on
2010/03/30
2:33 UTC
Read the original article
Hit count: 410
So given x, and power, n, solve for X^n. There's the easy way that's O(n)... I can get it down to O(n/2), by doing
numSquares = n/2; numOnes = n%2; return (numSquares * x * x + numOnes * x);
Now there's a log(n) solution, does anyone know how to do it? It can be done recursively.
© Stack Overflow or respective owner