mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
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>
This commit is contained in:
parent
5a5ca06944
commit
97e2de0763
8
.github/workflows/project_euler.yml
vendored
8
.github/workflows/project_euler.yml
vendored
|
@ -14,26 +14,26 @@ jobs:
|
||||||
project-euler:
|
project-euler:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: actions/setup-python@v4
|
- uses: actions/setup-python@v4
|
||||||
with:
|
with:
|
||||||
python-version: 3.x
|
python-version: 3.x
|
||||||
- name: Install pytest and pytest-cov
|
- name: Install pytest and pytest-cov
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
python -m pip install --upgrade pytest pytest-cov
|
python -m pip install --upgrade numpy pytest pytest-cov
|
||||||
- run: pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=project_euler/ project_euler/
|
- run: pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=project_euler/ project_euler/
|
||||||
validate-solutions:
|
validate-solutions:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v4
|
||||||
- uses: actions/setup-python@v4
|
- uses: actions/setup-python@v4
|
||||||
with:
|
with:
|
||||||
python-version: 3.x
|
python-version: 3.x
|
||||||
- name: Install pytest and requests
|
- name: Install pytest and requests
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
python -m pip install --upgrade pytest requests
|
python -m pip install --upgrade numpy pytest requests
|
||||||
- run: pytest scripts/validate_solutions.py
|
- run: pytest scripts/validate_solutions.py
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
|
@ -30,6 +30,8 @@ https://en.wikipedia.org/wiki/Euler's_totient_function#Euler's_product_formula
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
def get_totients(max_one: int) -> list[int]:
|
def get_totients(max_one: int) -> list[int]:
|
||||||
"""
|
"""
|
||||||
|
@ -42,17 +44,14 @@ def get_totients(max_one: int) -> list[int]:
|
||||||
>>> get_totients(10)
|
>>> get_totients(10)
|
||||||
[0, 1, 1, 2, 2, 4, 2, 6, 4, 6]
|
[0, 1, 1, 2, 2, 4, 2, 6, 4, 6]
|
||||||
"""
|
"""
|
||||||
totients = [0] * max_one
|
totients = np.arange(max_one)
|
||||||
|
|
||||||
for i in range(max_one):
|
|
||||||
totients[i] = i
|
|
||||||
|
|
||||||
for i in range(2, max_one):
|
for i in range(2, max_one):
|
||||||
if totients[i] == i:
|
if totients[i] == i:
|
||||||
for j in range(i, max_one, i):
|
x = np.arange(i, max_one, i) # array of indexes to select
|
||||||
totients[j] -= totients[j] // i
|
totients[x] -= totients[x] // i
|
||||||
|
|
||||||
return totients
|
return totients.tolist()
|
||||||
|
|
||||||
|
|
||||||
def has_same_digits(num1: int, num2: int) -> bool:
|
def has_same_digits(num1: int, num2: int) -> bool:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user