Uh oh!
There was an error while loading. Please reload this page.
improved prime number generator to check only up to sqrt(n) instead of n - #1984
Conversation
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
commented
May 14, 2020
|
steven7214
commented
May 14, 2020
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? |
Uh oh!
There was an error while loading. Please reload this page.
cclauss
left a comment
There was a problem hiding this comment.
Looks good to me but the performance gain is not impressive.
TheSuperNoob
commented
May 14, 2020
steven7214
commented
May 14, 2020
@TheSuperNoob How exactly are you testing them? Could you update the benchmark to better represent the performance gain? |
…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>

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.
Checklist:
Fixes: #{$ISSUE_NO}.