I'm trying to write a small program that calculate exponents recursively and I am a bit stuck. It is a homework assignment and we have been asked to have a base case, when the exponent is an odd number and when the exponent is even. So far I have this:
def quick_power(x,n):
if n == 0:
return 1
elif n % 2 != 0:
return x * quick_power(x, n-1)
elif n % 2 == 0:
return quick_power(quick_power(x, n//2), 2)
And I know that the line with n % 2 == 0 isn't what it should be. Any help is appreciated. Thanks.