2019-07-11 04:09:24 +08:00
|
|
|
"""Binary Exponentiation."""
|
|
|
|
|
|
|
|
# Author : Junth Basnet
|
|
|
|
# Time Complexity : O(logn)
|
|
|
|
|
2019-05-10 19:03:05 +08:00
|
|
|
|
|
|
|
def binary_exponentiation(a, n):
|
2019-07-11 04:09:24 +08:00
|
|
|
|
2019-05-10 19:03:05 +08:00
|
|
|
if (n == 0):
|
|
|
|
return 1
|
2019-07-11 04:09:24 +08:00
|
|
|
|
2019-05-10 19:03:05 +08:00
|
|
|
elif (n % 2 == 1):
|
|
|
|
return binary_exponentiation(a, n - 1) * a
|
2019-07-11 04:09:24 +08:00
|
|
|
|
2019-05-10 19:03:05 +08:00
|
|
|
else:
|
|
|
|
b = binary_exponentiation(a, n / 2)
|
|
|
|
return b * b
|
|
|
|
|
2019-07-11 04:09:24 +08:00
|
|
|
|
2019-05-10 19:03:05 +08:00
|
|
|
try:
|
2019-07-11 04:09:24 +08:00
|
|
|
BASE = int(input('Enter Base : '))
|
|
|
|
POWER = int(input("Enter Power : "))
|
2019-05-10 19:03:05 +08:00
|
|
|
except ValueError:
|
2019-07-11 04:09:24 +08:00
|
|
|
print("Invalid literal for integer")
|
2019-05-10 19:03:05 +08:00
|
|
|
|
2019-07-11 04:09:24 +08:00
|
|
|
RESULT = binary_exponentiation(BASE, POWER)
|
|
|
|
print("{}^({}) : {}".format(BASE, POWER, RESULT))
|