Skip to content

Commit

Permalink
Euler072 - application of vector operations to reduce calculation tim…
Browse files Browse the repository at this point in the history
…e and refactoring numpy (TheAlgorithms#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>
  • Loading branch information
quant12345 and pre-commit-ci[bot] authored Oct 1, 2023
1 parent 18cdbc4 commit 8d94f77
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions project_euler/problem_072/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
Time: 1 sec
"""

import numpy as np


def solution(limit: int = 1_000_000) -> int:
"""
Expand All @@ -33,14 +35,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__":
Expand Down

0 comments on commit 8d94f77

Please sign in to comment.