mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
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:
parent
18cdbc4165
commit
8d94f7745f
|
@ -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
|
Time: 1 sec
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
def solution(limit: int = 1_000_000) -> int:
|
def solution(limit: int = 1_000_000) -> int:
|
||||||
"""
|
"""
|
||||||
|
@ -33,14 +35,15 @@ def solution(limit: int = 1_000_000) -> int:
|
||||||
304191
|
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):
|
for i in range(2, limit + 1):
|
||||||
if phi[i] == i - 1:
|
if phi[i] == i - 1:
|
||||||
for j in range(2 * i, limit + 1, i):
|
ind = np.arange(2 * i, limit + 1, i) # indexes for selection
|
||||||
phi[j] -= phi[j] // i
|
phi[ind] -= phi[ind] // i
|
||||||
|
|
||||||
return sum(phi[2 : limit + 1])
|
return np.sum(phi[2 : limit + 1])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user