Skip to content

Commit

Permalink
Lazier sieve
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger committed Sep 9, 2023
1 parent 75cd865 commit d91c433
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down

0 comments on commit d91c433

Please sign in to comment.