<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- This manual describes how to install and use the GNU multiple precision arithmetic library, version 6.1.0. Copyright 1991, 1993-2015 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover Texts being "A GNU Manual", and with the Back-Cover Texts being "You have freedom to copy and modify this GNU Manual, like GNU software". A copy of the license is included in GNU Free Documentation License. --> <!-- Created by GNU Texinfo 6.4, http://www.gnu.org/software/texinfo/ --> <head> <title>Prime Testing Algorithm (GNU MP 6.1.0)</title> <meta name="description" content="How to install and use the GNU multiple precision arithmetic library, version 6.1.0."> <meta name="keywords" content="Prime Testing Algorithm (GNU MP 6.1.0)"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="Other-Algorithms.html#Other-Algorithms" rel="up" title="Other Algorithms"> <link href="Factorial-Algorithm.html#Factorial-Algorithm" rel="next" title="Factorial Algorithm"> <link href="Other-Algorithms.html#Other-Algorithms" rel="prev" title="Other Algorithms"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> </head> <body lang="en"> <a name="Prime-Testing-Algorithm"></a> <div class="header"> <p> Next: <a href="Factorial-Algorithm.html#Factorial-Algorithm" accesskey="n" rel="next">Factorial Algorithm</a>, Previous: <a href="Other-Algorithms.html#Other-Algorithms" accesskey="p" rel="prev">Other Algorithms</a>, Up: <a href="Other-Algorithms.html#Other-Algorithms" accesskey="u" rel="up">Other Algorithms</a> [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Prime-Testing"></a> <h4 class="subsection">15.7.1 Prime Testing</h4> <a name="index-Prime-testing-algorithms"></a> <p>The primality testing in <code>mpz_probab_prime_p</code> (see <a href="Number-Theoretic-Functions.html#Number-Theoretic-Functions">Number Theoretic Functions</a>) first does some trial division by small factors and then uses the Miller-Rabin probabilistic primality testing algorithm, as described in Knuth section 4.5.4 algorithm P (see <a href="References.html#References">References</a>). </p> <p>For an odd input <em>n</em>, and with <em>n = q*2^k+1</em> where <em>q</em> is odd, this algorithm selects a random base <em>x</em> and tests whether <em>x^q mod n</em> is 1 or <em>-1</em>, or an <em>x^(q*2^j) mod n</em> is <em>1</em>, for <em>1<=j<=k</em>. If so then <em>n</em> is probably prime, if not then <em>n</em> is definitely composite. </p> <p>Any prime <em>n</em> will pass the test, but some composites do too. Such composites are known as strong pseudoprimes to base <em>x</em>. No <em>n</em> is a strong pseudoprime to more than <em>1/4</em> of all bases (see Knuth exercise 22), hence with <em>x</em> chosen at random there’s no more than a <em>1/4</em> chance a “probable prime” will in fact be composite. </p> <p>In fact strong pseudoprimes are quite rare, making the test much more powerful than this analysis would suggest, but <em>1/4</em> is all that’s proven for an arbitrary <em>n</em>. </p> </body> </html>