Improve sieve of eratosthenes and remove duplicate#5043
Closed
matssson wants to merge 2 commits intoTheAlgorithms:masterfrom
matssson:master
Closed
Improve sieve of eratosthenes and remove duplicate#5043matssson wants to merge 2 commits intoTheAlgorithms:masterfrom matssson:master
matssson wants to merge 2 commits intoTheAlgorithms:masterfrom
matssson:master
Conversation
14 tasks
poyea
reviewed
Oct 6, 2021
Comment on lines
+39
to
+42
| # Do even numbers separately | ||
| primes = [2] | ||
| for i in range(4, num + 1, 2): | ||
| sieve[i] = False |
Member
There was a problem hiding this comment.
I think we don't want to optimize it like this to keep its original algorithm. https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm_and_variants
Author
There was a problem hiding this comment.
It is the same algorithm, but since 2 is the only even prime, you can check it seperately and remove unnecessary tests
poyea
reviewed
Oct 6, 2021
Comment on lines
+44
to
+45
| p = 3 | ||
| while p * p <= num: |
poyea
reviewed
Oct 6, 2021
Comment on lines
-21
to
-24
| >>> prime_sieve_eratosthenes(10) | ||
| 2,3,5,7, | ||
| >>> prime_sieve_eratosthenes(20) | ||
| 2,3,5,7,11,13,17,19, |
Member
There was a problem hiding this comment.
Move these tests to the existing file.
poyea
reviewed
Oct 6, 2021
| return primes | ||
|
|
||
|
|
||
| if __name__ == "__main__": |
Member
There was a problem hiding this comment.
Suggested change
| if __name__ == "__main__": | |
| if __name__ == "__main__": | |
| import doctest | |
| doctest.testmod() |
This pull request was closed.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe your change:
I removed a duplicate of sieve of eratosthenes in favour of the older one, and improved the older one by iterating by 2 elements each time and removing the math dependency
Checklist:
Fixes: #{$ISSUE_NO}.