Skip to content

Commit 8d94f77

Browse files
Euler072 - application of vector operations to reduce calculation time and refactoring numpy (#9229)
* Replacing the generator with numpy vector operations from lu_decomposition. * Revert "Replacing the generator with numpy vector operations from lu_decomposition." This reverts commit ad217c6. * Application of vector operations to reduce calculation time and refactoring numpy. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 18cdbc4 commit 8d94f77

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Diff for: project_euler/problem_072/sol1.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
Time: 1 sec
2222
"""
2323

24+
import numpy as np
25+
2426

2527
def solution(limit: int = 1_000_000) -> int:
2628
"""
@@ -33,14 +35,15 @@ def solution(limit: int = 1_000_000) -> int:
3335
304191
3436
"""
3537

36-
phi = [i - 1 for i in range(limit + 1)]
38+
# generating an array from -1 to limit
39+
phi = np.arange(-1, limit)
3740

3841
for i in range(2, limit + 1):
3942
if phi[i] == i - 1:
40-
for j in range(2 * i, limit + 1, i):
41-
phi[j] -= phi[j] // i
43+
ind = np.arange(2 * i, limit + 1, i) # indexes for selection
44+
phi[ind] -= phi[ind] // i
4245

43-
return sum(phi[2 : limit + 1])
46+
return np.sum(phi[2 : limit + 1])
4447

4548

4649
if __name__ == "__main__":

0 commit comments

Comments
 (0)