Skip to content
Closed
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
79 changes: 8 additions & 71 deletions maths/prime_numbers.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,5 @@
import math
from collections.abc import Generator


def slow_primes(max_n: int) -> Generator[int, None, None]:
"""
Return a list of all primes numbers up to max.
>>> list(slow_primes(0))
[]
>>> list(slow_primes(-1))
[]
>>> list(slow_primes(-10))
[]
>>> list(slow_primes(25))
[2, 3, 5, 7, 11, 13, 17, 19, 23]
>>> list(slow_primes(11))
[2, 3, 5, 7, 11]
>>> list(slow_primes(33))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> list(slow_primes(1000))[-1]
997
"""
numbers: Generator = (i for i in range(1, (max_n + 1)))
for i in (n for n in numbers if n > 1):
for j in range(2, i):
if (i % j) == 0:
break
else:
yield i


def primes(max_n: int) -> Generator[int, None, None]:
"""
Return a list of all primes numbers up to max.
>>> list(primes(0))
[]
>>> list(primes(-1))
[]
>>> list(primes(-10))
[]
>>> list(primes(25))
[2, 3, 5, 7, 11, 13, 17, 19, 23]
>>> list(primes(11))
[2, 3, 5, 7, 11]
>>> list(primes(33))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> list(primes(1000))[-1]
997
"""
numbers: Generator = (i for i in range(1, (max_n + 1)))
for i in (n for n in numbers if n > 1):
# only need to check for factors up to sqrt(i)
bound = int(math.sqrt(i)) + 1
for j in range(2, bound):
if (i % j) == 0:
break
else:
yield i


def fast_primes(max_n: int) -> Generator[int, None, None]:
"""
Return a list of all primes numbers up to max.
Expand All @@ -76,19 +18,14 @@
>>> list(fast_primes(1000))[-1]
997
"""
numbers: Generator = (i for i in range(1, (max_n + 1), 2))
# It's useless to test even numbers as they will not be prime
if max_n > 2:
yield 2 # Because 2 will not be tested, it's necessary to yield it now
for i in (n for n in numbers if n > 1):
bound = int(math.sqrt(i)) + 1
for j in range(3, bound, 2):
# As we removed the even numbers, we don't need them now
if (i % j) == 0:
break
else:
yield i

#it is useless to calculate who is already 2 multiple

Check failure on line 21 in maths/prime_numbers.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

maths/prime_numbers.py:21:58: W291 Trailing whitespace
def countPrimes(self, max_n: int) -> int:

Check failure on line 22 in maths/prime_numbers.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

maths/prime_numbers.py:22:1: SyntaxError: unindent does not match any outer indentation level
seen, ans = [0] * max_n, 0
for num in range(2, max_n):
if seen[num]: continue

Check failure on line 25 in maths/prime_numbers.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E701)

maths/prime_numbers.py:25:25: E701 Multiple statements on one line (colon)
ans += 1
seen[num*num:n:num] = [1] * ((max_n - 1) // num - num + 1)
return ans

def benchmark():
"""
Expand All @@ -107,3 +44,3 @@
for ret in primes(number):
print(ret)
benchmark()
Loading