ArticleslgStudy

science

Fermat primality test

Fermat primality test is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Fermat primality test rather than just read about it. In short: 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).

Key takeaways

  • Fermat primality test belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Fermat primality test to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Fermat primality test from memory before moving on to harder problems.

Reference excerpt

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.

Worked examples

Example 1 — a first encounter with Fermat primality test

Start with the simplest possible case. Write down what Fermat primality test claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Fermat primality test before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Fermat primality test ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Fermat primality test

In research
Fermat primality test appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Fermat primality test in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Fermat primality test is common in secondary-school and first-year university syllabi. It links to neighbouring topics Modular arithmetic, Pierre de Fermat, Primality tests, so understanding it makes those chapters shorter.
In everyday life
Look for Fermat primality test outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Fermat primality test” →

How to study Fermat primality test in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Fermat primality test means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Fermat primality test out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Fermat primality test in simple terms?

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).

Why does Fermat primality test matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Fermat primality test?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Fermat primality test.

Tags

  • Modular arithmetic
  • Pierre de Fermat
  • Primality tests

Keep exploring