Skip to content

Instantly share code, notes, and snippets.

@beiweiqiang
Created July 28, 2018 08:17
Show Gist options
  • Save beiweiqiang/0bbc8ce530bb7c8755062b11bae36573 to your computer and use it in GitHub Desktop.
Save beiweiqiang/0bbc8ce530bb7c8755062b11bae36573 to your computer and use it in GitHub Desktop.
Fast Exponentiation
# 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