forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_exponentiation.py
More file actions
Latest commit
28 lines (19 loc) · 600 Bytes
/
Copy pathbinary_exponentiation.py
File metadata and controls
28 lines (19 loc) · 600 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""Binary Exponentiation."""
# Author : Junth Basnet
# Time Complexity : O(logn)
defbinary_exponentiation(a, n):
ifn==0:
return1
elifn%2==1:
returnbinary_exponentiation(a, n-1) *a
else:
b=binary_exponentiation(a, n/2)
returnb*b
if__name__=="__main__":
try:
BASE=int(input("Enter Base : ").strip())
POWER=int(input("Enter Power : ").strip())
exceptValueError:
print("Invalid literal for integer")
RESULT=binary_exponentiation(BASE, POWER)
print(f"{BASE}^({POWER}) : {RESULT}")