Skip to content

Commit 97e2de0

Browse files
quant12345cclauss
andauthored
Euler 070 partial replacement of numpy loops. (#9055)
* Euler 070 partial replacement of numpy loops. * Update project_euler/problem_070/sol1.py * project_euler.yml: Upgrade actions/checkout@v4 and add numpy * Update project_euler.yml --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 5a5ca06 commit 97e2de0

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

.github/workflows/project_euler.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ jobs:
1414
project-euler:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v4
1818
- uses: actions/setup-python@v4
1919
with:
2020
python-version: 3.x
2121
- name: Install pytest and pytest-cov
2222
run: |
2323
python -m pip install --upgrade pip
24-
python -m pip install --upgrade pytest pytest-cov
24+
python -m pip install --upgrade numpy pytest pytest-cov
2525
- run: pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=project_euler/ project_euler/
2626
validate-solutions:
2727
runs-on: ubuntu-latest
2828
steps:
29-
- uses: actions/checkout@v3
29+
- uses: actions/checkout@v4
3030
- uses: actions/setup-python@v4
3131
with:
3232
python-version: 3.x
3333
- name: Install pytest and requests
3434
run: |
3535
python -m pip install --upgrade pip
36-
python -m pip install --upgrade pytest requests
36+
python -m pip install --upgrade numpy pytest requests
3737
- run: pytest scripts/validate_solutions.py
3838
env:
3939
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

project_euler/problem_070/sol1.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
"""
3131
from __future__ import annotations
3232

33+
import numpy as np
34+
3335

3436
def get_totients(max_one: int) -> list[int]:
3537
"""
@@ -42,17 +44,14 @@ def get_totients(max_one: int) -> list[int]:
4244
>>> get_totients(10)
4345
[0, 1, 1, 2, 2, 4, 2, 6, 4, 6]
4446
"""
45-
totients = [0] * max_one
46-
47-
for i in range(max_one):
48-
totients[i] = i
47+
totients = np.arange(max_one)
4948

5049
for i in range(2, max_one):
5150
if totients[i] == i:
52-
for j in range(i, max_one, i):
53-
totients[j] -= totients[j] // i
51+
x = np.arange(i, max_one, i) # array of indexes to select
52+
totients[x] -= totients[x] // i
5453

55-
return totients
54+
return totients.tolist()
5655

5756

5857
def has_same_digits(num1: int, num2: int) -> bool:

0 commit comments

Comments
 (0)