FLINT3.7.0-dev
Overview

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 nWhat n_is_prime does
n < 2^{15}Looks up odd n in a bit table.
all larger nTrial division by 2, 3 and 5.
2^{15} \le n < 2^{20}Looks up n in a table indexed modulo 30.
2^{20} \le nTrial 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:

Referenced from#

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
Cite this page

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).