forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_exponentiation.py
148 lines (120 loc) · 3.77 KB
/
binary_exponentiation.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
Binary Exponentiation
This is a method to find a^b in O(log b) time complexity and is one of the most commonly
used methods of exponentiation. The method is also useful for modular exponentiation,
when the solution to (a^b) % c is required.
To calculate a^b:
- If b is even, then a b = (a * a)^(b / 2)
- If b is odd, then a^b = a * a^(b - 1)
Repeat until b = 1 or b = 0
For modular exponentiation, we use the fact that (a * b) % c = ((a % c) * (b % c)) % c
"""
def binary_exp_recursive(base: int, exponent: int) -> int:
"""
>>> binary_exp_recursive(3, 5)
243
>>> binary_exp_recursive(-1, 3)
-1
>>> binary_exp_recursive(0, 5)
0
>>> binary_exp_recursive(3, 1)
3
>>> binary_exp_recursive(3, 0)
1
>>> binary_exp_recursive(3, -1)
Traceback (most recent call last):
...
ValueError: Exponent must be a non-negative integer
"""
if exponent < 0:
raise ValueError("Exponent must be a non-negative integer")
if exponent == 0:
return 1
if exponent % 2 == 1:
return binary_exp_recursive(base, exponent - 1) * base
b = binary_exp_recursive(base, exponent // 2)
return b * b
def binary_exp_iterative(base: int, exponent: int) -> int:
"""
>>> binary_exp_iterative(3, 5)
243
>>> binary_exp_iterative(-1, 3)
-1
>>> binary_exp_iterative(0, 5)
0
>>> binary_exp_iterative(3, 1)
3
>>> binary_exp_iterative(3, 0)
1
>>> binary_exp_iterative(3, -1)
Traceback (most recent call last):
...
ValueError: Exponent must be a non-negative integer
"""
if exponent < 0:
raise ValueError("Exponent must be a non-negative integer")
res = 1
while exponent > 0:
if exponent & 1:
res *= base
base *= base
exponent >>= 1
return res
def binary_exp_mod_recursive(base: int, exponent: int, modulus: int) -> int:
"""
>>> binary_exp_mod_recursive(3, 4, 5)
1
>>> binary_exp_mod_recursive(7, 13, 10)
7
>>> binary_exp_mod_recursive(7, -1, 10)
Traceback (most recent call last):
...
ValueError: Exponent must be a non-negative integer
>>> binary_exp_mod_recursive(7, 13, 0)
Traceback (most recent call last):
...
ValueError: Modulus must be a positive integer
"""
if exponent < 0:
raise ValueError("Exponent must be a non-negative integer")
if modulus <= 0:
raise ValueError("Modulus must be a positive integer")
if exponent == 0:
return 1
if exponent % 2 == 1:
return (binary_exp_mod_recursive(base, exponent - 1, modulus) * base) % modulus
r = binary_exp_mod_recursive(base, exponent // 2, modulus)
return (r * r) % modulus
def binary_exp_mod_iterative(base: int, exponent: int, modulus: int) -> int:
"""
>>> binary_exp_mod_iterative(3, 4, 5)
1
>>> binary_exp_mod_iterative(7, 13, 10)
7
>>> binary_exp_mod_iterative(7, -1, 10)
Traceback (most recent call last):
...
ValueError: Exponent must be a non-negative integer
>>> binary_exp_mod_iterative(7, 13, 0)
Traceback (most recent call last):
...
ValueError: Modulus must be a positive integer
"""
if exponent < 0:
raise ValueError("Exponent must be a non-negative integer")
if modulus <= 0:
raise ValueError("Modulus must be a positive integer")
res = 1
while exponent > 0:
if exponent & 1:
res = ((res % modulus) * (base % modulus)) % modulus
base *= base
exponent >>= 1
return res
if __name__ == "__main__":
import doctest
doctest.testmod()
BASE = int(input("Enter base: ").strip())
POWER = int(input("Enter power: ").strip())
RESULT = binary_exp_recursive(BASE, POWER)
print(f"{BASE}^({POWER}): {RESULT}")