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 n.
The method by size#
| Range of n | What n_is_prime does |
|---|---|
| n < 2^{15} | Looks up odd n in a bit table. |
| all larger n | Trial division by 2, 3 and 5. |
| 2^{15} \le n < 2^{20} | Looks up n in a table indexed modulo 30. |
| 2^{20} \le n | Trial division by the 32 primes from 7 to 149. |
| 2^{20} \le n < 2^{32} | Strong probable prime test to base 2 in 32-bit arithmetic, then a hash lookup among the 2314 base-2 strong pseudoprimes below 2^{32}. |
| 2^{32} \le n < 2^{64} | 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 2^{15} witnesses. |
All trial division uses the multiplication trick of Divisibility testing by multiplication, so it involves no division instructions. For n < 2^{32} 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 n > 2^{32} that pass the base-2 test, n_is_prime computes a 15-bit hash of n and reads a base a < 2^{16} 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 n = pq with q = k(p - 1) + 1 for k \in \{2, \ldots, 9, 12\}, for which reliable witnesses are rare. These are detected before the second test. Writing p = x + 1 gives n = (x + 1)(kx + 1), so for each k the candidate x = \lfloor \sqrt{n/k} \rfloor 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 2^{64} 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 2^{81}, with the double-word exponentiationsn_ll_small_2_powmodandn_ll_small_powmod_triplereduced with the two-word Barrett inverse \lfloor B^3/n \rfloor 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-10-20).
@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-10-20}
}