Skip to content

Improve sieve of eratosthenes and remove duplicate #5043

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
wants to merge 2 commits into from
Closed

Improve sieve of eratosthenes and remove duplicate #5043

wants to merge 2 commits into from

Conversation

matssson
Copy link

@matssson matssson commented Oct 5, 2021

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

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • [] All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@matssson matssson requested a review from Kush1101 as a code owner October 5, 2021 17:12
@ghost ghost added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 5, 2021
Comment on lines +39 to +42
# Do even numbers separately
primes = [2]
for i in range(4, num + 1, 2):
sieve[i] = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the same algorithm, but since 2 is the only even prime, you can check it seperately and remove unnecessary tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest stick to Wiki.

Comment on lines +44 to +45
p = 3
while p * p <= num:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use range here.

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these tests to the existing file.


return prime
return primes


if __name__ == "__main__":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if __name__ == "__main__":
if __name__ == "__main__":
import doctest
doctest.testmod()

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants