Skip to content

Unify O(sqrt(N)) is_prime functions under project_euler #6258

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

Merged

Conversation

ngiachou
Copy link
Contributor

@ngiachou ngiachou commented Jul 20, 2022

Describe your change:

I changed the implementation of is_prime functions inside project_euler in order to have a unified implementation. There are some cases where the solution uses the Eratosthenes' sieve method. In some cases there is no specific gain from using that method so I changed it to the O(sqrt(n)) algorithm, but in other cases the sieve method is used in the core structure of the solution, hence I did not touch those.

  • 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}.

fixes #5434

@ghost ghost added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Jul 20, 2022
@ghost ghost removed the tests are failing Do not merge until tests pass label Jul 20, 2022
@ngiachou
Copy link
Contributor Author

ngiachou commented Jul 20, 2022

I think it is better to remove the assertion in all together since it doesn't have actual value for the implementation of the primality test algorithm and maybe it is a bit of confusing even. Should I do that? @poyea

Edit: For example in project_euler/problem_003/sol1.py lines 41-43.

@poyea
Copy link
Member

poyea commented Jul 20, 2022

@elpaxoudis Yeah, I think they can be removed, and the negative / type check can be performed when processing input (if any). If we want to handle the negative / type check in the function, I think it makes sense to raise an exception / an assertion failure, instead of return False.

@poyea poyea changed the title fixes #5434 Unify is_prime under project_euler Jul 20, 2022
@poyea poyea changed the title Unify is_prime under project_euler Unify O(sqrt(N)) is_prime under project_euler Jul 20, 2022
@poyea poyea changed the title Unify O(sqrt(N)) is_prime under project_euler Unify O(sqrt(N)) is_prime functions under project_euler Jul 20, 2022
@poyea poyea self-assigned this Jul 20, 2022
@ghost ghost added the tests are failing Do not merge until tests pass label Jul 20, 2022
@ghost ghost removed the tests are failing Do not merge until tests pass label Jul 20, 2022
Copy link
Member

@poyea poyea left a comment

Choose a reason for hiding this comment

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

Thank you for your pull request!🤩

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Jul 27, 2022
@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Jul 29, 2022
@ngiachou
Copy link
Contributor Author

@poyea Is there any other changes I should make?

Copy link
Member

@poyea poyea left a comment

Choose a reason for hiding this comment

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

It's been a while and this looks good to me. 👍

@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Sep 14, 2022
@poyea poyea added awaiting reviews This PR is ready to be reviewed awaiting merge This PR is approved and ready to be merged and removed awaiting reviews This PR is ready to be reviewed labels Sep 14, 2022
@poyea poyea merged commit 2104fa7 into TheAlgorithms:master Sep 14, 2022
@algorithms-keeper algorithms-keeper bot removed the awaiting merge This PR is approved and ready to be merged label Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deduplicate repeated is_prime functions
2 participants