- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathmodular_division.py
158 lines (111 loc) · 3.39 KB
/
modular_division.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
149
150
151
152
153
154
155
156
157
158
from __future__ importannotations
defmodular_division(a: int, b: int, n: int) ->int:
"""
Modular Division :
An efficient algorithm for dividing b by a modulo n.
GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should
return an integer x such that 0≤x≤n-1, and b/a=x(modn) (that is, b=ax(modn)).
Theorem:
a has a multiplicative inverse modulo n iff gcd(a,n) = 1
This find x = b*a^(-1) mod n
Uses ExtendedEuclid to find the inverse of a
>>> modular_division(4,8,5)
2
>>> modular_division(3,8,5)
1
>>> modular_division(4, 11, 5)
4
"""
assertn>1
asserta>0
assertgreatest_common_divisor(a, n) ==1
(d, t, s) =extended_gcd(n, a) # Implemented below
x= (b*s) %n
returnx
definvert_modulo(a: int, n: int) ->int:
"""
This function find the inverses of a i.e., a^(-1)
>>> invert_modulo(2, 5)
3
>>> invert_modulo(8,7)
1
"""
(b, x) =extended_euclid(a, n) # Implemented below
ifb<0:
b= (b%n+n) %n
returnb
# ------------------ Finding Modular division using invert_modulo -------------------
defmodular_division2(a: int, b: int, n: int) ->int:
"""
This function used the above inversion of a to find x = (b*a^(-1))mod n
>>> modular_division2(4,8,5)
2
>>> modular_division2(3,8,5)
1
>>> modular_division2(4, 11, 5)
4
"""
s=invert_modulo(a, n)
x= (b*s) %n
returnx
defextended_gcd(a: int, b: int) ->tuple[int, int, int]:
"""
Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x
and y, then d = gcd(a,b)
>>> extended_gcd(10, 6)
(2, -1, 2)
>>> extended_gcd(7, 5)
(1, -2, 3)
** extended_gcd function is used when d = gcd(a,b) is required in output
"""
asserta>=0
assertb>=0
ifb==0:
d, x, y=a, 1, 0
else:
(d, p, q) =extended_gcd(b, a%b)
x=q
y=p-q* (a//b)
asserta%d==0
assertb%d==0
assertd==a*x+b*y
return (d, x, y)
defextended_euclid(a: int, b: int) ->tuple[int, int]:
"""
Extended Euclid
>>> extended_euclid(10, 6)
(-1, 2)
>>> extended_euclid(7, 5)
(-2, 3)
"""
ifb==0:
return (1, 0)
(x, y) =extended_euclid(b, a%b)
k=a//b
return (y, x-k*y)
defgreatest_common_divisor(a: int, b: int) ->int:
"""
Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
Euclid's Algorithm
>>> greatest_common_divisor(7,5)
1
Note : In number theory, two integers a and b are said to be relatively prime,
mutually prime, or co-prime if the only positive integer (factor) that divides
both of them is 1 i.e., gcd(a,b) = 1.
>>> greatest_common_divisor(121, 11)
11
"""
ifa<b:
a, b=b, a
whilea%b!=0:
a, b=b, a%b
returnb
if__name__=="__main__":
fromdoctestimporttestmod
testmod(name="modular_division", verbose=True)
testmod(name="modular_division2", verbose=True)
testmod(name="invert_modulo", verbose=True)
testmod(name="extended_gcd", verbose=True)
testmod(name="extended_euclid", verbose=True)
testmod(name="greatest_common_divisor", verbose=True)