forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_exp_mod.py
More file actions
Latest commit
28 lines (23 loc) · 661 Bytes
/
Copy pathbinary_exp_mod.py
File metadata and controls
28 lines (23 loc) · 661 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
defbin_exp_mod(a, n, b):
"""
>>> bin_exp_mod(3, 4, 5)
1
>>> bin_exp_mod(7, 13, 10)
7
"""
# mod b
assertnot (b==0), "This cannot accept modulo that is == 0"
ifn==0:
return1
ifn%2==1:
return (bin_exp_mod(a, n-1, b) *a) %b
r=bin_exp_mod(a, n/2, b)
return (r*r) %b
if__name__=="__main__":
try:
BASE=int(input("Enter Base : ").strip())
POWER=int(input("Enter Power : ").strip())
MODULO=int(input("Enter Modulo : ").strip())
exceptValueError:
print("Invalid literal for integer")
print(bin_exp_mod(BASE, POWER, MODULO))