From ad217c66165898d62b76cc89ba09c2d7049b6448 Mon Sep 17 00:00:00 2001 From: quant12345 Date: Fri, 29 Sep 2023 17:50:16 +0500 Subject: [PATCH 1/4] 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 2/4] 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 b62818d7de2bae39bfa556ebecaeb8ed332c352a Mon Sep 17 00:00:00 2001 From: quant12345 Date: Fri, 29 Sep 2023 20:32:08 +0500 Subject: [PATCH 3/4] Application of vector operations to reduce calculation time and refactoring numpy. --- project_euler/problem_072/sol1.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_072/sol1.py b/project_euler/problem_072/sol1.py index a2a0eeeb31c5..85169ce5a6ba 100644 --- a/project_euler/problem_072/sol1.py +++ b/project_euler/problem_072/sol1.py @@ -21,6 +21,7 @@ Time: 1 sec """ +import numpy as np def solution(limit: int = 1_000_000) -> int: """ @@ -33,14 +34,15 @@ def solution(limit: int = 1_000_000) -> int: 304191 """ - phi = [i - 1 for i in range(limit + 1)] + # generating an array from -1 to limit + phi = np.arange(-1, limit) for i in range(2, limit + 1): if phi[i] == i - 1: - for j in range(2 * i, limit + 1, i): - phi[j] -= phi[j] // i + ind = np.arange(2 * i, limit + 1, i) # indexes for selection + phi[ind] -= phi[ind] // i - return sum(phi[2 : limit + 1]) + return np.sum(phi[2: limit + 1]) if __name__ == "__main__": From 280b696fa52f9558d422652ff898f1d15859b69d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Sep 2023 15:35:36 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_072/sol1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_072/sol1.py b/project_euler/problem_072/sol1.py index 85169ce5a6ba..5a28be564556 100644 --- a/project_euler/problem_072/sol1.py +++ b/project_euler/problem_072/sol1.py @@ -23,6 +23,7 @@ import numpy as np + def solution(limit: int = 1_000_000) -> int: """ Returns an integer, the solution to the problem @@ -42,7 +43,7 @@ def solution(limit: int = 1_000_000) -> int: ind = np.arange(2 * i, limit + 1, i) # indexes for selection phi[ind] -= phi[ind] // i - return np.sum(phi[2: limit + 1]) + return np.sum(phi[2 : limit + 1]) if __name__ == "__main__":