Skip to content

Instantly share code, notes, and snippets.

@PatchRowcester
Created April 19, 2013 03:10
Show Gist options
  • Save PatchRowcester/5417841 to your computer and use it in GitHub Desktop.
Save PatchRowcester/5417841 to your computer and use it in GitHub Desktop.
A recursive function to calculate the exponential of a base number by checking if the exponential is even, odd or equal to 0
def recurPowerNew(base, exp):
'''
base: int or float.
exp: int >= 0
returns: int or float; base^exp
'''
# Your code here
if exp<=0:
return 1
elif exp%2 == 0:
return recurPowerNew(base*base,(exp/2))
return base*recurPowerNew(base, exp-1)
x = float(raw_input('Enter a number (integer or float): '))
y = int(raw_input('Enter the exponential (integer): '))
z = recurPowerNew(x,y)
print('The answer is: ' + str(z))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment