-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_modular.py
67 lines (52 loc) · 1.61 KB
/
fast_modular.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
from Rabin_Miller import *
# 快速模幂算法 Fast Exponentiation
# This file contains the implementation of the Fast Exponentiation algorithm by both squaring and binary method.
def FastExponentation(a, p, n):
"""
Computes a^p mod n using the method of exponentiation by squaring.
Parameters:
a (int): The base.
p (int): The exponent.
n (int): The modulus.
Returns:
int: The result of a^p mod n.
Examples:
>>> FastExponentation(2, 10, 1000)
24
>>> FastExponentation(3, 5, 13)
5
"""
if p == 0:
return 1
if p % 2 == 0:
t = FastExponentation(a, p // 2, n)
return (t * t) % n
else:
t = FastExponentation(a, (p - 1) // 2, n)
return (a * t * t) % n
def MODULAR_EXPONENTIATION(a, b, n):
"""
Perform modular exponentiation using the binary method.
This function calculates (a^b) % n using an efficient method that
leverages the binary representation of the exponent.
Parameters:
a (int): The base.
b (int): The exponent.
n (int): The modulus.
Returns:
int: The result of (a^b) % n.
"""
c = 0
d = 1
binary_representation = bin(b)[2:] # Convert b to binary representation
print("The binary representation is ", binary_representation)
for i in range(len(binary_representation) - 1, -1, -1):
c = c << 1 # Multiply c by 2
d = (d * d) % n
if binary_representation[i] == '1':
c = c + 1
d = (d * a) % n
return d
if __name__ == "__main__":
print(FastExponentation(14, 3, 29))
print(MODULAR_EXPONENTIATION(14, 3, 29))