Write a program which takes two integers x and y and returns the power of x to the y.
This is very easy in Python using the built-in pow
function:
def power_x_y(x, y):
return pow(x, y)
However, let’s assume we can’t use this built-in function. There’s the very obvious brute force solution where we multiply x by itself y times:
def power_x_y_brute_force(x, y):
if not x and not y:
return 1
total = 1
for _ in range(y):
total *= x
return total
This solution runs in O(y) time.