Consider the problem of calculating power where is any number, and is a positive integer. If you create your own power function, what is best possible complexity of the function?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
A recursive O ( n ) implementation:
We can reduce it to O ( lo g ( n ) ) by calculating
power(x, y//2)
only once at each level.