* Fix mypy in #3149 
* Fix pre-commit
This commit is contained in:
John Law
2020-11-30 01:46:26 +08:00
committed by GitHub
parent ba6310b647
commit 06dad4f9d8
3 changed files with 72 additions and 67 deletions

View File

@ -1,21 +1,23 @@
# 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≤n1, 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
from typing import Tuple
def modular_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≤n1, 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
@ -32,9 +34,10 @@ def modular_division(a: int, b: int, n: int) -> int:
return x
# This function find the inverses of a i.e., a^(-1)
def invert_modulo(a: int, n: int) -> int:
"""
This function find the inverses of a i.e., a^(-1)
>>> invert_modulo(2, 5)
3
@ -50,9 +53,11 @@ def invert_modulo(a: int, n: int) -> int:
# ------------------ Finding Modular division using invert_modulo -------------------
# This function used the above inversion of a to find x = (b*a^(-1))mod n
def modular_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
@ -68,17 +73,15 @@ def modular_division2(a: int, b: int, n: int) -> int:
return x
# 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)
def extended_gcd(a: int, b: int) -> (int, int, int):
def extended_gcd(a: int, b: int) -> Tuple[int, int, int]:
"""
>>> extended_gcd(10, 6)
(2, -1, 2)
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(7, 5)
(1, -2, 3)
** extended_gcd function is used when d = gcd(a,b) is required in output
@ -98,9 +101,9 @@ def extended_gcd(a: int, b: int) -> (int, int, int):
return (d, x, y)
# Extended Euclid
def extended_euclid(a: int, b: int) -> (int, int):
def extended_euclid(a: int, b: int) -> Tuple[int, int]:
"""
Extended Euclid
>>> extended_euclid(10, 6)
(-1, 2)
@ -115,12 +118,11 @@ def extended_euclid(a: int, b: int) -> (int, int):
return (y, x - k * y)
# Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
# Euclid's Algorithm
def greatest_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