Fermat primality test

Fermat primality test

The Fermat primality test is a probabilistic test to determine if a number is probably prime.


Fermat's little theorem states that if "p" is prime and 1 le a < p, then

:a^{p-1} equiv 1 pmod{p}

If we want to test if "p" is prime, then we can pick random "a"'s in the interval and see if the equality holds. If the equality does not hold for a value of "a", then "p" is composite. If the equality does hold for many values of "a", then we can say that "p" is probably prime, or a pseudoprime.

It might be in our tests that we do not pick any value for "a" such that the equality fails. Any "a" such that

:a^{n-1} equiv 1 pmod{n}

when "n" is composite is known as a "Fermat liar". If we do pick an "a" such that

:a^{n-1} otequiv 1 pmod{n}

then "a" is known as a "Fermat witness" for the compositeness of "n".

Algorithm and running time

The algorithm can be written as follows::Inputs: "n": a value to test for primality; "k": a parameter that determines the number of times to test for primality:Output: composite if "n" is composite, otherwise probably prime:repeat "k" times:::pick "a" randomly in the range (1, "n" − 1] ::if "a""n" − 1 mod "n" ≠ 1 then return composite:return probably prime

Using fast algorithms for modular exponentiation, the running time of this algorithm is O("k" × log2"n" × log log "n" × log log log "n"), where "k" is the number of times we test a random "a", and "n" is the value we want to test for primality.


There are certain values of "n" known as Carmichael numbers for which all values of "a" for which gcd(a,n)=1 are Fermat liars. Although Carmichael numbers are rare, there are enough of them that Fermat's primality test is often not used in favor of other primality tests such as Miller-Rabin and Solovay-Strassen.

In general, if "n" is not a Carmichael number then at least half of all


are Fermat witnesses. For proof of this, let "a" be a Fermat witness and "a"1, "a"2, ..., "a""s" be Fermat liars. Then

:(acdot a_i)^{n-1} equiv a^{n-1}cdot a_i^{n-1} equiv a^{n-1} otequiv 1pmod{n}

and so all "a" &times; "a"i for "i" = 1, 2, ..., "s" are Fermat witnesses.


The encryption program PGP uses this primality test in its algorithms. The chance of PGP generating a Carmichael number is less than 1 in 1050, which is more than adequate for practical purposes.


* Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. "Introduction to Algorithms", Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Pages 889&ndash;890 of section 31.8, Primality testing.

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Primality test — A primality test is an algorithm for determining whether an input number is prime. Amongst other fields of mathematics, it is used for cryptography. Unlike integer factorization, primality tests do not generally give prime factors, only stating… …   Wikipedia

  • AKS primality test — The AKS primality test (also known as Agrawal–Kayal–Saxena primality test and cyclotomic AKS test) is a deterministic primality proving algorithm created and published by three Indian Institute of Technology Kanpur computer scientists, Manindra… …   Wikipedia

  • Miller–Rabin primality test — The Miller–Rabin primality test or Rabin–Miller primality test is a primality test: an algorithm which determines whether a given number is prime, similar to the Fermat primality test and the Solovay–Strassen primality test. Its original version …   Wikipedia

  • Solovay–Strassen primality test — The Solovay–Strassen primality test, developed by Robert M. Solovay and Volker Strassen, is a probabilistic test to determine if a number is composite or probably prime. It has been largely superseded by the Miller–Rabin primality test, but has… …   Wikipedia

  • Fermat's little theorem — (not to be confused with Fermat s last theorem) states that if p is a prime number, then for any integer a , a^p a will be evenly divisible by p . This can be expressed in the notation of modular arithmetic as follows::a^p equiv a pmod{p},!A… …   Wikipedia

  • Primality certificate — In mathematics and computer science, a primality certificate or primality proof is a succinct, formal proof that a number is prime. Primality certificates allow the primality of a number to be rapidly checked without having to run an expensive or …   Wikipedia

  • Test de primalidad — El 39º número primo de Mersenne era el mayor conocido hasta la fecha de creación de este artículo. La cuestión de la determinación de si un número n …   Wikipedia Español

  • Fermat number — In mathematics, a Fermat number, named after Pierre de Fermat who first studied them, is a positive integer of the form:F {n} = 2^{2^{ overset{n} {} + 1where n is a nonnegative integer. The first nine Fermat numbers are OEIS|id=A000215:As of|2008 …   Wikipedia

  • Test de primalidad AKS — El test de primalidad AKS o algoritmo AKS es un algoritmo determinista que decide en tiempo polinómico si un número natural es primo o compuesto. Fue diseñado por los científicos de computación Manindra Agrawal, Neeraj Kayal y Nitin Saxena del… …   Wikipedia Español

  • Lucas–Lehmer test for Mersenne numbers — This article is about the Lucas–Lehmer test (LLT), that only applies to Mersenne numbers. There is also a Lucas Lehmer Riesel test for numbers of the form N=k 2^n 1, with 2^n > k, based on the LLT: see Lucas Lehmer Riesel test. There is also a… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”