Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: 58% faster Project Euler 070 #10558

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion maths/prime_sieve_eratosthenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
"""

from math import isqrt

import numpy as np


def prime_sieve_eratosthenes(num: int) -> list[int]:
"""
Expand Down Expand Up @@ -45,10 +49,36 @@ def prime_sieve_eratosthenes(num: int) -> list[int]:
return [prime for prime in range(2, num + 1) if primes[prime]]


def np_prime_sieve_eratosthenes(max_number: int) -> list[int]:
"""
Returns prime numbers below max_number.
See: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

>>> np_prime_sieve_eratosthenes(10)
[2, 3, 5, 7]
>>> np_prime_sieve_eratosthenes(2)
[]
"""
if max_number <= 2:
return []

# List containing a bool value for every odd number below max_number/2
is_prime = np.ones(max_number // 2, dtype=bool)

for i in range(3, isqrt(max_number - 1) + 1, 2):
if is_prime[i // 2]:
# Mark all multiple of i as not prime using list slicing
is_prime[i**2 // 2 :: i] = False

primes = np.where(is_prime)[0] * 2 + 1
primes[0] = 2
return primes.tolist()


if __name__ == "__main__":
import doctest

doctest.testmod()

user_num = int(input("Enter a positive integer: ").strip())
print(prime_sieve_eratosthenes(user_num))
print(np_prime_sieve_eratosthenes(user_num))
23 changes: 12 additions & 11 deletions project_euler/problem_070/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,31 @@

References:
Finding totients
https://en.wikipedia.org/wiki/Euler's_totient_function#Euler's_product_formula
https://en.wikipedia.org/wiki/Euler%27s_totient_function#Euler%27s_product_formula
"""
from __future__ import annotations

import numpy as np

from maths.prime_sieve_eratosthenes import np_prime_sieve_eratosthenes

def get_totients(max_one: int) -> list[int]:

def np_get_totients(limit) -> list[int]:
"""
Calculates a list of totients from 0 to max_one exclusive, using the
definition of Euler's product formula.

>>> get_totients(5)
>>> np_get_totients(5)
[0, 1, 1, 2, 2]

>>> get_totients(10)
>>> np_get_totients(10)
[0, 1, 1, 2, 2, 4, 2, 6, 4, 6]
"""
totients = np.arange(max_one)
totients = np.arange(limit)
primes = np_prime_sieve_eratosthenes(limit)

for i in range(2, max_one):
if totients[i] == i:
x = np.arange(i, max_one, i) # array of indexes to select
totients[x] -= totients[x] // i
for i in primes:
totients[i::i] -= totients[i::i] // i

return totients.tolist()

Expand Down Expand Up @@ -81,10 +82,10 @@ def solution(max_n: int = 10000000) -> int:
>>> solution(10000)
4435
"""
totients = np_get_totients(max_n + 1)

min_numerator = 1 # i
min_denominator = 0 # φ(i)
totients = get_totients(max_n + 1)

for i in range(2, max_n + 1):
t = totients[i]
Expand All @@ -97,4 +98,4 @@ def solution(max_n: int = 10000000) -> int:


if __name__ == "__main__":
print(f"{solution() = }")
print(f"Solution : {solution()}")