Primality testing for word-size integers
Every primality test in ulong_extras that answers yes or no is deterministic: for word-size integers, probable primality and primality coincide because the relevant pseudoprimes have been enumerated. The default function n_is_prime chooses its method by the size of .
The method by size#
| Range of | What n_is_prime does |
|---|---|
| Looks up odd in a bit table. | |
| all larger | Trial division by 2, 3 and 5. |
| Looks up in a table indexed modulo 30. | |
| Trial division by the 32 primes from 7 to 149. | |
| Strong probable prime test to base 2 in 32-bit arithmetic, then a hash lookup among the 2314 base-2 strong pseudoprimes below . | |
| Base-2 test in Montgomery arithmetic, a check for one special form of semiprime, and a second test with a base taken from a table of witnesses. |
All trial division uses the multiplication trick of Divisibility testing by multiplication, so it involves no division instructions. For the 32 divisibility tests have no branches and are vectorised by the compiler.
AlgorithmStrong probable prime testThe Miller–Rabin test for a single base. Every odd prime passes it, at most a quarter of the bases let a composite pass, and with tabulated pseudoprimes it becomes a proof of primality for word-size integers.Choosing the second base#
For that pass the base-2 test, n_is_prime computes a 15-bit hash of and reads a base from a table of 32768 entries (64 KiB). The table was built from the Feitsma–Galway list of base-2 strong pseudoprimes [FeiGal2013] so that, in each hash bucket, the stored base exposes every pseudoprime in that bucket. This follows the approach of Forišek and Jančina [ForJan2015], whose table is eight times larger.
One family of pseudoprimes makes this hard: semiprimes with for , for which reliable witnesses are rare. These are detected before the second test. Writing gives , so for each the candidate is computed from a single floating-point square root and checked. This costs about a twentieth of a Fermat test.
The correctness of the whole procedure up to can be verified with the program examples/check_n_is_prime, which runs n_is_prime on every entry of the Feitsma–Galway table.
Other tests#
The other tests in the module are kept for comparison, for other applications, or as certificates:
n_is_probabprime_BPSW(Baillie–PSW [PSW1980; BaiWag1980]) has no known counterexample at any size;n_is_prime_pocklington[Poc1914; BLS1975] andn_is_prime_pseudosquare[LukPatWil1996] produce proofs that do not depend on tables;n_ll_is_primehandles two-word integers, deterministically below about , with the double-word exponentiationsn_ll_small_2_powmodandn_ll_small_powmod_triplereduced with the two-word Barrett inverse computed byn_ll_small_preinv.
Referenced from#
- Primes and number theory (overview)
- Primality proving: APRCL and ECPP (algorithm)
- Strong probable prime test (algorithm)
- What FLINT guarantees (overview)
- ulong_extras.h (module)
- Deterministic primality test
References#
- [FeiGal2013] J. Feitsma and W. Galway, "Tables of pseudoprimes and related data", http://www.cecm.sfu.ca/Pseudoprimes/index-2-to-64.html
- [ForJan2015] M. Forisek and J. Jancina, "Fast Primality Testing for Integers That Fit into a Machine Word", SOFSEM 2015, https://ceur-ws.org/Vol-1326/020-Forisek.pdf
- [PSW1980] C. Pomerance, J. L. Selfridge and S. S. Wagstaff, Jr., "The pseudoprimes to 25·10^9", Mathematics of Computation 35 (1980), 1003–1026
- [BaiWag1980] Robert Baillie; Samuel S. Wagstaff, Jr. (October 1980). "Lucas Pseudoprimes". Mathematics of Computation. 35 (152): 1391–1417.
- [Poc1914] H. C. Pocklington, "The determination of the prime or composite nature of large numbers by Fermat's theorem", Proceedings of the Cambridge Philosophical Society 18 (1914), 29–30
- [BLS1975] J. Brillhart, D. H. Lehmer and J. L. Selfridge, "New primality criteria and factorizations of 2^m ± 1", Mathematics of Computation 29 (1975), 620–647
- [LukPatWil1996] R. F. Lukes and C. D. Patterson and H. C. Williams "Some results on pseudosquares" Math. Comp. 1996, no. 65, 361–372
The FLINT team, “Primality testing for word-size integers,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/number-theory/primality-word.html (generated 2026-11-02).
@misc{flint-doc-algorithms-number-theory-primality-word,
author = {{The FLINT team}},
title = {{Primality testing for word-size integers}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/number-theory/primality-word.html},
note = {Generated 2026-11-02}
}