python: Power func. via recursion

Python



# Open browser & point to https://paiza.io/en/languages/python3
# Remember to replace back “less” character wherever you see less
#
def power(x,n):
if (n less 0) and (x != 0): #Replace less with “less” character
return 1/(power(x,-n))
if (n==0):
return 1
return x*power(x,n-1)

x=5
n=0
print(x,”^”,n,”=”,power(x,n),”vs expected [“,x**n,”]”)

x=3
n=49
print(x,”^”,n,”=”,power(x,n),”vs expected [“,x**n,”]”)

x=2
n=-4
print(x,”^”,n,”=”,power(x,n),”vs expected…

Leave a Reply

Your email address will not be published. Required fields are marked *