Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve the sieve() recipe in the itertools docs #109199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2023

Conversation

rhettinger
Copy link
Contributor

@rhettinger rhettinger commented Sep 9, 2023

The old version fully marked the entire primes table before yielding its first value. Now subsequences of primes are yielded as soon as the subsequence becomes fully marked. This improves responsiveness — the first call to next() returns more quickly. And since the work is done lazily, the factor() recipe saves work when small factors reduce the search space. For example factor(12**20) is now several times faster.

The old version did some perplexing manipulations to the data array, data[:3] = 0, 0, 0 and data[2] = 1. The new version is straight forward and has clean loop invariants:

  • data[start : p*p] has valid primes that have not yet been yielded
  • data[p*p : n] still needs to be marked for multiples of p.

The new version is easy experiment with and can be easily changedto the simplest version of the algorithm that isn't lazy and hasn't optimized away the even composites.

def sieve(n):
       start = 2
       data = bytearray([1]) * n
       limit = math.isqrt(n) + 1
       for p in iter_index(data, 1, start, limit):
           data[p*p : n : p] = bytes(len(range(p*p, n, p)))
       yield from iter_index(data, 1, start)

📚 Documentation preview 📚: https://cpython-previews--109199.org.readthedocs.build/

@rhettinger rhettinger added docs Documentation in the Doc dir skip issue skip news needs backport to 3.12 only security fixes labels Sep 9, 2023
@rhettinger rhettinger merged commit d3ed992 into python:main Sep 9, 2023
@miss-islington
Copy link
Contributor

Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 9, 2023
Lazier sieve
(cherry picked from commit d3ed992)

Co-authored-by: Raymond Hettinger <[email protected]>
@bedevere-bot
Copy link

GH-109203 is a backport of this pull request to the 3.12 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Sep 9, 2023
@rhettinger rhettinger deleted the responsive_sieve branch September 9, 2023 22:50
Yhg1s pushed a commit that referenced this pull request Sep 12, 2023
…109203)

Improve the sieve() recipe in the itertools docs (gh-109199)

Lazier sieve
(cherry picked from commit d3ed992)

Co-authored-by: Raymond Hettinger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants