Return a list of all primes numbers up to max. >>> list(fast_primes(0)) [] >>> list(fast_primes(-1)) [] >>> list(fast_primes(-10)) [] >>> list(fast_primes(25)) [2, 3, 5, 7, 11, 13, 17, 19, 23] >>> list(fast_primes(11)) [2, 3, 5, 7, 11] >>> list(fast_p
(max_n: int)
| 59 | |
| 60 | |
| 61 | def fast_primes(max_n: int) -> Generator[int]: |
| 62 | """ |
| 63 | Return a list of all primes numbers up to max. |
| 64 | >>> list(fast_primes(0)) |
| 65 | [] |
| 66 | >>> list(fast_primes(-1)) |
| 67 | [] |
| 68 | >>> list(fast_primes(-10)) |
| 69 | [] |
| 70 | >>> list(fast_primes(25)) |
| 71 | [2, 3, 5, 7, 11, 13, 17, 19, 23] |
| 72 | >>> list(fast_primes(11)) |
| 73 | [2, 3, 5, 7, 11] |
| 74 | >>> list(fast_primes(33)) |
| 75 | [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] |
| 76 | >>> list(fast_primes(1000))[-1] |
| 77 | 997 |
| 78 | """ |
| 79 | numbers: Generator = (i for i in range(1, (max_n + 1), 2)) |
| 80 | # It's useless to test even numbers as they will not be prime |
| 81 | if max_n > 2: |
| 82 | yield 2 # Because 2 will not be tested, it's necessary to yield it now |
| 83 | for i in (n for n in numbers if n > 1): |
| 84 | bound = int(math.sqrt(i)) + 1 |
| 85 | for j in range(3, bound, 2): |
| 86 | # As we removed the even numbers, we don't need them now |
| 87 | if (i % j) == 0: |
| 88 | break |
| 89 | else: |
| 90 | yield i |
| 91 | |
| 92 | |
| 93 | def benchmark(): |
nothing calls this directly
no outgoing calls
no test coverage detected