Last active
May 23, 2018 12:30
-
-
Save hackintoshrao/cdb5d993d98867b1592bcb6c0e9f9b5d to your computer and use it in GitHub Desktop.
find power by using recursion
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 power(x, n): | |
""" | |
Compute the value x n for integer n | |
""" | |
if n == 0: | |
return 1 | |
else: | |
partial = power(x, n // 2) | |
result = partial * partial | |
if n % 2 == 1: | |
result *= x | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment