**Fermat's little theorem** states that if p is a prime number, then for any integer a, the number *a* ^{p} − *a* is an integer multiple of p. In the notation of modular arithmetic, this is expressed as

a
p
≡
a
(
mod
p
)
.
For example, if a = 2 and p = 7, 2^{7} = 128, and 128 − 2 = 7 × 18 is an integer multiple of 7.

If a is not divisible by p, Fermat's little theorem is equivalent to the statement that *a* ^{p − 1} − 1 is an integer multiple of p, or in symbols

a
p
−
1
≡
1
(
mod
p
)
.
For example, if a = 2 and p = 7 then 2^{6} = 64 and 64 − 1 = 63 is thus a multiple of 7.

Fermat's little theorem is the basis for the Fermat primality test and is one of the fundamental results of elementary number theory. The theorem is named after Pierre de Fermat, who stated it in 1640. It is called the "little theorem" to distinguish it from Fermat's last theorem.

Pierre de Fermat first stated the theorem in a letter dated October 18, 1640, to his friend and confidant Frénicle de Bessy. His formulation is equivalent to the following:

If p is a prime and a is any integer not divisible by p, then *a* ^{p - 1} - 1 is divisible by p.

In fact, the original statement was

Tout nombre premier mesure infailliblement une des puissances – 1 de quelque progression que ce soit, et l'exposant de la dite puissance est sous-multiple du nombre premier donné – 1 ; et, après qu'on a trouvé la première puissance qui satisfait à la question, toutes celles dont les exposants sont multiples de l'exposant de la première satisfont tout de même à la question.

This may be translated in modern terminology, with explanations and formulas inserted between brackets, for easier understanding:

Every prime number [p] divides necessarily one of the powers –1 of any [geometric] progression [*a*, *a*^{2}, *a*^{3}, ...] [that is there exists t such that p divides *a*^{t} – 1], and the exponent of this power [t] divides the given prime –1 [divides *p* – 1]; and, after one has found the first power [t] that satisfies the question, all those whose exponents are multiple of the exponent of the first one satisfy similarly the question [that is, all multiples of the first t have the same property].

Fermat did not consider the case where a is a multiple of p nor prove his assertion, only stating:

Et cette proposition est généralement vraie en toutes progressions et en tous nombres premiers; de quoi je vous envoierois la démonstration, si je n'appréhendois d'être trop long.

(And this proposition is generally true for all series [*sic*] and for all prime numbers; I would send you a demonstration of it, if I did not fear going on for too long.)

Euler provided the first published proof in 1736 in a paper entitled "Theorematum Quorundam ad Numeros Primos Spectantium Demonstratio" in the *Proceedings* of the St. Petersburg Academy, but Leibniz had given virtually the same proof in an unpublished manuscript from sometime before 1683.

The term "Fermat's Little Theorem" was probably first used in print in 1913 in *Zahlentheorie* by Kurt Hensel:

Für jede endliche Gruppe besteht nun ein Fundamentalsatz, welcher der kleine Fermatsche Satz genannt zu werden pflegt, weil ein ganz spezieller Teil desselben zuerst von Fermat bewiesen worden ist."

(There is a fundamental theorem holding in every finite group, usually called Fermat's little Theorem because Fermat was the first to have proved a very special part of it.)

An early use in English occurs in A.A. Albert, Modern Higher Algebra (1937), which refers to "the so-called "little" Fermat theorem" on page 206.

Some mathematicians independently made the related hypothesis (sometimes incorrectly called the Chinese Hypothesis) that 2^{p} ≡ 2 (mod *p*) if and only if p is a prime. Indeed, the "if" part is true, and is a special case of Fermat's little theorem. However, the "only if" part of this hypothesis is false: for example, 2^{341} ≡ 2 (mod 341), but 341 = 11 × 31 is a pseudoprime. See below.

Several proofs of Fermat's little theorem are known. It is frequently proved as a corollary of Euler's theorem.

Fermat's little theorem is a special case of Euler's theorem: for any modulus n and any integer a coprime to n, we have

a
φ
(
n
)
≡
1
(
mod
n
)
,
where φ(*n*) denotes Euler's totient function (which counts the integers between 1 and n that are coprime to n). Euler's theorem is indeed a generalization, because if *n* = *p* is a prime number, then φ(*p*) = *p* − 1.

A slight generalization of Euler's theorem, which immediately follows from it, is: if a, n, x, y are integers with n *positive* and a and n coprime, then

If

x
≡
y
(
mod
φ
(
n
)
)
, then

a
x
≡
a
y
(
mod
n
)
.

This follows as x is of the form *y* + φ(*n*)*k*, so

a
x
=
a
y
+
φ
(
n
)
k
=
a
y
(
a
φ
(
n
)
)
k
≡
a
y
1
k
≡
a
y
(
mod
n
)
.
In this form, the theorem finds many uses in cryptography and, in particular, underlies the computations used in the RSA public key encryption method. The special case with n a prime may be considered a consequence of Fermat's little theorem.

Fermat's little theorem is also related to the Carmichael function and Carmichael's theorem, as well as to Lagrange's theorem in group theory.

The algebraic setting of Fermat's little theorem can be generalized to finite fields.

The converse of Fermat's little theorem is not generally true, as it fails for Carmichael numbers. However, a slightly stronger form of the theorem is true, and is known as Lehmer's theorem. The theorem is as follows:

If there exists an a such that

a
p
−
1
≡
1
(
mod
p
)
and for all primes q dividing p − 1

a
(
p
−
1
)
/
q
≢
1
(
mod
p
)
then p is prime.

This theorem forms the basis for the Lucas–Lehmer test, an important primality test.

If a and p are coprime numbers such that *a* ^{p − 1} − 1 is divisible by p, then p need not be prime. If it is not, then p is called a pseudoprime to base a (or a Fermat pseudoprime). F. Sarrus in 1820 found 341 = 11 × 31 as one of the first pseudoprimes, to base 2.

A number p that is a pseudoprime to base a for every number a coprime to p is called a Carmichael number (e.g. 561). Alternately, any number p satisfying the equality

gcd
(
∑
a
=
1
p
−
1
a
p
−
1
,
p
)
=
1
is either a prime or a Carmichael number.

Miller–Rabin primality test uses the following extension of Fermat's little theorem:

If p is an odd prime number, and *p* – 1 = 2^{s} *d*, with d odd, then for every a prime to p, either *a*^{d} ≡ 1 mod *p*, or there exists t such that 0 ≤ *t* < s and *a*^{2td} ≡ −1 mod *p*

This result may be deduced from Fermat's little theorem by the fact that, if p is an odd prime, then the integers modulo p form a finite field, in which 1 has exactly two square roots, 1 and −1.

Miller–Rabin test uses this property in the following way. Given *p* = 2^{s} *d* + 1, with d odd, an odd integer for which primality has to be tested, choose randomly a such that 1 < *a* < *p*; then compute *b* = *a*^{d} mod *p*; if b is not 1 nor −1, then square it repeatedly modulo p until getting 1, −1, or having squared d times. If *b* ≠ 1 and −1 has not been obtained, then p is not prime. Otherwise, p may be prime or not. If p is not prime, the probability that this is proved by the test is higher than 1/4. Therefore, after k non-conclusive random tests, the probability that p is not prime is lower than (3/4)^{k}, and may thus be made as low as desired, by increasing k.

In summary, the test either proves that a number is not prime, or asserts that it is prime with a probability of error that may be chosen as low as desired. The test is very simple to implement and computationally more efficient than all known deterministic tests. Therefore, it is generally used before starting a proof of primality.