The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V12. Number theory

Mathlib.NumberTheory.FermatPsp

12 declarations and 1 deprecated aliases. Mathlib docs · Source

Fermat Pseudoprimes

In this file we define Fermat pseudoprimes: composite numbers that pass the Fermat primality test. A natural number n passes the Fermat primality test to base b (and is therefore deemed a "probable prime") if n divides b ^ (n - 1) - 1. n is a Fermat pseudoprime to base b if n is a composite number that passes the Fermat primality test to base b and is coprime with b.

Fermat pseudoprimes can also be seen as composite numbers for which Fermat's little theorem holds true.

Numbers which are Fermat pseudoprimes to all bases are known as Carmichael numbers (defined in Mathlib/NumberTheory/CarmichaelNumber.lean).

Main Results

The main definitions for this file are

  • Nat.ProbablePrime: A number n is a probable prime to base b if it passes the Fermat primality test; that is, if n divides b ^ (n - 1) - 1
  • Nat.FermatPsp: A number n is a pseudoprime to base b if it is a probable prime to base b, is composite, and is coprime with b (this last condition is automatically true if n divides b ^ (n - 1) - 1, but some sources include it in the definition).

Note that all composite numbers are pseudoprimes to base 0 and 1, and that the definition of Nat.ProbablePrime in this file implies that all numbers are probable primes to bases 0 and 1, and that 0 and 1 are probable primes to any base.

The main theorems are

  • Nat.exists_infinite_pseudoprimes: there are infinitely many pseudoprimes to any base b ≥ 1

def Nat.ProbablePrime E3 #

ℕ → ℕ → Prop

n is a probable prime to base b if n passes the Fermat primality test; that is, n divides b ^ (n - 1) - 1. This definition implies that all numbers are probable primes to base 0 or 1, and that 0 and 1 are probable primes to any base.

theorem Nat.probablePrime_iff_zmod_one E1 #

∀ (n : ℕ) {b : ℕ}, b ≠ 0 → (n.ProbablePrime b ↔ ↑b ^ (n - 1) = 1)

def Nat.FermatPsp E3 #

ℕ → ℕ → Prop

n is a Fermat pseudoprime to base b if n is a probable prime to base b and is composite. By this definition, all composite natural numbers are pseudoprimes to base 0 and 1. This definition also permits n to be less than b, so that 4 is a pseudoprime to base 5, for example.

instance Nat.decidableProbablePrime E0 #

(n b : ℕ) → Decidable (n.ProbablePrime b)

instance Nat.decidablePsp E0 #

(n b : ℕ) → Decidable (n.FermatPsp b)

theorem Nat.coprime_of_probablePrime E1 #

∀ {n b : ℕ}, n.ProbablePrime b → 1 ≤ n → 1 ≤ b → n.Coprime b

If n passes the Fermat primality test to base b, then n is coprime with b, assuming that n and b are both positive.

theorem Nat.probablePrime_iff_modEq E1 #

∀ (n : ℕ) {b : ℕ}, 1 ≤ b → (n.ProbablePrime b ↔ b ^ (n - 1) ≡ 1 [MOD n])

theorem Nat.coprime_of_fermatPsp E2 #

∀ {n b : ℕ}, n.FermatPsp b → 1 ≤ b → n.Coprime b

If n is a Fermat pseudoprime to base b, then n is coprime with b, assuming that b is positive.

This lemma is a small wrapper based on coprime_of_probablePrime

theorem Nat.fermatPsp_base_one E1 #

∀ {n : ℕ}, 1 < n → ¬Nat.Prime n → n.FermatPsp 1

All composite numbers are Fermat pseudoprimes to base 1.

theorem Nat.exists_infinite_pseudoprimes E3 #

∀ {b : ℕ}, 1 ≤ b → ∀ (m : ℕ), ∃ n, n.FermatPsp b ∧ m ≤ n

For all positive bases, there exist infinitely many Fermat pseudoprimes to that base. Given in this form: for all numbers b ≥ 1 and m, there exists a pseudoprime n to base b such that m ≤ n. This form is similar to Nat.exists_infinite_primes.

theorem Nat.frequently_atTop_fermatPsp E1 #

∀ {b : ℕ}, 1 ≤ b → ∃ᶠ (n : ℕ) in Filter.atTop, n.FermatPsp b

theorem Nat.infinite_setOfPred_pseudoprimes E1 #

∀ {b : ℕ}, 1 ≤ b → {n | n.FermatPsp b}.Infinite

Infinite set variant of Nat.exists_infinite_pseudoprimes

Deprecated aliases

NameNew nameSince
Nat.infinite_setOf_pseudoprimesNat.infinite_setOfPred_pseudoprimes2026-07-09