The Fermat primality test is a probabilistic test to determine whether a number is a probable prime.
Concept Fermat's little theorem states that if p is prime and a is not divisible by p, then
ap−1≡1(modp). If one wants to test whether p is prime, then we can pick random integers a not divisible by p and see whether the congruence holds. If it does not hold for a value of a, then p is composite. This congruence is unlikely to hold for a random a if p is composite. Therefore, if the equality does hold for one or more values of a, then we say that p is probably prime. However, note that the above congruence holds trivially for a≡1(modp), because the congruence relation is compatible with exponentiation. It also holds trivially for a≡−1(modp) if p is odd, for the same reason. That is why one usually chooses a random a in the interval 1<a<p−1. Any a such that
an−1≡1(modn) when n is composite is known as a Fermat liar. In this case n is called Fermat pseudoprime to base a. If we do pick an a such that
an−1≢1(modn) then a is known as a Fermat witness for the compositeness of n.
Example Suppose we wish to determine whether n = 221 is prime. Randomly pick 1 < a < 220, say a = 38. We check the above congruence and find that it holds:
an−1=38220≡1(mod221). Either 221 is prime, or 38 is a Fermat liar, so we take another a, say 24:
an−1=24220≡81≢1(mod221). So 221 is composite and 38 was indeed a Fermat liar. Furthermore, 24 is a Fermat witness for the compositeness of 221.
Algorithm The algorithm can be written as follows:
Inputs: n: a value to test for primality, n>3; 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 [2, n − 2] If an−1≢1(modn), then return composite If composite is never returned: return probably prime The a values 1 and n − 1 are not used as the equality holds for all n and all odd n respectively, hence testing them adds no value.
Complexity Using fast algorithms for modular exponentiation and multiprecision multiplication, the running time of this algorithm is O(k log2n log log n) = Õ(k log2n), where k is the number of times we test a random a, and n is the value we want to test for primality; see Miller–Rabin primality test for details.
Flaw There are infinitely many Fermat pseudoprimes to any given basis a > 1. Even worse, there are infinitely many Carmichael numbers. These are numbers n for which all values of a with gcd(a,n)=1 are Fermat liars. For these numbers, repeated application of the Fermat primality test performs the same as a simple random search for factors. While Carmichael numbers are substantially rarer than prime numbers (Erdős' upper bound for the number of Carmichael numbers is lower than the prime number function n/log(n)) there are enough of them that Fermat's primality test is not often used in the above form. Instead, other more powerful extensions of the Fermat test, such as Baillie–PSW, Miller–Rabin, and Solovay–Strassen are more commonly used. In general, if n is a composite number that is not a Carmichael number, then at least half of all
a∈(ℤ/nℤ)∗ (i.e. gcd(a,n)=1) are Fermat witnesses. For proof of this, let a be a Fermat witness and a1, a2, ..., as be Fermat liars. Then
(a⋅ai)n−1≡an−1⋅ain−1≡an−1≢1(modn) and so all a⋅ai for i=1,2,...,s are Fermat witnesses.
Corollaries Analogous to the Lucas–Lehmer residue, r=an−1(modn) is called the Fermet residue of n to base a. There are a few variants that produce different types of residues, most importantly the strong probable prime (SPRP) residue.
Efficient exponentiation For Mersenne numbers (or more broadly, any n=2p−1, p not necessarily a prime), it is more efficient to calculate a2r=a2p(modn) than to calculate r due to exponentiation by squaring preferring a low Hamming weight (number of ones) in the exponent. The desired r may be recovered by multiplying by the modular inverse of a2 mod n. It can alternatively be recovered by finding a small multiplier u such that lift(a2r)+un is divisible by a2 in ordinary integer arithmetic, then r=(lift(a2r)+un)/a2. This can proceed by trial and error, or by noticing that u=modinv(n,a2).
Testing cofactors of n If the residue r for n to base a is known, then for any proper divisor k of n, it is possible to perform a quick though weaker primality test on n/k. If n/k is prime, by Fermat's theorem an/k=ak(modn/k) and an=ak(modn/k). As a result r=ak−1(modn/k), which can be much more efficiently checked for values of k much smaller than n. (This is the method used by the Great Internet Mersenne Prime Search for testing cofactors.) A even weaker form of the test can be conducted with a truncated an(modn)(mod2t) if storage space for the residue is a concern, so long as d≤2t and n>2t. Let s=ak(modn/k), then r=s+wn/k for some w. Take this mod 2t and we have w=(n/k)−1(s−r)(mod2t). n/k is composite if w≥d. A similar test can be done on truncated Lucas–Lehmer residues.
Path to being deterministic It is also true that if all bases a are systematically checked in the interval [2,n], each demonstrating congruence to 1, the test is effectively deterministic. We may say that n is definitely prime. At cursory glance one may assume that n exists in the union of both primes and Carmichael numbers for such a scenario, but if a-values are systematically checked in the interval, one is bound to be a prime factor of a composite n at some point before n, thus making a and n not coprime, and failing the congruence, even if n is indeed a Carmichael number. Carmichael numbers do fail Fermat's congruence to 1 if the base used is not coprime. Though this is more computationally expensive than brute force divisibility checking (trial division), it is of theoretical value.
… excerpt ends here. Continue reading the full article.