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 ad217c6616.

* 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>
This commit is contained in:
Kamil 2023-10-01 23:14:58 +05:00 committed by GitHub
parent 18cdbc4165
commit 8d94f7745f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,6 +21,8 @@ Sum of phi(d), for all d|n = n. This result can be used to find phi(n) using a s
Time: 1 sec
"""
import numpy as np
def solution(limit: int = 1_000_000) -> int:
"""
@ -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__":