The Baillie-PSW Primality Test (2024)

The Baillie-PSW primality test

Thomas R. Nicely

http://www.trnicely.net
Current e-mail address


Freeware copyright (c) 2012 Thomas R. Nicely. Released into the publicdomain by the author, who disclaims any legal liability arising fromits use.

Last updated 0700 GMT 13 January 2012. Originally posted 10 June 2005.

  • Bibliography

The Baillie-PSW (BPSW or BSW) primality test is actually a compositenesstest, in the manner of Fermat's test and the Miller-Rabin test. It is namedfor Robert Baillie, Carl Pomerance, John L. Selfridge, and Samuel S.Wagstaff, Jr. The algorithm was apparently first conceived by Baillie(Baillie and Wagstaff, 1980), with refinements added by Selfridge (Pomerance,Selfridge, and Wagstaff, 1980). The procedure is as follows; note that somedetails may vary from one implementation to another (Martin, 2004).

  • Process all N < 3 and all even N.
  • Check N for any small prime divisors p < 1000.
  • Perform a Miller-Rabin (strong probable prime) test, base 2, on N.
  • Perform a (standard or strong) Lucas-Selfridge test on N, using Lucas sequences with the parameters suggested by Selfridge.
At any stage of this procedure, N may be exposed as definitely composite.If not, it has passed the (standard or strong) BPSW test, and is eitherprime or a (standard or strong) BPSW pseudoprime.

As of this date, both the standard and strong tests are flawless; nocounterexample (BPSW standard or strong pseudoprime) is known.With the aid of Richard G. E. Pinch's table of base-2 strong pseudoprimes,the author verified (May, 2005) that no BPSW pseudoprime (standardor strong) exists for N < 10^13. In January, 2007, with the aid ofthe author's code andWilliam Galway's tableof pseudoprimes, Martin Fuller determined that no BPSWpseudoprime (standard or strong) exists for N < 10^15. More recently,JeffGilchrist, using the author's code anda database ofpseudoprimes prepared by Jan Feitsma, has verified (13 June 2009)that no BPSW pseudoprime (standard or strong) exists below 10^17.Furthermore, analysis (24 October 2009) by Gilchrist of an extension ofFeitsma's database to 2^64 found no BPSW pseudoprime (standard orstrong) below 2^64 (approximately 1.8446744e19). This lower bound of 2^64for any BPSW pseudoprime has since been separately verified (11 July 2011)by Charles Greathouse; however, he also used Feitsma's database, so acompletely independent check has not yet been carried out.

Note, however, that Carl Pomerance (1984) presented a heuristic argumentthat an infinite number of counterexamples exist to the standard test(and presumably to the strong test as well, based on similar reasoning),and even that (for sufficiently large x, dependent on µ) the numberof BPSW pseudoprimes < x exceeds x^(1-µ), where µ isan arbitrarily small pre-assigned positive number. Nevertheless, not asingle BPSW pseudoprime has yet been found. Consequently, theBPSW test carries an aura of dependability (justified or not)exceeding that of competing algorithms, such as multiple Miller-Rabintests.

It is believed that some commercial mathematics software packagesrely, in part or in whole, on the BPSW test for primalitychecking; see, for example, Ribenboim (1995/6, p. 142), alsoMartin (2004). In some instances, these packages appear to use thestrong BPSW and/or Lucas test, or add additional Miller-Rabin tests.

BPSW requires O((log n)^3) bit operations, as do Fermat'stest and the Miller-Rabin algorithm. However, a BPSW testtypically requires roughly three to seven times as many bitoperations as a single Miller-Rabin test. The strong version ofBPSW differs only in replacing the standard Lucas-Selfridge testwith the strong Lucas-Selfridge test. The strong Lucas-Selfridgetest produces only roughly 30 % as many pseudoprimes as thestandard version; for example, among the odd composites N < 10^6,there are 219 standard Lucas-Selfridge pseudoprimes, 58 strongLucas-Selfridge pseudoprimes, and 46 base-2 strong pseudoprimes(these totals presume no screening with odd trial divisors).Since the strong Lucas-Selfridge test incurs roughly 10 % morerunning time, the strong tests appear to be more effective.

Selfridge specified the following parameters for the generationof the Lucas sequences: P = 1 and Q = (1 - D)/4, where D is thefirst integer in the sequence {5, -7, 9, -11, 13, -15, ...} forwhich GCD(D,N)=1 and the Jacobi symbol (D|N) = -1. Note, however,that if N is a perfect square, no such D will exist, and thesearch for D would continue all the way to ±sqrt(N), at whichpoint GCD(D,N)=sqrt(N) would expose N as composite. Consequently,the algorithm also presumes the presence of a preliminary checkfor perfect squares (as well as even integers and integers < 3).

Additional conditions sometimes imposed (Riesel, 1994, p. 130),that N not divide Q and that D be square-free, were found to beirrelevant to this application of Lucas sequences.

There remains the theoretical concern that, for very large N,the required D would be so large in magnitude that the algorithmwould become computationally useless. Given the exclusion ofperfect square values of N, no such behavior has been observedin practice; indeed, the range of values observed for D is quitesmall---|D|max=47 for N < 10^6, |D|max=67 for10^19 < N < 10^19 + 10^6. Baillie and Wagstaff (1980) presentedanalytical arguments to support this observation (Ribenboim, 1995/6,p. 142). However, the eventuality cannot be entirely excluded, andthe author's code includes an overflow trap for D.

The author has translated the algorithm into GNU C, employing the GMPlibrary, with a command-line executable compiled for the DOS/Winteloperating environment; see the accompanyingzipfile, which includes the source codes andWintel/DOS executables. To examine the complete library source modulesor to recompile the codes, you will also need to download the supportfiles trn.c and trn.h from trn.zip. Theillustrative main routine computes pi(x) between specified bounds, andenumerates the corresponding pseudoprimes produced by various primalitytests, including the standard and strong BPSW tests (none foundto date), the standard, strong, and extra strong Lucas tests, and variousothers, including the Fermat and Miller-Rabin tests (base 2) as well asthe GMP mpz_probab_prime_p function (seeGNU GMP mpz_probab_prime_p pseudoprimes).

Code is also included in trn.c for the "extra strong" Lucastest, as developed by Zhaiyu Mo and James P. Jones (circa 1997),and described by Jon Grantham (1998). However, I have not employed theextra strong Lucas test in the BPSW test, as the Lucas sequenceparameters are inconsistent with those of the Lucas-Selfridge tests;consequently, the extra strong Lucas pseudoprimes were not found to bedisjoint from those of the Miller-Rabin test with base 2 (or any othersingle Miller-Rabin base employed). This behavior was observed for allbases employed for the extra strong Lucas test. For a more detaileddiscussion, see the documentation in the module iExtraStrongLucas inthe support file trn.c included in trn.zip.

I would appreciate notification of any significant errors found in thisdocument, the codes, or their results.

BIBLIOGRAPHY

  • Source code and executable for BPSW
  • Support library modules trn.c and trn.h
  • GNU GMP mpz_probab_prime_p pseudoprimes
  • Home page

The Baillie-PSW Primality Test (2024)

References

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 5564

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.