-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Improve isprime
performance for 32-bit integers
#16349
Conversation
Is |
Just for reference, the time it takes to check that all 32-bit the primes are primes is
Also, checking whether or not each element of an array of 10^7 random
|
As far as i can see, none of the functions in |
for m in (3,5,7,11,13,17,19,23) | ||
n % m == 0 && return false | ||
end | ||
n < 841 && return n > 1 | ||
s = trailing_zeros(n-1) | ||
d = (n-1) >>> s | ||
for a in witnesses(n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can give a type hint
for a in witnesses(n)::Tuple{Vararg{Int}}
making the witnesses stack allocated.
This should now be moved to https://github.com/JuliaMath/Primes.jl @pabloferz Great work, thanks for this. |
No problem. Moved over JuliaMath/Primes.jl#9 |
As discussed here #11594 (mainly by @VicDrastik and @danaj).
isprime
has room for improvement. In particular this PR takes care only of the case of integers < 2^32. A version for 64-bit integers would require another strategy and should be done in another PR.This version is much faster and uses less memory than the current implementation.
There is another possibility that was mentioned also in #11594, that is to use the algorithm due to Forišek and Jančina (which can be found here http://ceur-ws.org/Vol-1326/020-Forisek.pdf). @VicDrastik didn't seem to be fond of that solution but it is certainly faster than the one proposed here (although it uses a little more of memory 512 bytes, but we were already pre computing al primes up to 2^16, so this doesn't seem like much more.
I leave the code for that option below in case someone wants to consider it.