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
(max_n: int)
| 30 | |
| 31 | |
| 32 | def primes(max_n: int) -> Generator[int]: |
| 33 | """ |
| 34 | Return a list of all primes numbers up to max. |
| 35 | >>> list(primes(0)) |
| 36 | [] |
| 37 | >>> list(primes(-1)) |
| 38 | [] |
| 39 | >>> list(primes(-10)) |
| 40 | [] |
| 41 | >>> list(primes(25)) |
| 42 | [2, 3, 5, 7, 11, 13, 17, 19, 23] |
| 43 | >>> list(primes(11)) |
| 44 | [2, 3, 5, 7, 11] |
| 45 | >>> list(primes(33)) |
| 46 | [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] |
| 47 | >>> list(primes(1000))[-1] |
| 48 | 997 |
| 49 | """ |
| 50 | numbers: Generator = (i for i in range(1, (max_n + 1))) |
| 51 | for i in (n for n in numbers if n > 1): |
| 52 | # only need to check for factors up to sqrt(i) |
| 53 | bound = int(math.sqrt(i)) + 1 |
| 54 | for j in range(2, bound): |
| 55 | if (i % j) == 0: |
| 56 | break |
| 57 | else: |
| 58 | yield i |
| 59 | |
| 60 | |
| 61 | def fast_primes(max_n: int) -> Generator[int]: |