Fermat primality test: Difference between revisions

From Number
(Created page with "{{primality test}} ==Definition== The '''Fermat primality test''' is a quick and often inconclusive primality test. In the version below, we consider only <math>n > 2 </...")
 
No edit summary
 
Line 3: Line 3:
==Definition==
==Definition==


The '''Fermat primality test''' is a quick and often inconclusive [[primality test]]. In the version below, we consider only <math>n > 2 </math>.
The '''Fermat primality test''' is a quick and often inconclusive [[primality test]]. In the version below, we consider only <math>n > 1</math>.


Given a [[natural number]] <math>n > 2</math>, the test works as follows:
Given a [[natural number]] <math>n > 1</math>, the test works as follows:


# Pick a random element <math>a</math> from the congruence classes of integers mod <math>n</math> (exclude the congruence classes of 0 and 1). Explicitly, pick <math>a</math> as an integer among <math>2,3,\dots,n-1</math>.
# Pick a random element <math>a</math> from the congruence classes of nonzero integers mod <math>n</math> (exclude the congruence class of 0). Explicitly, pick <math>a</math> as an integer among <math>1,2,3,\dots,n-1</math>.
# Check whether <math>a^{n-1} \equiv 1 \pmod n</math>. To compute the power use [[repeated squaring]]. If the condition does ''not'' hold, then <math>n</math> is composite. If the condition ''does'' hold, then <math>n</math> may be prime or composite.
# Check whether <math>a^{n-1} \equiv 1 \pmod n</math>. To compute the power use [[repeated squaring]]. If the condition does ''not'' hold, then <math>n</math> is composite. If the condition ''does'' hold, then <math>n</math> may be prime or composite.



Latest revision as of 23:26, 4 January 2012

Template:Primality test

Definition

The Fermat primality test is a quick and often inconclusive primality test. In the version below, we consider only n>1.

Given a natural number n>1, the test works as follows:

  1. Pick a random element a from the congruence classes of nonzero integers mod n (exclude the congruence class of 0). Explicitly, pick a as an integer among 1,2,3,…,n−1.
  2. Check whether an−1≡1(modn). To compute the power use repeated squaring. If the condition does not hold, then n is composite. If the condition does hold, then n may be prime or composite.

The above is a single iteration of the test. Multiple iterations of the test may be carried out with multiple random elements. If at any stage the condition an−1≡1(modn) is violated, we can conclude that the number is composite. If, however, the condition holds every time, the situation is inconclusive and n may be prime or composite.

Theory

Why it works

The primality test above is correct because of Fermat's little theorem, which says that for a prime p and a relatively prime to p, we have:

ap−1≡1(modp)

Behavior on composite numbers

For any composite number n, we use the following terms:

Nonzero numbers that have a common factor with n always serve as Fermat witnesses. There are n−φ(n)−1 of these where φ is the Euler totient function. For n composite, this number is at least equal to n−1, the worst case arising when n is a square of a prime. In general, if n has a small number of large prime factors, there are very few such numbers and a uniform random choice is highly unlikely to hit them.

For the numbers that are relatively prime to n, we have the formula for probability of relatively prime integer being a Fermat liar. If the probability is 1, then n is a Carmichael number. If the probability is less than 1, then it is at most 1/2, which means that the Fermat primality test can find a witness with arbitrarily high probability of success in a finite number of trials.


Related tests