-
- Notifications
You must be signed in to change notification settings - Fork 48.9k
Add optimized Sieve of Eratosthenes algorithm #13617
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
Closed
Naman-Vasudev wants to merge 8 commits into TheAlgorithms:master from Naman-Vasudev:sieve_of_erastosthenes
+70 −0
Closed
Changes from 5 commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
6506531 Create sieve_of_eratosthenes.py
Naman-Vasudev e33cfc2 Update sieve_of_eratosthenes.py
Naman-Vasudev aceb903 Update sieve_of_eratosthenes.py
Naman-Vasudev f16e35e Update sieve_of_eratosthenes.py
Naman-Vasudev eb55817 Add optimized Sieve of Eratosthenes algorithm
Naman-Vasudev 40608c9 Update optimised_sieve_of_eratosthenes.py
Naman-Vasudev c9baa24 Update optimised_sieve_of_eratosthenes.py
Naman-Vasudev d876146 Refactor optimized_sieve function for clarity
Naman-Vasudev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Optimized Sieve of Eratosthenes: An efficient algorithm to compute all prime numbers up to n. | ||
| # This version skips even numbers after 2, improving both memory and time usage. | ||
| # It is particularly efficient for larger n (e.g., up to 10**8 on typical hardware). | ||
| # Wikipedia URL - https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | ||
| | ||
| from math import isqrt | ||
| | ||
| def optimized_sieve(n: int) -> list[int]: | ||
| """ | ||
| Compute all prime numbers up to and including n using an optimized Sieve of Eratosthenes. | ||
| | ||
| This implementation skips even numbers after 2 to reduce memory and runtime by about 50%. | ||
| | ||
| Parameters | ||
| ---------- | ||
| n : int | ||
| Upper bound (inclusive) of the range in which to find prime numbers. | ||
| Expected to be a non-negative integer. If n < 2 the function returns an empty list. | ||
| | ||
| Returns | ||
| ------- | ||
| list[int] | ||
| A list of primes in ascending order that are <= n. | ||
| | ||
| Examples | ||
| -------- | ||
| >>> optimized_sieve(10) | ||
| [2, 3, 5, 7] | ||
| >>> optimized_sieve(1) | ||
| [] | ||
| >>> optimized_sieve(2) | ||
| [2] | ||
| >>> optimized_sieve(30) | ||
| [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] | ||
| """ | ||
| if n < 2: | ||
| return [] | ||
| | ||
| # Handle 2 separately, then consider only odd numbers | ||
| primes = [2] if n >= 2 else [] | ||
| | ||
| # Only odd numbers from 3 to n | ||
| size = (n - 1) // 2 | ||
| is_prime = [True] * (size + 1) | ||
| limit = isqrt(n) | ||
| | ||
| for i in range((limit - 1) // 2 + 1): | ||
| if is_prime[i]: | ||
| p = 2 * i + 3 | ||
| # Start marking from p^2, converted to index | ||
| start = (p * p - 3) // 2 | ||
| for j in range(start, size + 1, p): | ||
| is_prime[j] = False | ||
| | ||
| primes.extend(2 * i + 3 for i in range(size + 1) if is_prime[i]) | ||
| return primes | ||
| | ||
| | ||
| if __name__ == "__main__": | ||
| | ||
| print(optimized_sieve(50)) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
n