Lably
Skip to content

Baillie–PSW primality test

Probabilistic primality testing algorithm

  • 9 min read
  • 4 chapters

Related on Lably

Through Primality test, Composite number and Prime number

The Baillie–PSW test is a combination of a strong Fermat probable prime test to base 2 and a standard or strong Lucas probable prime test. The Fermat and Lucas test each have their own list of pseudoprimes, that is, composite numbers that pass the test. For example, the first ten strong pseudoprimes to base 2 are

2047, 3277, 4033, 4681, 8321, 15841, 29341, 42799, 49141, and 52633 (sequence A001262 in the OEIS).

The first ten strong Lucas pseudoprimes (with Lucas parameters (P, Q) defined by Selfridge's Method A) are

5459, 5777, 10877, 16109, 18971, 22499, 24569, 25199, 40309, and 58519 (sequence A217255 in the OEIS).

There is no known overlap between these lists, and there is even evidence that the numbers tend to be of different kind, in fact even with standard and not strong Lucas test there is no known overlap. For example, Fermat pseudoprimes to base 2 tend to fall into the residue class 1 (mod m) for many small m, whereas Lucas pseudoprimes tend to fall into the residue class −1 (mod m). As a result, a number that passes both a strong Fermat base 2 and a strong Lucas test is very likely to be prime. If a random base is chosen, there might be some composite n that passes both the Fermat and Lucas tests. For example, n=5777 is a strong psp base 76, and is also a strong Lucas pseudoprime.

No composite number below 264 (approximately 1.845·1019) passes the strong or standard Baillie–PSW test; this result was also separately verified by Charles Greathouse in June 2011. Consequently, this test is a deterministic primality test on numbers below 264. There are also no known composite numbers above that bound that pass the test; in other words, there are no known Baillie–PSW pseudoprimes.

In 1980, the authors Pomerance, Selfridge, and Wagstaff offered $30 for the discovery of a counterexample, that is, a composite number that passed this test, or the production of a proof of this test's correctness. A heuristic argument by Pomerance suggests that there are infinitely many counterexamples. There is also a $2000 bounty on a strengthened version of this test.

The test

Let n be the odd positive integer that we wish to test for primality.

  1. Optionally, perform trial division to check if n is divisible by a small prime number less than some convenient limit.
  2. Perform a base 2 strong probable prime test. If n is not a strong probable prime base 2, then n is composite; quit.
  3. Find the first D in the sequence 5, −7, 9, −11, 13, −15, ... for which the Jacobi symbol (D/n) is −1. Set P = 1 and Q = (1 − D) / 4 (method A).
    • If D = 5, the choice should be modified to P = Q = 5 instead of P = 1, Q = -1 for better strength (method A*).
  4. Perform a strong Lucas probable prime test on n using parameters D, P, and Q. If n is not a strong Lucas probable prime, then n is composite. Otherwise, n is almost certainly prime.

Remarks.

  1. The first step is for efficiency only. The Baillie–PSW test works without this step, but if n has small prime factors, then the quickest way to show that n is composite is to find a factor by trial division.
  2. Step 2 is, in effect, a single application of the Miller–Rabin primality test, but using the fixed base 2. There is nothing special about using base 2; pseudoprimes to different bases seem to have the same growth rate, so other bases might work just as well. However, much work has been done (see above) to verify that the combination of the base 2 strong probable prime test and a strong Lucas test does a good job of distinguishing primes from composites.
  3. Baillie and Wagstaff proved in Theorem 9 on page 1413 of that the average number of Ds that must be tried is about 3.147755149.
  4. If n is a perfect square, then step 3 will never yield a D with (D/n) = −1; this is not a problem because perfect squares are easy to detect using Newton's method for square roots. If step 3 fails to produce a D quickly, one should check whether n is a perfect square.
  5. Given n, there are other methods for choosing D, P, and Q. What is important is that the Jacobi symbol (D/n) be −1. Bressoud and Wagon explain why we want the Jacobi symbol to be −1, as well as why one gets more powerful primality tests if Q ≠ ±1.

Other changes

Section 6 of Baillie and Wagstaff (1980) and Baillie, Fiori, and Wagstaff (2021) recommend that a strengthened primality test should check two additional congruence conditions:

  1. Vn+1≡2Q(modn), testing that n is a Lucas-V probable prime
  2. Q(n+1)/2=Q⋅Q(n−1)/2≡Q⋅(Qn)(modn), an Euler–Jacobi probable prime in base Q.

These two congruences involve almost no extra computational cost, and are only rarely true if n is composite. Using the A* method for picking D, P, and Q, there are only five odd, composite numbers (also called Dickson pseudoprimes of the second kind) less than 1015 for which Vn+1≡2Q(modn). Baillie, Fiori, and Wagstaff (2021) offers a $2000 reward for either correctness or a counter example on the test with these two congruence conditions.

Di Iorio (2025) proposes a slight simplification of the strong probable prime part of the test by exploiting the fixed base of 2 used by the test.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Topics in mathematics