Skip to content

Commit 06dad4f

Browse files
authored
* Fix mypy in TheAlgorithms#3149 * Fix pre-commit
1 parent ba6310b commit 06dad4f

File tree

3 files changed

+67
-62
lines changed

3 files changed

+67
-62
lines changed

blockchain/chinese_remainder_theorem.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
# Chinese Remainder Theorem:
2-
# GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
1+
"""
2+
Chinese Remainder Theorem:
3+
GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
34
4-
# If GCD(a,b) = 1, then for any remainder ra modulo a and any remainder rb modulo b
5-
# there exists integer n, such that n = ra (mod a) and n = ra(mod b). If n1 and n2 are
6-
# two such integers, then n1=n2(mod ab)
5+
If GCD(a,b) = 1, then for any remainder ra modulo a and any remainder rb modulo b
6+
there exists integer n, such that n = ra (mod a) and n = ra(mod b). If n1 and n2 are
7+
two such integers, then n1=n2(mod ab)
78
8-
# Algorithm :
9+
Algorithm :
910
10-
# 1. Use extended euclid algorithm to find x,y such that a*x + b*y = 1
11-
# 2. Take n = ra*by + rb*ax
11+
1. Use extended euclid algorithm to find x,y such that a*x + b*y = 1
12+
2. Take n = ra*by + rb*ax
13+
"""
14+
from typing import Tuple
1215

1316

1417
# Extended Euclid
15-
def extended_euclid(a: int, b: int) -> (int, int):
18+
def extended_euclid(a: int, b: int) -> Tuple[int, int]:
1619
"""
1720
>>> extended_euclid(10, 6)
1821
(-1, 2)

blockchain/diophantine_equation.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# Diophantine Equation : Given integers a,b,c ( at least one of a and b != 0), the
2-
# diophantine equation a*x + b*y = c has a solution (where x and y are integers)
3-
# iff gcd(a,b) divides c.
1+
from typing import Tuple
42

5-
# GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
63

7-
8-
def diophantine(a: int, b: int, c: int) -> (int, int):
4+
def diophantine(a: int, b: int, c: int) -> Tuple[float, float]:
95
"""
6+
Diophantine Equation : Given integers a,b,c ( at least one of a and b != 0), the
7+
diophantine equation a*x + b*y = c has a solution (where x and y are integers)
8+
iff gcd(a,b) divides c.
9+
10+
GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
11+
1012
>>> diophantine(10,6,14)
1113
(-7.0, 14.0)
1214
@@ -26,19 +28,19 @@ def diophantine(a: int, b: int, c: int) -> (int, int):
2628
return (r * x, r * y)
2729

2830

29-
# Lemma : if n|ab and gcd(a,n) = 1, then n|b.
30-
31-
# Finding All solutions of Diophantine Equations:
31+
def diophantine_all_soln(a: int, b: int, c: int, n: int = 2) -> None:
32+
"""
33+
Lemma : if n|ab and gcd(a,n) = 1, then n|b.
3234
33-
# Theorem : Let gcd(a,b) = d, a = d*p, b = d*q. If (x0,y0) is a solution of Diophantine
34-
# Equation a*x + b*y = c. a*x0 + b*y0 = c, then all the solutions have the form
35-
# a(x0 + t*q) + b(y0 - t*p) = c, where t is an arbitrary integer.
35+
Finding All solutions of Diophantine Equations:
3636
37-
# n is the number of solution you want, n = 2 by default
37+
Theorem : Let gcd(a,b) = d, a = d*p, b = d*q. If (x0,y0) is a solution of
38+
Diophantine Equation a*x + b*y = c. a*x0 + b*y0 = c, then all the
39+
solutions have the form a(x0 + t*q) + b(y0 - t*p) = c,
40+
where t is an arbitrary integer.
3841
42+
n is the number of solution you want, n = 2 by default
3943
40-
def diophantine_all_soln(a: int, b: int, c: int, n: int = 2) -> None:
41-
"""
4244
>>> diophantine_all_soln(10, 6, 14)
4345
-7.0 14.0
4446
-4.0 9.0
@@ -67,13 +69,12 @@ def diophantine_all_soln(a: int, b: int, c: int, n: int = 2) -> None:
6769
print(x, y)
6870

6971

70-
# Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
71-
72-
# Euclid's Algorithm
73-
74-
7572
def greatest_common_divisor(a: int, b: int) -> int:
7673
"""
74+
Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
75+
76+
Euclid's Algorithm
77+
7778
>>> greatest_common_divisor(7,5)
7879
1
7980
@@ -94,12 +95,11 @@ def greatest_common_divisor(a: int, b: int) -> int:
9495
return b
9596

9697

97-
# Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers
98-
# x and y, then d = gcd(a,b)
99-
100-
101-
def extended_gcd(a: int, b: int) -> (int, int, int):
98+
def extended_gcd(a: int, b: int) -> Tuple[int, int, int]:
10299
"""
100+
Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers
101+
x and y, then d = gcd(a,b)
102+
103103
>>> extended_gcd(10, 6)
104104
(2, -1, 2)
105105

blockchain/modular_division.py

+30-28
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
# Modular Division :
2-
# An efficient algorithm for dividing b by a modulo n.
1+
from typing import Tuple
32

4-
# GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
53

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.
88
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 )
1110
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)).
1213
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
1516
1617
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+
1921
>>> modular_division(4,8,5)
2022
2
2123
@@ -32,9 +34,10 @@ def modular_division(a: int, b: int, n: int) -> int:
3234
return x
3335

3436

35-
# This function find the inverses of a i.e., a^(-1)
3637
def invert_modulo(a: int, n: int) -> int:
3738
"""
39+
This function find the inverses of a i.e., a^(-1)
40+
3841
>>> invert_modulo(2, 5)
3942
3
4043
@@ -50,9 +53,11 @@ def invert_modulo(a: int, n: int) -> int:
5053

5154
# ------------------ Finding Modular division using invert_modulo -------------------
5255

53-
# This function used the above inversion of a to find x = (b*a^(-1))mod n
56+
5457
def modular_division2(a: int, b: int, n: int) -> int:
5558
"""
59+
This function used the above inversion of a to find x = (b*a^(-1))mod n
60+
5661
>>> modular_division2(4,8,5)
5762
2
5863
@@ -68,17 +73,15 @@ def modular_division2(a: int, b: int, n: int) -> int:
6873
return x
6974

7075

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]:
7677
"""
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)
7982
80-
>>> extended_gcd(7, 5)
81-
(1, -2, 3)
83+
>>> extended_gcd(7, 5)
84+
(1, -2, 3)
8285
8386
** extended_gcd function is used when d = gcd(a,b) is required in output
8487
@@ -98,9 +101,9 @@ def extended_gcd(a: int, b: int) -> (int, int, int):
98101
return (d, x, y)
99102

100103

101-
# Extended Euclid
102-
def extended_euclid(a: int, b: int) -> (int, int):
104+
def extended_euclid(a: int, b: int) -> Tuple[int, int]:
103105
"""
106+
Extended Euclid
104107
>>> extended_euclid(10, 6)
105108
(-1, 2)
106109
@@ -115,12 +118,11 @@ def extended_euclid(a: int, b: int) -> (int, int):
115118
return (y, x - k * y)
116119

117120

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-
122121
def greatest_common_divisor(a: int, b: int) -> int:
123122
"""
123+
Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
124+
Euclid's Algorithm
125+
124126
>>> greatest_common_divisor(7,5)
125127
1
126128

0 commit comments

Comments
 (0)