From ad217c66165898d62b76cc89ba09c2d7049b6448 Mon Sep 17 00:00:00 2001 From: quant12345 Date: Fri, 29 Sep 2023 17:50:16 +0500 Subject: [PATCH 1/5] Replacing the generator with numpy vector operations from lu_decomposition. --- arithmetic_analysis/lu_decomposition.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arithmetic_analysis/lu_decomposition.py b/arithmetic_analysis/lu_decomposition.py index eaabce5449c5e..094b20abfecce 100644 --- a/arithmetic_analysis/lu_decomposition.py +++ b/arithmetic_analysis/lu_decomposition.py @@ -88,15 +88,19 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray lower = np.zeros((rows, columns)) upper = np.zeros((rows, columns)) + + # in 'total', the necessary data is extracted through slices + # and the sum of the products is obtained. + for i in range(columns): for j in range(i): - total = sum(lower[i][k] * upper[k][j] for k in range(j)) + total = np.sum(lower[i, :i] * upper[:i, j]) if upper[j][j] == 0: raise ArithmeticError("No LU decomposition exists") lower[i][j] = (table[i][j] - total) / upper[j][j] lower[i][i] = 1 for j in range(i, columns): - total = sum(lower[i][k] * upper[k][j] for k in range(j)) + total = np.sum(lower[i, :i] * upper[:i, j]) upper[i][j] = table[i][j] - total return lower, upper From 07dfc067e475f36ddce1549f33197f15f65f59d1 Mon Sep 17 00:00:00 2001 From: quant12345 Date: Fri, 29 Sep 2023 17:53:57 +0500 Subject: [PATCH 2/5] Revert "Replacing the generator with numpy vector operations from lu_decomposition." This reverts commit ad217c66165898d62b76cc89ba09c2d7049b6448. --- arithmetic_analysis/lu_decomposition.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/arithmetic_analysis/lu_decomposition.py b/arithmetic_analysis/lu_decomposition.py index 094b20abfecce..eaabce5449c5e 100644 --- a/arithmetic_analysis/lu_decomposition.py +++ b/arithmetic_analysis/lu_decomposition.py @@ -88,19 +88,15 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray lower = np.zeros((rows, columns)) upper = np.zeros((rows, columns)) - - # in 'total', the necessary data is extracted through slices - # and the sum of the products is obtained. - for i in range(columns): for j in range(i): - total = np.sum(lower[i, :i] * upper[:i, j]) + total = sum(lower[i][k] * upper[k][j] for k in range(j)) if upper[j][j] == 0: raise ArithmeticError("No LU decomposition exists") lower[i][j] = (table[i][j] - total) / upper[j][j] lower[i][i] = 1 for j in range(i, columns): - total = np.sum(lower[i, :i] * upper[:i, j]) + total = sum(lower[i][k] * upper[k][j] for k in range(j)) upper[i][j] = table[i][j] - total return lower, upper From 0e0c707e9759a4184e6f57319fd691b27b4a1a36 Mon Sep 17 00:00:00 2001 From: quant12345 Date: Sat, 7 Oct 2023 21:31:06 +0500 Subject: [PATCH 3/5] Added doctests --- maths/carmichael_number.py | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index c9c1447592460..41702c301fbd6 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -13,6 +13,16 @@ def gcd(a: int, b: int) -> int: + """ + + Examples: + >>> gcd(9, 3) + 3 + + >>> gcd(2, 1) + 1 + """ + if a < b: return gcd(b, a) if a % b == 0: @@ -21,6 +31,16 @@ def gcd(a: int, b: int) -> int: def power(x: int, y: int, mod: int) -> int: + """ + + Examples: + >>> power(2, 15, 3) + 2 + + >>> power(5, 1, 30) + 5 + """ + if y == 0: return 1 temp = power(x, y // 2, mod) % mod @@ -31,6 +51,25 @@ def power(x: int, y: int, mod: int) -> int: def is_carmichael_number(n: int) -> bool: + """ + + Examples: + >>> is_carmichael_number(562) + False + + >>> is_carmichael_number(561) + True + + >>> is_carmichael_number(5.1) + Traceback (most recent call last): + ... + ValueError: Number 5.1 must instead be integer + """ + + if not isinstance(n, int): + msg = f"Number {n} must instead be integer" + raise ValueError(msg) + b = 2 while b < n: if gcd(b, n) == 1 and power(b, n - 1, n) != 1: From 2efac811faa22dabda38fad180f2042ecdd36880 Mon Sep 17 00:00:00 2001 From: quant12345 Date: Sat, 7 Oct 2023 22:28:49 +0500 Subject: [PATCH 4/5] Added doctests. --- maths/carmichael_number.py | 39 -------------------------------------- 1 file changed, 39 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 41702c301fbd6..c9c1447592460 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -13,16 +13,6 @@ def gcd(a: int, b: int) -> int: - """ - - Examples: - >>> gcd(9, 3) - 3 - - >>> gcd(2, 1) - 1 - """ - if a < b: return gcd(b, a) if a % b == 0: @@ -31,16 +21,6 @@ def gcd(a: int, b: int) -> int: def power(x: int, y: int, mod: int) -> int: - """ - - Examples: - >>> power(2, 15, 3) - 2 - - >>> power(5, 1, 30) - 5 - """ - if y == 0: return 1 temp = power(x, y // 2, mod) % mod @@ -51,25 +31,6 @@ def power(x: int, y: int, mod: int) -> int: def is_carmichael_number(n: int) -> bool: - """ - - Examples: - >>> is_carmichael_number(562) - False - - >>> is_carmichael_number(561) - True - - >>> is_carmichael_number(5.1) - Traceback (most recent call last): - ... - ValueError: Number 5.1 must instead be integer - """ - - if not isinstance(n, int): - msg = f"Number {n} must instead be integer" - raise ValueError(msg) - b = 2 while b < n: if gcd(b, n) == 1 and power(b, n - 1, n) != 1: From a2eb0e91ec68cd8777cfd08b28e2a2909340f42a Mon Sep 17 00:00:00 2001 From: quant12345 Date: Sat, 7 Oct 2023 22:35:51 +0500 Subject: [PATCH 5/5] Added doctests. --- maths/carmichael_number.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index c9c1447592460..17a24c2b48217 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -13,6 +13,15 @@ def gcd(a: int, b: int) -> int: + """ + + Examples: + >>> gcd(9, 3) + 3 + + >>> gcd(2, 1) + 1 + """ if a < b: return gcd(b, a) if a % b == 0: @@ -21,6 +30,15 @@ def gcd(a: int, b: int) -> int: def power(x: int, y: int, mod: int) -> int: + """ + + Examples: + >>> power(2, 15, 3) + 2 + + >>> power(5, 1, 30) + 5 + """ if y == 0: return 1 temp = power(x, y // 2, mod) % mod @@ -31,6 +49,20 @@ def power(x: int, y: int, mod: int) -> int: def is_carmichael_number(n: int) -> bool: + """ + + Examples: + >>> is_carmichael_number(562) + False + + >>> is_carmichael_number(561) + True + + >>> is_carmichael_number(5.1) + Traceback (most recent call last): + ... + ValueError: Number 5.1 must instead be integer + """ b = 2 while b < n: if gcd(b, n) == 1 and power(b, n - 1, n) != 1: @@ -40,6 +72,10 @@ def is_carmichael_number(n: int) -> bool: if __name__ == "__main__": + import doctest + + doctest.testmod() + number = int(input("Enter number: ").strip()) if is_carmichael_number(number): print(f"{number} is a Carmichael Number.")