Lably
Skip to content

Fermat primality test

  • 3 min read
  • 5 chapters

Related on Lably

Through Modular arithmetic and Big O notation

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.

Sign in to keep reading

4 more chapters are free with a Lably account.

More in Computing and communication