Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Primality test

From Wikipedia, the free encyclopedia
Algorithm for determining whether a number is prime

Aprimality test is analgorithm for 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 time ispolynomial in the size of the input). Some primality tests prove that a number is prime, while others likeMiller–Rabin prove that a number iscomposite. Therefore, the latter might more accurately be calledcompositeness tests instead of primality tests.

Simple methods

[edit]

The simplest primality test istrial division: given an input number,n{\displaystyle n}, check whether it isdivisible by anyprime number between 2 andn{\displaystyle {\sqrt {n}}} (i.e., whether the division leaves noremainder). If so, thenn{\displaystyle n} iscomposite. Otherwise, it is prime.[1] For any divisorpn{\displaystyle p\geq {\sqrt {n}}}, there must be another divisorn/pn{\displaystyle n/p\leq {\sqrt {n}}}, and a prime divisorq{\displaystyle q} ofn/p{\displaystyle n/p}, and therefore looking for prime divisors at mostn{\displaystyle {\sqrt {n}}} is 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 ton{\displaystyle n} are tested, some divisors will be discoveredtwice. To observe this, consider the list of divisor pairs of 100:

1×100,2×50,4×25,5×20,10×10,20×5,25×4,50×2,100×1{\displaystyle 1\times 100,\;2\times 50,\;4\times 25,\;5\times 20,\;10\times 10,\;20\times 5,\;25\times 4,\;50\times 2,\;100\times 1}.

Products past10×10{\displaystyle 10\times 10} are the reverse of products that appeared earlier. For example,5×20{\displaystyle 5\times 20} and20×5{\displaystyle 20\times 5} are the reverse of each other. Further, that of the two divisors,5100=10{\displaystyle 5\leq {\sqrt {100}}=10} and20100=10{\displaystyle 20\geq {\sqrt {100}}=10}. This observation generalizes to alln{\displaystyle n}: all divisor pairs ofn{\displaystyle n} contain a divisor less than or equal ton{\displaystyle {\sqrt {n}}}, so the algorithm need only search for divisors less than or equal ton{\displaystyle {\sqrt {n}}} to 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 forprime divisors less than or equal ton{\displaystyle {\sqrt {n}}}.

For another example, consider how this algorithm determines the primality of 17. One has4<17<5{\displaystyle 4<{\sqrt {17}}<5}, and the only primes17{\displaystyle \leq {\sqrt {17}}} are 2 and 3. Neither divides 17, proving that 17 is prime. For a last example, consider 221. One has14<221<15{\displaystyle 14<{\sqrt {221}}<15}, and the primes221{\displaystyle \leq {\sqrt {221}}} are 2, 3, 5, 7, 11, and 13. Upon checking each, one discovers that221/13=17{\displaystyle 221/13=17}, proving that 221 is not prime.

In cases where it is not feasible to compute the list of primesn{\displaystyle \leq {\sqrt {n}}}, it is also possible to simply (and slowly) check all numbers between2{\displaystyle 2} andn{\displaystyle {\sqrt {n}}} for divisors. A simple improvement is to test divisibility by 2 and by just the odd numbers between 3 andn{\displaystyle {\sqrt {n}}}, since divisibility by an even number implies divisibility by 2.

This method can be improved further. Observe that all primes greater than 5 are of the form6k+i{\displaystyle 6k+i} for a nonnegative integerk{\displaystyle k} andi{1,5}{\displaystyle i\in \{1,5\}}. Indeed, every integer is of the form6k+i{\displaystyle 6k+i} for a positive integerk{\displaystyle k} andi{0,1,2,3,4,5}{\displaystyle i\in \{0,1,2,3,4,5\}}. Since 2 divides6k,6k+2{\displaystyle 6k,6k+2}, and6k+4{\displaystyle 6k+4}, and 3 divides6k{\displaystyle 6k} and6k+3{\displaystyle 6k+3}, the only possible remainders mod 6 for a prime greater than 3 are 1 and 5. So, a more efficient primality test forn{\displaystyle n} is to test whethern{\displaystyle n} is divisible by 2 or 3, then to check through all numbers of the form6k+1{\displaystyle 6k+1} and6k+5{\displaystyle 6k+5} which aren{\displaystyle \leq {\sqrt {n}}}. This is almost three times as fast as testing all numbers up ton{\displaystyle {\sqrt {n}}}.

Generalizing further, all primes greater thanc#{\displaystyle c\#} (c primorial) are of the formc#k+i{\displaystyle c\#\cdot k+i} fori,k{\displaystyle i,k} positive integers,0i<c#{\displaystyle 0\leq i<c\#}, andi{\displaystyle i}coprime toc#{\displaystyle c\#}. For example, consider6#=235=30{\displaystyle 6\#=2\cdot 3\cdot 5=30}. All integers are of the form30k+i{\displaystyle 30k+i} fori,k{\displaystyle i,k} integers with0i<30{\displaystyle 0\leq i<30}. Now, 2 divides0,2,4,,28{\displaystyle 0,2,4,\dots ,28}, 3 divides0,3,6,,27{\displaystyle 0,3,6,\dots ,27}, and 5 divides0,5,10,,25{\displaystyle 0,5,10,\dots ,25}. Thus all prime numbers greater than 30 are of the form30k+i{\displaystyle 30k+i} fori{1,7,11,13,17,19,23,29}{\displaystyle i\in \{1,7,11,13,17,19,23,29\}}. Of course, not all numbers of the formc#k+i{\displaystyle c\#\cdot k+i} withi{\displaystyle i} coprime toc#{\displaystyle c\#} are prime. For example,1923=437=2102+17=27#+17{\displaystyle 19\cdot 23=437=210\cdot 2+17=2\cdot 7\#+17} is not prime, even though 17 is coprime to7#=2357{\displaystyle 7\#=2\cdot 3\cdot 5\cdot 7}.

Asc{\displaystyle c} grows, the fraction of coprime remainders to remainders decreases, and so the time to testn{\displaystyle n} decreases (though it still necessary to check for divisibility by all primes that are less thanc{\displaystyle c}). 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 incrementalm{\displaystyle m} against all known primesm{\displaystyle \leq {\sqrt {m}}}). Then, before testingn{\displaystyle n} for primality with a large-scale method,n{\displaystyle n} 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 inefficient primality test usesWilson's theorem, which states thatp{\displaystyle p} is prime if and only if:

(p1)!1(modp){\displaystyle (p-1)!\equiv -1{\pmod {p}}}

Although this method requires aboutp{\displaystyle p} modular multiplications,[2] rendering it impractical, theorems about primes and modular residues form the basis of many more practical methods.

Heuristic tests

[edit]
icon
This sectionneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources in this section. Unsourced material may be challenged and removed.(August 2025) (Learn how and when to remove this message)

These are tests that seem to work well in practice, but are unproven and therefore are not, technically speaking, algorithms at all.TheFermat primality test and the Fibonacci test are simple examples, and they are effective when combined.John Selfridge has conjectured that ifp is an odd number, andp ≡ ±2 (mod 5), thenp will be prime if both of the following hold:

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

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

In general, ifp ≡ a (modx2+4), wherea is a quadratic non-residue (modx2+4) thenp should be prime if the following conditions hold:

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

f(x)k is thek-thFibonacci polynomial atx.

Selfridge,Pomerance andWagstaff together offered $620 for a counterexample or proof that there is none,[3] with the prize now due from theNumber Theory Foundation.[citation needed]

Probabilistic tests

[edit]

Probabilistic tests are more rigorous than heuristics in that they provide provable bounds on the probability of being fooled by a composite number.Multiple popular primality tests are probabilistic tests. These tests use, apart from the tested numbern, some other numbersa which 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, forany compositen at least half thea's detectn's compositeness, sok repetitions 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) involvinga and the given numbern. If the equality fails to hold true, thenn is a composite number anda is awitness for the compositeness, and the test stops.
  3. Get back to the step one until the required accuracy is reached.

After one or more iterations, ifn is 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 integera coprime ton and calculatean − 1modulon. If the result is different from 1, thenn is composite. If it is 1, thenn may be prime.

Ifan−1 (modulon) is 1 butn is not prime, thenn is called apseudoprime to basea. In practice, ifan−1 (modulon)is 1, thenn is usually prime. But here is a counterexample:ifn = 341 anda = 2, then

23401(mod341){\displaystyle 2^{340}\equiv 1{\pmod {341}}}

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

There are only 21853 pseudoprimes base 2 that are less than 2.5×1010 (see page 1005 of[4]). This means that, forn up to 2.5×1010, if2n−1 (modulon) equals 1, thenn is prime, unlessn is one of these 21853 pseudoprimes.

Some composite numbers (Carmichael numbers) have the property thatan − 1 is 1 (modulon) for everya that is coprime ton. The smallest example isn = 561 = 3·11·17, for whicha560 is 1 (modulo 561) for alla coprime 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 test andSolovay–Strassen primality test are more sophisticated variants, which detect all composites (once again, this means: forevery composite numbern, at least 3/4 (Miller–Rabin) or 1/2 (Solovay–Strassen) of numbersa are 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, whered is odd. If

ad±1(modn){\displaystyle a^{d}\not \equiv \pm 1{\pmod {n}}}

and

a2rd1(modn){\displaystyle a^{2^{r}d}\not \equiv -1{\pmod {n}}} for all0rs1,{\displaystyle 0\leq r\leq s-1,}

thenn is composite anda is a witness for the compositeness. Otherwise,n may or may not be prime.The Miller–Rabin test is astrong probable prime test (see PSW[4] page 1004).

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

a(n1)/2(an)(modn){\displaystyle a^{(n-1)/2}\not \equiv \left({\frac {a}{n}}\right){\pmod {n}}}, where(an){\displaystyle \left({\frac {a}{n}}\right)} is theJacobi symbol,

thenn is composite anda is a witness for the compositeness. Otherwise,n may or may not be prime.The Solovay–Strassen test is anEuler probable prime test (see PSW[4] 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 thatn is composite, but the Solovay–Strassen test does not. This is because 1905 is an Eulerpseudoprime base 2 but not a strong pseudoprime base 2 (this is illustrated in Figure 1 of PSW[4]).

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 prime test.

Baillie–PSW primality test

[edit]

TheBaillie–PSW primality test is a probabilistic primality test that combines a Fermat or Miller–Rabin test with aLucas probable prime test to get a primality test that has no known counterexamples. That is, there are no known compositen for which this test reports thatn is probably prime.[5][6] It has been shown that there are no counterexamples forn<264{\displaystyle <2^{64}}.

Other tests

[edit]

Leonard Adleman and 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.[7] The algorithm is prohibitively slow in practice.

Ifquantum computers were available, primality could be testedasymptotically faster than by using classical computers. A combination ofShor's algorithm, an integer factorization method, with thePocklington primality test could solve the problem inO((logn)3(loglogn)2logloglogn){\displaystyle O((\log n)^{3}(\log \log n)^{2}\log \log \log n)}.[8]

Fast deterministic tests

[edit]

Near the beginning of the 20th century, it was shown that a corollary ofFermat's little theorem could be used to test for primality.[9] This resulted in thePocklington primality test.[10] However, as this test requires a partialfactorization ofn − 1 the running time was still quite slow in the worst case. The firstdeterministic primality test significantly faster than the naive methods was thecyclotomy test; its runtime can be proven to beO((log n)c log log log n), wheren is the number to test for primality andc is a constant independent ofn. A number of 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 ~ log n, that being the number of bits needed to represent the numbern.) Theelliptic curve primality test can be proven to run in O((log n)6), if some conjectures onanalytic number theory are true.[which?] Similarly, under thegeneralized Riemann hypothesis (which Miller, confusingly, calls the "extended Riemann hypothesis"), the deterministicMiller's test, which forms the basis of the probabilistic Miller–Rabin test, can be proved to run inÕ((log n)4).[11] 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 test runs in Õ((log n)12) (improved to Õ((log n)7.5)[12] in the published revision of their paper), which can be further reduced to Õ((log n)6) if theSophie Germain conjecture is true.[13] Subsequently, Lenstra and Pomerance presented a version of the test which runs in time Õ((log n)6) unconditionally.[14]

Agrawal, Kayal and Saxena suggest a variant of their algorithm which would run in Õ((log n)3) ifAgrawal's conjecture is true; however, a heuristic argument by Hendrik Lenstra and Carl Pomerance suggests that it is probably false.[12] A modified version of the Agrawal's conjecture, the Agrawal–Popovych conjecture,[15] 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 inNP because one can decide compositeness by nondeterministically guessing a factor.

In 1975,Vaughan Pratt showed that there existed a certificate for primality that was checkable in polynomial time, and thus that PRIMES was inNP, and therefore inNPcoNP{\displaystyle {\mathsf {NP\cap coNP}}}. Seeprimality certificate for details.

The subsequent discovery of the Solovay–Strassen and Miller–Rabin algorithms put PRIMES incoRP. In 1992, the Adleman–Huang algorithm[7] reduced the complexity toZPP=RPcoRP{\displaystyle {\mathsf {{\color {Blue}ZPP}=RP\cap coRP}}}, which superseded Pratt's result.

TheAdleman–Pomerance–Rumely primality test from 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 test finally 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 insideP such asNC orL. It is known that PRIMES is not inAC0.[16]

Number-theoretic methods

[edit]

Certain number-theoretic methods exist for testing whether a number is prime, such as theLucas test andProth'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 numbern is known to have a special form.

The Lucas test relies on the fact that themultiplicative order of a numbera modulon isn − 1 for a primen whena is aprimitive root modulo n. If we can showa is primitive forn, we can shown is prime.

References

[edit]
  1. ^abRiesel (1994) pp.2-3
  2. ^Barrus, Mike; Clark, W. Edwin (2021-09-05)."Primality Tests".Elementary Number Theory. Mathematics LibreTexts. Retrieved2025-03-14.
  3. ^Guy, Richard (1994).Unsolved problems in number theory. Springer-Verlag: New York. p. 28.ISBN 0387942890.
  4. ^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.
  5. ^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.MR 0583518.
  6. ^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.S2CID 220055722.
  7. ^abAdleman, Leonard M.; Huang, Ming-Deh (1992).Primality testing and Abelian varieties over finite field. Lecture notes in mathematics. Vol. 1512.Springer-Verlag.ISBN 3-540-55308-8.
  8. ^Chau, H. F.; Lo, H.-K. (1995). "Primality Test Via Quantum Factorization".arXiv:quant-ph/9508005.
  9. ^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.JFM 45.1250.02.
  10. ^Weisstein, Eric W."Pocklington's Theorem".MathWorld.
  11. ^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.
  12. ^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.
  13. ^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.
  14. ^Carl Pomerance & Hendrik W. Lenstra (July 20, 2005)."Primality testing with Gaussian periods"(PDF).
  15. ^Popovych, Roman (December 30, 2008)."A note on Agrawal conjecture"(PDF).
  16. ^Allender, Eric; Saks, Michael; Shparlinski, Igor (2001)."A Lower Bound for Primality".Journal of Computer and System Sciences.62 (2):356–366.doi:10.1006/jcss.2000.1725.

Sources

[edit]

External links

[edit]
Primality tests
Prime-generating
Integer factorization
Multiplication
Euclideandivision
Discrete logarithm
Greatest common divisor
Modular square root
Other algorithms
  • Italics indicate that algorithm is for numbers of special forms
Retrieved from "https://en.wikipedia.org/w/index.php?title=Primality_test&oldid=1321400226"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp