Jump to content

Primality test

From Wikipedia, the free encyclopedia

Aprimality testis analgorithmfor determining whether an input number isprime.Among other fields ofmathematics,it is used forcryptography.Unlikeinteger factorization,primality tests do not generally giveprime factors,only stating whether the input number is prime or not. Factorization is thought to be a computationally difficult problem, whereas primality testing is comparatively easy (itsrunning timeispolynomialin the size of the input). Some primality tests prove that a number is prime, while others likeMiller–Rabinprove that a number iscomposite.Therefore, the latter might more accurately be calledcompositeness testsinstead of primality tests.

Simple methods[edit]

The simplest primality test istrial division:given an input number,,check whether it isdivisibleby anyprime numberbetween 2 and(i.e., whether the division leaves noremainder). If so, theniscomposite.Otherwise, it is prime.[1]For any divisor,there must be another divisor,and a prime divisorof,and therefore looking for prime divisors at mostis sufficient.

For example, consider the number 100, whose divisors are these numbers:

1, 2, 4, 5, 10, 20, 25, 50, 100.

When all possible divisors up toare tested, some divisors will be discoveredtwice.To observe this, consider the list of divisor pairs of 100:

.

Products pastare the reverse of products that appeared earlier. For example,andare the reverse of each other. Further, that of the two divisors,and.This observation generalizes to all:all divisor pairs ofcontain a divisor less than or equal to,so the algorithm need only search for divisors less than or equal toto guarantee detection of all divisor pairs.[1]

Also, 2 is a prime dividing 100, which immediately proves that 100 is not prime. Every positive integer except 1 is divisible by at least one prime number by theFundamental Theorem of Arithmetic.Therefore the algorithm need only search forprimedivisors less than or equal to.

For another example, consider how this algorithm determines the primality of 17. One has,and the only primesare 2 and 3. Neither divides 17, proving that 17 is prime. For a last example, consider 221. One has,and the primesare 2, 3, 5, 7, 11, and 13. Upon checking each, one discovers that,proving that 221 is not prime.

In cases where it is not feasible to compute the list of primes,it is also possible to simply (and slowly) check all numbers betweenandfor divisors. A rather simple optimization is to test divisibility by 2 and by just the odd numbers between 3 and,since divisibility by an even number implies divisibility by 2.

This method can be improved further. Observe that all primes greater than 3 are of the formfor a nonnegative integerand.Indeed, every integer is of the formfor a positive integerand.Since 2 divides,and,and 3 dividesand,the only possible remainders mod 6 for a prime greater than 3 are 1 and 5. So, a more efficient primality test foris to test whetheris divisible by 2 or 3, then to check through all numbers of the formandwhich are.This is almost three times as fast as testing all numbers up to.

Generalizing further, all primes greater than(c primorial) are of the formforpositive integers,,andcoprimeto.For example, consider.All integers are of the formforintegers with.Now, 2 divides,3 divides,and 5 divides.Thus all prime numbers greater than 30 are of the formfor.Of course, not all numbers of the formwithcoprime toare prime. For example,is not prime, even though 17 is coprime to.

Asgrows, the fraction of coprime remainders to remainders decreases, and so the time to testdecreases (though it still necessary to check for divisibility by all primes that are less than). Observations analogous to the preceding can be appliedrecursively,giving theSieve of Eratosthenes.

One way to speed up these methods (and all the others mentioned below) is to pre-compute and store a list of all primes up to a certain bound, such as all primes up to 200. (Such a list can be computed with the Sieve of Eratosthenes or by an algorithm that tests each incrementalagainst all known primes). Then, before testingfor primality with a large-scale method,can first be checked for divisibility by any prime from the list. If it is divisible by any of those numbers then it is composite, and any further tests can be skipped.

A simple but very inefficient primality test usesWilson's theorem,which states thatis prime if and only if:

Although this method requires aboutmodular multiplications, rendering it impractical, theorems about primes and modular residues form the basis of many more practical methods.

Heuristic tests[edit]

These are tests that seem to work well in practice, but are unproven and therefore are not, technically speaking, algorithms at all. The Fermat test and the Fibonacci test are simple examples, and they areveryeffective when combined.John Selfridgehas conjectured that ifpis an odd number, andp≡ ±2 (mod 5), thenpwill be prime if both of the following hold:

  • 2p−1≡ 1 (modp),
  • fp+1≡ 0 (modp),

wherefkis thek-thFibonacci number.The first condition is the Fermat primality test using base 2.

In general, ifp≡ a (modx2+4), whereais a quadratic non-residue (modx2+4) thenpshould be prime if the following conditions hold:

  • 2p−1≡ 1 (modp),
  • f(1)p+1≡ 0 (modp),

f(x)kis thek-thFibonacci polynomialatx.

Selfridge,Carl Pomerance,andSamuel Wagstafftogether offer $620 for a counterexample.[2]

Probabilistic tests[edit]

Probabilistic testsare more rigorous than heuristics in that they provide provable bounds on the probability of being fooled by a composite number. Many popular primality tests are probabilistic tests. These tests use, apart from the tested numbern,some other numbersawhich are chosen at random from somesample space;the usual randomized primality tests never report a prime number as composite, but it is possible for a composite number to be reported as prime. The probability of error can be reduced by repeating the test with several independently chosen values ofa;for two commonly used tests, foranycompositenat least half thea's detectn's compositeness, sokrepetitions reduce the error probability to at most 2k,which can be made arbitrarily small by increasingk.

The basic structure of randomized primality tests is as follows:

  1. Randomly pick a numbera.
  2. Check equality (corresponding to the chosen test) involvingaand the given numbern.If the equality fails to hold true, thennis a composite number andais awitnessfor the compositeness, and the test stops.
  3. Get back to the step one until the required accuracy is reached.

After one or more iterations, ifnis not found to be a composite number, then it can be declaredprobably prime.

Fermat primality test[edit]

The simplest probabilistic primality test is theFermat primality test(actually a compositeness test). It works as follows:

Given an integern,choose some integeracoprime tonand calculatean− 1modulon.If the result is different from 1, thennis composite. If it is 1, thennmay be prime.

Ifan−1(modulon) is 1 butnis not prime, thennis called a pseudoprimeto basea.In practice, if an−1(modulon) is 1, thennis usually prime. But here is a counterexample: ifn= 341 anda= 2, then

even though 341 = 11·31 is composite. In fact, 341 is the smallest pseudoprime base 2 (see Figure 1 of [3]).

There are only 21853 pseudoprimes base 2 that are less than 2.5×1010(see page 1005 of[3]). This means that, fornup to 2.5×1010,if2n−1(modulon) equals 1, thennis prime, unlessnis one of these 21853 pseudoprimes.

Some composite numbers (Carmichael numbers) have the property thatan− 1is 1 (modulon) for everyathat is coprime ton.The smallest example isn= 561 = 3·11·17, for whicha560is 1 (modulo 561) for allacoprime to 561. Nevertheless, the Fermat test is often used if a rapid screening of numbers is needed, for instance in the key generation phase of theRSA public key cryptographic algorithm.

Miller–Rabin and Solovay–Strassen primality test[edit]

TheMiller–Rabin primality testandSolovay–Strassen primality testare more sophisticated variants, which detect all composites (once again, this means: foreverycomposite numbern,at least 3/4 (Miller–Rabin) or 1/2 (Solovay–Strassen) of numbersaare witnesses of compositeness ofn). These are also compositeness tests.

The Miller–Rabin primality test works as follows: Given an integern,choose some positive integera<n.Let 2sd=n− 1, wheredis odd. If

and

for all

thennis composite andais a witness for the compositeness. Otherwise,nmay or may not be prime. The Miller–Rabin test is astrong probable primetest (see PSW[3]page 1004).

The Solovay–Strassen primality test uses another equality: Given an odd numbern,choose some integera<n,if

,whereis theJacobi symbol,

thennis composite andais a witness for the compositeness. Otherwise,nmay or may not be prime. The Solovay–Strassen test is anEuler probable primetest (see PSW[3]page 1003).

For each individual value ofa,the Solovay–Strassen test is weaker than the Miller–Rabin test. For example, ifn= 1905 anda= 2, then the Miller-Rabin test shows thatnis composite, but the Solovay–Strassen test does not. This is because 1905 is an Euler pseudoprime base 2 but not a strong pseudoprime base 2 (this is illustrated in Figure 1 of PSW[3]).

Frobenius primality test[edit]

The Miller–Rabin and the Solovay–Strassen primality tests are simple and are much faster than other general primality tests. One method of improving efficiency further in some cases is theFrobenius pseudoprimality test;a round of this test takes about three times as long as a round of Miller–Rabin, but achieves a probability bound comparable to seven rounds of Miller–Rabin.

The Frobenius test is a generalization of theLucas probable primetest.

Baillie–PSW primality test[edit]

TheBaillie–PSW primality testis a probabilistic primality test that combines a Fermat or Miller–Rabin test with aLucas probable primetest to get a primality test that has no known counterexamples. That is, there are no known compositenfor which this test reports thatnis probably prime.[4][5]It has been shown that there are no counterexamples forn.

Other tests[edit]

Leonard Adlemanand Ming-Deh Huang presented an errorless (but expected polynomial-time) variant of theelliptic curve primality test.Unlike the other probabilistic tests, this algorithm produces aprimality certificate,and thus can be used to prove that a number is prime.[6]The algorithm is prohibitively slow in practice.

Ifquantum computerswere available, primality could be testedasymptotically fasterthan by using classical computers. A combination ofShor's algorithm,an integer factorization method, with thePocklington primality testcould solve the problem in.[7]

Fast deterministic tests[edit]

Near the beginning of the 20th century, it was shown that a corollary ofFermat's little theoremcould be used to test for primality.[8]This resulted in thePocklington primality test.[9]However, as this test requires a partialfactorizationofn− 1 the running time was still quite slow in the worst case. The firstdeterministicprimality test significantly faster than the naive methods was thecyclotomy test;its runtime can be proven to beO((logn)clog log logn), wherenis the number to test for primality andcis a constant independent ofn.Many further improvements were made, but none could be proven to have polynomial running time. (Running time is measured in terms of the size of the input, which in this case is ~ logn,that being the number of bits needed to represent the numbern.) Theelliptic curve primality testcan be proven to run in O((logn)6), if some conjectures onanalytic number theoryare true.[which?]Similarly, under thegeneralized Riemann hypothesis,the deterministicMiller's test,which forms the basis of the probabilistic Miller–Rabin test, can be proved to run inÕ((logn)4).[10]In practice, this algorithm is slower than the other two for sizes of numbers that can be dealt with at all. Because the implementation of these two methods is rather difficult and creates a risk of programming errors, slower but simpler tests are often preferred.

In 2002, the first provably unconditional deterministic polynomial time test for primality was invented byManindra Agrawal,Neeraj Kayal,andNitin Saxena.TheAKS primality testruns in Õ((logn)12) (improved to Õ((logn)7.5)[11]in the published revision of their paper), which can be further reduced to Õ((logn)6) if theSophie Germain conjectureis true.[12]Subsequently, Lenstra and Pomerance presented a version of the test which runs in time Õ((logn)6) unconditionally.[13]

Agrawal, Kayal and Saxena suggest a variant of their algorithm which would run in Õ((logn)3) ifAgrawal's conjectureis true; however, a heuristic argument by Hendrik Lenstra and Carl Pomerance suggests that it is probably false.[11]A modified version of the Agrawal's conjecture, the Agrawal–Popovych conjecture,[14]may still be true.

Complexity[edit]

Incomputational complexity theory,the formal language corresponding to the prime numbers is denoted as PRIMES. It is easy to show that PRIMES is inCo-NP:its complement COMPOSITES is inNPbecause one can decide compositeness by nondeterministically guessing a factor.

In 1975,Vaughan Prattshowed that there existed a certificate for primality that was checkable in polynomial time, and thus that PRIMES was inNP,and therefore in.Seeprimality certificatefor details.

The subsequent discovery of the Solovay–Strassen and Miller–Rabin algorithms put PRIMES incoRP.In 1992, the Adleman–Huang algorithm[6]reduced the complexity to,which superseded Pratt's result.

TheAdleman–Pomerance–Rumely primality testfrom 1983 put PRIMES inQP(quasi-polynomial time), which is not known to be comparable with the classes mentioned above.

Because of its tractability in practice, polynomial-time algorithms assuming the Riemann hypothesis, and other similar evidence, it was long suspected but not proven that primality could be solved in polynomial time. The existence of theAKS primality testfinally settled this long-standing question and placed PRIMES inP.However, PRIMES is not known to beP-complete,and it is not known whether it lies in classes lying insidePsuch asNCorL.It is known that PRIMES is not inAC0.[15]

Number-theoretic methods[edit]

Certain number-theoretic methods exist for testing whether a number is prime, such as theLucas testandProth's test.These tests typically require factorization ofn+ 1,n− 1, or a similar quantity, which means that they are not useful for general-purpose primality testing, but they are often quite powerful when the tested numbernis known to have a special form.

The Lucas test relies on the fact that themultiplicative orderof a numberamodulonisn− 1 for a primenwhenais aprimitive root modulo n.If we can showais primitive forn,we can shownis prime.

References[edit]

  1. ^abRiesel (1994) pp.2-3
  2. ^John Selfridge#Selfridge's conjecture about primality testing.
  3. ^abcdePomerance, Carl;Selfridge, John L.;Wagstaff, Samuel S. Jr.(July 1980)."The pseudoprimes to 25·109"(PDF).Mathematics of Computation.35(151): 1003–1026.doi:10.1090/S0025-5718-1980-0572872-7.
  4. ^Baillie, Robert;Wagstaff, Samuel S. Jr.(October 1980)."Lucas Pseudoprimes"(PDF).Mathematics of Computation.35(152): 1391–1417.doi:10.1090/S0025-5718-1980-0583518-6.MR0583518.
  5. ^Baillie, Robert; Fiori, Andrew;Wagstaff, Samuel S. Jr.(July 2021). "Strengthening the Baillie-PSW Primality Test".Mathematics of Computation.90(330): 1931–1955.arXiv:2006.14425.doi:10.1090/mcom/3616.S2CID220055722.
  6. ^abAdleman, Leonard M.;Huang, Ming-Deh (1992).Primality testing and Abelian varieties over finite field.Lecture notes in mathematics. Vol. 1512.Springer-Verlag.ISBN3-540-55308-8.
  7. ^Chau, H. F.; Lo, H.-K. (1995). "Primality Test Via Quantum Factorization".arXiv:quant-ph/9508005.
  8. ^Pocklington, H. C. (1914). "The determination of the prime or composite nature of large numbers by Fermat's theorem".Cambr. Phil. Soc. Proc.18:29–30.JFM45.1250.02.
  9. ^Weisstein, Eric W."Pocklington's Theorem".MathWorld.
  10. ^Gary L. Miller(1976)."Riemann's Hypothesis and Tests for Primality".Journal of Computer and System Sciences.13(3): 300–317.doi:10.1016/S0022-0000(76)80043-8.
  11. ^abAgrawal, Manindra; Kayal, Neeraj; Saxena, Nitin (2004)."Primes is in P"(PDF).Annals of Mathematics.160(2): 781–793.doi:10.4007/annals.2004.160.781.
  12. ^Agrawal, Manindra; Kayal, Neeraj; Saxena, Nitin (2004)."PRIMES is in P"(PDF).Annals of Mathematics.160(2): 781–793.doi:10.4007/annals.2004.160.781.
  13. ^Carl Pomerance & Hendrik W. Lenstra (July 20, 2005)."Primality testing with Gaussian periods"(PDF).
  14. ^Popovych, Roman (December 30, 2008)."A note on Agrawal conjecture"(PDF).
  15. ^E. Allender, M. Saks, and I.E. Shparlinski, A lower bound for primality,J. Comp. Syst. Sci.62(2001), pp. 356–366.

Sources[edit]

External links[edit]