Skip to content
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

improved prime number generator to check only up to sqrt(n) instead of n #1984

Merged
merged 5 commits into from
May 14, 2020

Conversation

steven7214
Copy link
Contributor

@steven7214 steven7214 commented May 14, 2020

Describe your change:

The old algorithm checked to see if a number, n, was divisible by any number 2 - (n-1), but by pigeon hole principle it's only necessary to check up to sqrt(n). I changed the algorithm so it wouldn't do unnecessary checks.

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

@cclauss
Copy link
Member

cclauss commented May 14, 2020

This is cool! Can we rename the current function slow_primes() and then name the new one primes() and then we can land this pull request. Then I will create a benchmark (or you can if you want) to demonstrate how much of an improvement this is. Thanks!

@cclauss
Copy link
Member

cclauss commented May 14, 2020

./maths/prime_numbers.py:26:34: E226 missing whitespace around arithmetic operator
        bound = int(math.sqrt(i))+1
                                 ^
1     E226 missing whitespace around arithmetic operator

@steven7214
Copy link
Contributor Author

Ok, I've added the previous code in a separate function called slow_primes(). You'll notice that the code between the two is very similar; the only difference is that the search has been restricted in the faster method. Is this what you were looking for?

maths/prime_numbers.py Outdated Show resolved Hide resolved
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Looks good to me but the performance gain is not impressive.

@cclauss cclauss merged commit c8fbdee into TheAlgorithms:master May 14, 2020
@TheSuperNoob
Copy link
Contributor

These tests do not work as intended. The primes and slow_primes are generators and will need to be looped through fully in order to time them properly.

Testing the old compared to the new properly you can see that the performance gain is quite significant.

Screenshot 2020-05-15 at 00 57 56

@steven7214
Copy link
Contributor Author

@TheSuperNoob How exactly are you testing them? Could you update the benchmark to better represent the performance gain?

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…f n (TheAlgorithms#1984)

* improved prime number generator to check only up to sqrt(n) instead of n

* added old version as slow_primes() and named new, faster version primes()

* fixed docstring in slow_primes

* Add a timeit benchmark

* Update prime_numbers.py

Co-authored-by: Christian Clauss <cclauss@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants