From ad217c66165898d62b76cc89ba09c2d7049b6448 Mon Sep 17 00:00:00 2001 From: quant12345 Date: Fri, 29 Sep 2023 17:50:16 +0500 Subject: [PATCH 01/16] 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 eaabce5449c5..094b20abfecc 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 02/16] 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 094b20abfecc..eaabce5449c5 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 03/16] 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 c9c144759246..41702c301fbd 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 e0a12c79d9f07d83517d9e13736a56721f51d764 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:32:28 +0500 Subject: [PATCH 04/16] Update carmichael_number.py --- maths/carmichael_number.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 41702c301fbd..f48ab68cba7d 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -79,6 +79,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.") From d8efb8c0eb9a6428da1be9259dd38fd7b9b875ec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:33:00 +0000 Subject: [PATCH 05/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/carmichael_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index f48ab68cba7d..439ae7f7a538 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -82,7 +82,7 @@ def is_carmichael_number(n: int) -> bool: import doctest doctest.testmod() - + number = int(input("Enter number: ").strip()) if is_carmichael_number(number): print(f"{number} is a Carmichael Number.") From ff6d418298abd3b7344992216402de4cb1cdf386 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:39:31 +0500 Subject: [PATCH 06/16] Update carmichael_number.py I make an empty commit to reset: tests are failing. --- maths/carmichael_number.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 439ae7f7a538..921398e5eed4 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -83,6 +83,7 @@ def is_carmichael_number(n: int) -> bool: doctest.testmod() + number = int(input("Enter number: ").strip()) if is_carmichael_number(number): print(f"{number} is a Carmichael Number.") From e0c8084d0494a2eae8f760dddf58bf4ed4ad0992 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:39:59 +0000 Subject: [PATCH 07/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/carmichael_number.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 921398e5eed4..439ae7f7a538 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -83,7 +83,6 @@ def is_carmichael_number(n: int) -> bool: doctest.testmod() - number = int(input("Enter number: ").strip()) if is_carmichael_number(number): print(f"{number} is a Carmichael Number.") From 32bcb81c21019d8c063122e9ff0dda2aba595453 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Mon, 9 Oct 2023 22:48:23 +0500 Subject: [PATCH 08/16] Update carmichael_number.py Made changes taking into account the addition: from maths.greatest_common_divisor import greatest_common_divisor. Now instead of gcd it is used: greatest_common_divisor. --- maths/carmichael_number.py | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 439ae7f7a538..1c297f8cb117 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -11,23 +11,7 @@ https://en.wikipedia.org/wiki/Carmichael_number """ - -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: - return b - return gcd(b, a % b) +from maths.greatest_common_divisor import greatest_common_divisor def power(x: int, y: int, mod: int) -> int: @@ -72,7 +56,7 @@ def is_carmichael_number(n: int) -> bool: b = 2 while b < n: - if gcd(b, n) == 1 and power(b, n - 1, n) != 1: + if greatest_common_divisor(b, n) == 1 and power(b, n - 1, n) != 1: return False b += 1 return True From 83909fb1a4753216464ab5777f6a0d987226a0e7 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Mon, 9 Oct 2023 22:52:31 +0500 Subject: [PATCH 09/16] Update carmichael_number.py --- maths/carmichael_number.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 1c297f8cb117..9a62d133aee6 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -14,6 +14,7 @@ from maths.greatest_common_divisor import greatest_common_divisor + def power(x: int, y: int, mod: int) -> int: """ From fa79091ba2fe81a184b57449189ed59ebc3fbf06 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Mon, 9 Oct 2023 22:55:17 +0500 Subject: [PATCH 10/16] Update carmichael_number.py --- maths/carmichael_number.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 9a62d133aee6..fdceffed2247 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -10,11 +10,9 @@ Examples of Carmichael Numbers: 561, 1105, ... https://en.wikipedia.org/wiki/Carmichael_number """ - from maths.greatest_common_divisor import greatest_common_divisor - def power(x: int, y: int, mod: int) -> int: """ From fba99b7356fad7818e37395e38c2a96250cb74a2 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Tue, 10 Oct 2023 13:44:32 +0500 Subject: [PATCH 11/16] Update carmichael_number.py I added a check for 0 and negative numbers in the tests and the code itself. Simplified obtaining the final result. --- maths/carmichael_number.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index fdceffed2247..5efc6b99250d 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -47,18 +47,26 @@ def is_carmichael_number(n: int) -> bool: Traceback (most recent call last): ... ValueError: Number 5.1 must instead be integer + + >>> is_carmichael_number(-7) + Traceback (most recent call last): + ... + ValueError: Number -7 must instead be integer + + >>> is_carmichael_number(0) + Traceback (most recent call last): + ... + ValueError: Number 0 must instead be integer """ - if not isinstance(n, int): + if n <= 0 or not isinstance(n, int): msg = f"Number {n} must instead be integer" raise ValueError(msg) - b = 2 - while b < n: - if greatest_common_divisor(b, n) == 1 and power(b, n - 1, n) != 1: - return False - b += 1 - return True + return all( + power(b, n - 1, n) == 1 + for b in range(2, n) if greatest_common_divisor(b, n) == 1 + ) if __name__ == "__main__": From 1834794ea375939dcd0bcce6970735ff8da1e637 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Tue, 10 Oct 2023 13:56:54 +0500 Subject: [PATCH 12/16] Update carmichael_number.py --- maths/carmichael_number.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 5efc6b99250d..7e8a96b8bf97 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -10,6 +10,7 @@ Examples of Carmichael Numbers: 561, 1105, ... https://en.wikipedia.org/wiki/Carmichael_number """ + from maths.greatest_common_divisor import greatest_common_divisor From 0fbcb91380b446efc3f227fbd1eaa96c098bc3be Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:14:46 +0000 Subject: [PATCH 13/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/carmichael_number.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 7e8a96b8bf97..3c3d4127212d 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -66,7 +66,8 @@ def is_carmichael_number(n: int) -> bool: return all( power(b, n - 1, n) == 1 - for b in range(2, n) if greatest_common_divisor(b, n) == 1 + for b in range(2, n) + if greatest_common_divisor(b, n) == 1 ) From 79a5b64b8c9a46cd91e18c44bad8990d98be8524 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Tue, 10 Oct 2023 20:19:50 +0500 Subject: [PATCH 14/16] Update maths/carmichael_number.py Co-authored-by: Tianyi Zheng --- maths/carmichael_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 3c3d4127212d..2291de319afd 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -61,7 +61,7 @@ def is_carmichael_number(n: int) -> bool: """ if n <= 0 or not isinstance(n, int): - msg = f"Number {n} must instead be integer" + msg = f"Number {n} must instead be a positive integer" raise ValueError(msg) return all( From 41d7d7d28fc99cca029d6137347ab306ecdb538b Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Tue, 10 Oct 2023 20:24:16 +0500 Subject: [PATCH 15/16] Update carmichael_number.py --- maths/carmichael_number.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 2291de319afd..1e32cce49e18 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -47,17 +47,17 @@ def is_carmichael_number(n: int) -> bool: >>> is_carmichael_number(5.1) Traceback (most recent call last): ... - ValueError: Number 5.1 must instead be integer + ValueError: Number 5.1 must instead be a positive intege >>> is_carmichael_number(-7) Traceback (most recent call last): ... - ValueError: Number -7 must instead be integer + ValueError: Number -7 must instead be a positive integer >>> is_carmichael_number(0) Traceback (most recent call last): ... - ValueError: Number 0 must instead be integer + ValueError: Number 0 must instead be a positive intege """ if n <= 0 or not isinstance(n, int): From 2019348cbcab843d7373c1beddd1b106e5ffccc5 Mon Sep 17 00:00:00 2001 From: Kamil <32775019+quant12345@users.noreply.github.com> Date: Tue, 10 Oct 2023 20:31:29 +0500 Subject: [PATCH 16/16] Update carmichael_number.py --- maths/carmichael_number.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 1e32cce49e18..08b5c70e8fe7 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -47,7 +47,7 @@ def is_carmichael_number(n: int) -> bool: >>> is_carmichael_number(5.1) Traceback (most recent call last): ... - ValueError: Number 5.1 must instead be a positive intege + ValueError: Number 5.1 must instead be a positive integer >>> is_carmichael_number(-7) Traceback (most recent call last): @@ -57,7 +57,7 @@ def is_carmichael_number(n: int) -> bool: >>> is_carmichael_number(0) Traceback (most recent call last): ... - ValueError: Number 0 must instead be a positive intege + ValueError: Number 0 must instead be a positive integer """ if n <= 0 or not isinstance(n, int):