Created
July 28, 2018 08:17
-
-
Save beiweiqiang/0bbc8ce530bb7c8755062b11bae36573 to your computer and use it in GitHub Desktop.
Fast Exponentiation
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
# Fast Exponentiation | |
# Ask the user to enter 2 integers a and b and output a^b (i.e. pow(a,b)) in O(lg n) time complexity. | |
def my_pow(a, b): | |
if b == 0: return 1 | |
temp = my_pow(a, b // 2) | |
if b % 2 == 0: | |
return temp * temp | |
else: | |
return temp * temp * a | |
def shell(): | |
while True: | |
a = input('a ->') | |
b = input('b ->') | |
print(my_pow(int(a), int(b))) | |
print('') | |
if __name__ == '__main__': | |
shell() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment