Repository navigation
maths: add Sieve of Sundaram #15508
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
base: master
Are you sure you want to change the base?
maths: add Sieve of Sundaram #15508
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """ | ||
| Sieve of Sundaram algorithm for finding prime numbers up to a given limit. | ||
|
|
||
| The Sieve of Sundaram generates prime numbers by eliminating integers | ||
| of the form i + j + 2ij. | ||
|
|
||
| Time Complexity: O(n log n) | ||
| Space Complexity: O(n) | ||
|
|
||
| Reference: https://en.wikipedia.org/wiki/Sieve_of_Sundaram | ||
| """ | ||
|
|
||
|
|
||
| def sieve_of_sundaram(limit: int) -> list[int]: | ||
| """ | ||
| Generate all prime numbers up to a given limit. | ||
|
|
||
| Args: | ||
| limit: Upper bound for finding primes (inclusive). | ||
|
|
||
| Returns: | ||
| List of prime numbers up to the given limit. | ||
|
|
||
| Raises: | ||
| ValueError: If limit is negative. | ||
|
|
||
| Examples: | ||
| >>> sieve_of_sundaram(20) | ||
| [2, 3, 5, 7, 11, 13, 17, 19] | ||
| >>> sieve_of_sundaram(10) | ||
| [2, 3, 5, 7] | ||
| >>> sieve_of_sundaram(2) | ||
| [2] | ||
| >>> sieve_of_sundaram(1) | ||
| [] | ||
| >>> sieve_of_sundaram(0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a few more doctests?
|
||
| [] | ||
| >>> sieve_of_sundaram(-1) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: -1: Invalid input, please enter a non-negative integer. | ||
| """ | ||
| if limit < 0: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-integer input such as sieve_of_sundaram(10.5) currently fails with "can't multiply sequence by non-int of type 'float'", which isn't very helpful. Since the type hint says int, either leave it, or reject non-integers up front with a clearer TypeError and add a doctest for it. |
||
| msg = f"{limit}: Invalid input, please enter a non-negative integer." | ||
| raise ValueError(msg) | ||
|
|
||
| if limit < 2: | ||
| return [] | ||
|
|
||
| upper_bound = (limit - 1) // 2 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A short comment here would help readers new to this sieve: each index k stands for the odd number 2k + 1, and k is marked when 2k + 1 is composite. That explains both the (limit - 1) // 2 bound and the 2 * value + 1 on line 63. A clearer name than upper_bound (e.g. max_index) would also help. |
||
| marked = [False] * (upper_bound + 1) | ||
|
|
||
| for i in range(1, upper_bound + 1): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: only values of i with 2 * i * (i + 1) <= upper_bound mark anything, so the outer loop does nothing for most of its range. The inner marking can also be a slice assignment, which matched the current output for every limit from 0 to 3000 in my check and was about 10x faster at limit 10**6: Feel free to keep the explicit loop if you prefer clarity, since it reads closer to the textbook formula. |
||
| j = i | ||
| while i + j + 2 * i * j <= upper_bound: | ||
| marked[i + j + 2 * i * j] = True | ||
| j += 1 | ||
|
|
||
| primes = [2] | ||
|
|
||
| for value in range(1, upper_bound + 1): | ||
| if not marked[value]: | ||
| prime = 2 * value + 1 | ||
| if prime <= limit: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition can never be false. The largest candidate is 2 * upper_bound + 1, and upper_bound = (limit - 1) // 2, so it is at most limit. I checked every limit from 0 to 2999 and the condition was always true. You can drop it and simplify lines 61-65 to |
||
| primes.append(prime) | ||
|
|
||
| return primes | ||
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 define n here (it is the limit). It would also help to say that this is slower than the Sieve of Eratosthenes (O(n log log n)). Also consider a "see also" note pointing at the existing sieve in maths/, so readers can compare.