]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Improve the sieve() recipe in the itertools docs (gh-109199)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Sat, 9 Sep 2023 22:50:04 +0000 (17:50 -0500)
committerGitHub <noreply@github.com>
Sat, 9 Sep 2023 22:50:04 +0000 (17:50 -0500)
Lazier sieve

Doc/library/itertools.rst

index 42243715c2d93bf1cab7203df145e1a07251739f..3cfc2602fe0694d01021f1cbbb401eecb2d14c7a 100644 (file)
@@ -1030,13 +1030,16 @@ The following recipes have a more mathematical flavor:
    def sieve(n):
        "Primes less than n."
        # sieve(30) --> 2 3 5 7 11 13 17 19 23 29
+       if n > 2:
+           yield 2
+       start = 3
        data = bytearray((0, 1)) * (n // 2)
-       data[:3] = 0, 0, 0
        limit = math.isqrt(n) + 1
-       for p in compress(range(limit), data):
+       for p in iter_index(data, 1, start, limit):
+           yield from iter_index(data, 1, start, p*p)
            data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
-       data[2] = 1
-       return iter_index(data, 1) if n > 2 else iter([])
+           start = p*p
+       yield from iter_index(data, 1, start)
 
    def factor(n):
        "Prime factors of n."