Builtin prime table up to 20 bits; ulong_extras cleanup#2548
Merged
fredrik-johansson merged 3 commits intoflintlib:mainfrom Jan 12, 2026
Merged
Builtin prime table up to 20 bits; ulong_extras cleanup#2548fredrik-johansson merged 3 commits intoflintlib:mainfrom
ulong_extras cleanup#2548fredrik-johansson merged 3 commits intoflintlib:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The static bit table of odd primes up to 2^15 is complemented with a static bit table mod 30 up to 2^20 (costing 35 KB), making
n_is_primenearly instantaneous up to 20 bits. We also search these tables directly inn_nextprime, significantly speeding up generating primes up to one million.Timings for
n_is_prime, average for 10^5 random inputs of the given bit size:Timings generating the primes up to 10^3 sequentially using repeated calls to
n_nextprime:Timings generating the primes up to 10^6:
For comparison, repeatedly calling
n_primes_nexttakes3.92e-07seconds up to 10^3 (where it's just copying from the small primes table) and0.00115seconds up to 10^6 (where it's sieving). Obviously,n_primes_nextwould benefit significantly from using the same tables asn_is_primeandn_nextprime, but I'm not going to touch that function for now (it wants other optimizations too).This PR also removes the obsolete
n_is_oddprime_smallandn_is_oddprime_binaryfunctions and replaces the obsoleten_is_probabprimewith a wrapper ofn_is_prime.