2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
2019-10-06 18:52:04 +00:00
|
|
|
|
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
def modular_division(a: int, b: int, n: int) -> int:
|
|
|
|
"""
|
|
|
|
Modular Division :
|
|
|
|
An efficient algorithm for dividing b by a modulo n.
|
2019-10-06 18:52:04 +00:00
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
|
2019-10-06 18:52:04 +00:00
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should
|
2024-04-22 19:51:47 +00:00
|
|
|
return an integer x such that 0≤x≤n-1, and b/a=x(modn) (that is, b=ax(modn)).
|
2019-10-06 18:52:04 +00:00
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
Theorem:
|
|
|
|
a has a multiplicative inverse modulo n iff gcd(a,n) = 1
|
2019-10-06 18:52:04 +00:00
|
|
|
|
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
This find x = b*a^(-1) mod n
|
|
|
|
Uses ExtendedEuclid to find the inverse of a
|
|
|
|
|
2019-10-06 18:52:04 +00:00
|
|
|
>>> modular_division(4,8,5)
|
|
|
|
2
|
|
|
|
|
|
|
|
>>> modular_division(3,8,5)
|
|
|
|
1
|
|
|
|
|
|
|
|
>>> modular_division(4, 11, 5)
|
|
|
|
4
|
|
|
|
|
|
|
|
"""
|
2023-10-11 18:30:02 +00:00
|
|
|
assert n > 1
|
|
|
|
assert a > 0
|
|
|
|
assert greatest_common_divisor(a, n) == 1
|
2019-10-06 18:52:04 +00:00
|
|
|
(d, t, s) = extended_gcd(n, a) # Implemented below
|
|
|
|
x = (b * s) % n
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2020-11-29 16:20:54 +00:00
|
|
|
def invert_modulo(a: int, n: int) -> int:
|
2019-10-06 18:52:04 +00:00
|
|
|
"""
|
2020-11-29 17:46:26 +00:00
|
|
|
This function find the inverses of a i.e., a^(-1)
|
|
|
|
|
2019-10-06 18:52:04 +00:00
|
|
|
>>> invert_modulo(2, 5)
|
|
|
|
3
|
|
|
|
|
|
|
|
>>> invert_modulo(8,7)
|
|
|
|
1
|
|
|
|
|
|
|
|
"""
|
|
|
|
(b, x) = extended_euclid(a, n) # Implemented below
|
|
|
|
if b < 0:
|
|
|
|
b = (b % n + n) % n
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------ Finding Modular division using invert_modulo -------------------
|
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
|
2020-11-29 16:20:54 +00:00
|
|
|
def modular_division2(a: int, b: int, n: int) -> int:
|
2019-10-06 18:52:04 +00:00
|
|
|
"""
|
2020-11-29 17:46:26 +00:00
|
|
|
This function used the above inversion of a to find x = (b*a^(-1))mod n
|
|
|
|
|
2019-10-06 18:52:04 +00:00
|
|
|
>>> 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
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
|
2019-10-06 18:52:04 +00:00
|
|
|
"""
|
2020-11-29 17:46:26 +00:00
|
|
|
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)
|
2019-10-06 18:52:04 +00:00
|
|
|
|
2020-11-29 17:46:26 +00:00
|
|
|
>>> extended_gcd(7, 5)
|
|
|
|
(1, -2, 3)
|
2019-10-06 18:52:04 +00:00
|
|
|
|
2020-09-10 08:31:26 +00:00
|
|
|
** extended_gcd function is used when d = gcd(a,b) is required in output
|
2019-10-06 18:52:04 +00:00
|
|
|
|
|
|
|
"""
|
2023-10-11 18:30:02 +00:00
|
|
|
assert a >= 0
|
|
|
|
assert b >= 0
|
2019-10-06 18:52:04 +00:00
|
|
|
|
|
|
|
if b == 0:
|
|
|
|
d, x, y = a, 1, 0
|
|
|
|
else:
|
|
|
|
(d, p, q) = extended_gcd(b, a % b)
|
|
|
|
x = q
|
|
|
|
y = p - q * (a // b)
|
|
|
|
|
2023-10-11 18:30:02 +00:00
|
|
|
assert a % d == 0
|
|
|
|
assert b % d == 0
|
2019-10-06 18:52:04 +00:00
|
|
|
assert d == a * x + b * y
|
|
|
|
|
|
|
|
return (d, x, y)
|
|
|
|
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def extended_euclid(a: int, b: int) -> tuple[int, int]:
|
2019-10-06 18:52:04 +00:00
|
|
|
"""
|
2020-11-29 17:46:26 +00:00
|
|
|
Extended Euclid
|
2019-10-06 18:52:04 +00:00
|
|
|
>>> extended_euclid(10, 6)
|
|
|
|
(-1, 2)
|
|
|
|
|
|
|
|
>>> extended_euclid(7, 5)
|
|
|
|
(-2, 3)
|
|
|
|
|
|
|
|
"""
|
|
|
|
if b == 0:
|
|
|
|
return (1, 0)
|
|
|
|
(x, y) = extended_euclid(b, a % b)
|
|
|
|
k = a // b
|
|
|
|
return (y, x - k * y)
|
|
|
|
|
|
|
|
|
2020-11-29 16:20:54 +00:00
|
|
|
def greatest_common_divisor(a: int, b: int) -> int:
|
2019-10-06 18:52:04 +00:00
|
|
|
"""
|
2020-11-29 17:46:26 +00:00
|
|
|
Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
|
|
|
|
Euclid's Algorithm
|
|
|
|
|
2019-10-06 18:52:04 +00:00
|
|
|
>>> greatest_common_divisor(7,5)
|
|
|
|
1
|
|
|
|
|
2020-06-16 08:09:19 +00:00
|
|
|
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.
|
2019-10-06 18:52:04 +00:00
|
|
|
|
|
|
|
>>> greatest_common_divisor(121, 11)
|
|
|
|
11
|
|
|
|
|
|
|
|
"""
|
|
|
|
if a < b:
|
|
|
|
a, b = b, a
|
|
|
|
|
|
|
|
while a % b != 0:
|
|
|
|
a, b = b, a % b
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
2019-10-18 06:13:58 +00:00
|
|
|
if __name__ == "__main__":
|
2020-05-19 10:56:16 +00:00
|
|
|
from doctest import testmod
|
|
|
|
|
2019-10-18 06:13:58 +00:00
|
|
|
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)
|