1
- # Modular Division :
2
- # An efficient algorithm for dividing b by a modulo n.
1
+ from typing import Tuple
3
2
4
- # GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
5
3
6
- # Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should
7
- # return an integer x such that 0≤x≤n−1, and b/a=x(modn) (that is, b=ax(modn)).
4
+ def modular_division (a : int , b : int , n : int ) -> int :
5
+ """
6
+ Modular Division :
7
+ An efficient algorithm for dividing b by a modulo n.
8
8
9
- # Theorem:
10
- # a has a multiplicative inverse modulo n iff gcd(a,n) = 1
9
+ GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
11
10
11
+ Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should
12
+ return an integer x such that 0≤x≤n−1, and b/a=x(modn) (that is, b=ax(modn)).
12
13
13
- # This find x = b*a^(-1) mod n
14
- # Uses ExtendedEuclid to find the inverse of a
14
+ Theorem:
15
+ a has a multiplicative inverse modulo n iff gcd(a,n) = 1
15
16
16
17
17
- def modular_division (a : int , b : int , n : int ) -> int :
18
- """
18
+ This find x = b*a^(-1) mod n
19
+ Uses ExtendedEuclid to find the inverse of a
20
+
19
21
>>> modular_division(4,8,5)
20
22
2
21
23
@@ -32,9 +34,10 @@ def modular_division(a: int, b: int, n: int) -> int:
32
34
return x
33
35
34
36
35
- # This function find the inverses of a i.e., a^(-1)
36
37
def invert_modulo (a : int , n : int ) -> int :
37
38
"""
39
+ This function find the inverses of a i.e., a^(-1)
40
+
38
41
>>> invert_modulo(2, 5)
39
42
3
40
43
@@ -50,9 +53,11 @@ def invert_modulo(a: int, n: int) -> int:
50
53
51
54
# ------------------ Finding Modular division using invert_modulo -------------------
52
55
53
- # This function used the above inversion of a to find x = (b*a^(-1))mod n
56
+
54
57
def modular_division2 (a : int , b : int , n : int ) -> int :
55
58
"""
59
+ This function used the above inversion of a to find x = (b*a^(-1))mod n
60
+
56
61
>>> modular_division2(4,8,5)
57
62
2
58
63
@@ -68,17 +73,15 @@ def modular_division2(a: int, b: int, n: int) -> int:
68
73
return x
69
74
70
75
71
- # Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x
72
- # and y, then d = gcd(a,b)
73
-
74
-
75
- def extended_gcd (a : int , b : int ) -> (int , int , int ):
76
+ def extended_gcd (a : int , b : int ) -> Tuple [int , int , int ]:
76
77
"""
77
- >>> extended_gcd(10, 6)
78
- (2, -1, 2)
78
+ Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x
79
+ and y, then d = gcd(a,b)
80
+ >>> extended_gcd(10, 6)
81
+ (2, -1, 2)
79
82
80
- >>> extended_gcd(7, 5)
81
- (1, -2, 3)
83
+ >>> extended_gcd(7, 5)
84
+ (1, -2, 3)
82
85
83
86
** extended_gcd function is used when d = gcd(a,b) is required in output
84
87
@@ -98,9 +101,9 @@ def extended_gcd(a: int, b: int) -> (int, int, int):
98
101
return (d , x , y )
99
102
100
103
101
- # Extended Euclid
102
- def extended_euclid (a : int , b : int ) -> (int , int ):
104
+ def extended_euclid (a : int , b : int ) -> Tuple [int , int ]:
103
105
"""
106
+ Extended Euclid
104
107
>>> extended_euclid(10, 6)
105
108
(-1, 2)
106
109
@@ -115,12 +118,11 @@ def extended_euclid(a: int, b: int) -> (int, int):
115
118
return (y , x - k * y )
116
119
117
120
118
- # Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
119
- # Euclid's Algorithm
120
-
121
-
122
121
def greatest_common_divisor (a : int , b : int ) -> int :
123
122
"""
123
+ Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
124
+ Euclid's Algorithm
125
+
124
126
>>> greatest_common_divisor(7,5)
125
127
1
126
128
0 commit comments