Created
April 19, 2013 03:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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