MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / slow_primes

Function slow_primes

maths/prime_numbers.py:5–29  ·  view source on GitHub ↗

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_p

(max_n: int)

Source from the content-addressed store, hash-verified

3
4
5def slow_primes(max_n: int) -> Generator[int]:
6 """
7 Return a list of all primes numbers up to max.
8 >>> list(slow_primes(0))
9 []
10 >>> list(slow_primes(-1))
11 []
12 >>> list(slow_primes(-10))
13 []
14 >>> list(slow_primes(25))
15 [2, 3, 5, 7, 11, 13, 17, 19, 23]
16 >>> list(slow_primes(11))
17 [2, 3, 5, 7, 11]
18 >>> list(slow_primes(33))
19 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
20 >>> list(slow_primes(1000))[-1]
21 997
22 """
23 numbers: Generator = (i for i in range(1, (max_n + 1)))
24 for i in (n for n in numbers if n > 1):
25 for j in range(2, i):
26 if (i % j) == 0:
27 break
28 else:
29 yield i
30
31
32def primes(max_n: int) -> Generator[int]:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected