Repository navigation
maths: add Sieve of Sundaram - #15508
peeyushtiwary27 wants to merge 1 commit into
Conversation
jgohel9902
left a comment
There was a problem hiding this comment.
Thanks for the contribution. The implementation is correct: I compared it to trial division for limits up to 3000 and found no differences. A few small suggestions inline: a redundant condition, one clarifying comment, extra doctests and clearer complexity wording.
| 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.
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.extend(2 * value + 1 for value in range(1, upper_bound + 1) if not marked[value]), or keep the loop without the check.
| if limit < 2: | ||
| return [] | ||
|
|
||
| upper_bound = (limit - 1) // 2 |
There was a problem hiding this comment.
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.
| upper_bound = (limit - 1) // 2 | ||
| marked = [False] * (upper_bound + 1) | ||
|
|
||
| for i in range(1, upper_bound + 1): |
There was a problem hiding this comment.
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:
i = 1
while 2 * i * (i + 1) <= upper_bound:
start, step = 2 * i * (i + 1), 2 * i + 1
marked[start::step] = [True] * len(range(start, upper_bound + 1, step))
i += 1
Feel free to keep the explicit loop if you prefer clarity, since it reads closer to the textbook formula.
| [2] | ||
| >>> sieve_of_sundaram(1) | ||
| [] | ||
| >>> sieve_of_sundaram(0) |
There was a problem hiding this comment.
Could you add a few more doctests?
- sieve_of_sundaram(3) -> [2, 3] and sieve_of_sundaram(4) -> [2, 3], to cover the smallest limits and an even limit
- len(sieve_of_sundaram(100)) -> 25, to check a larger input
- a cross-check against trial division for a range of limits, which would document that the sieve is correct, not only that a few outputs match
| ... | ||
| ValueError: -1: Invalid input, please enter a non-negative integer. | ||
| """ | ||
| if limit < 0: |
There was a problem hiding this comment.
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.
| The Sieve of Sundaram generates prime numbers by eliminating integers | ||
| of the form i + j + 2ij. | ||
|
|
||
| Time Complexity: O(n log n) |
There was a problem hiding this comment.
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.
Describe your change
Checklist