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 / V02. Numbers

Mathlib.Data.Nat.Factorization.Defs

44 declarations and 2 deprecated aliases. Mathlib docs · Source

Prime factorizations

n.factorization is the finitely supported function ℕ →₀ ℕ mapping each prime factor of n to its multiplicity in n. For example, since 2000 = 2^4 * 5^3,

  • factorization 2000 2 is 4
  • factorization 2000 5 is 3
  • factorization 2000 k is 0 for all other k : ℕ.

TODO

  • As discussed in this Zulip thread: https://leanprover.zulipchat.com/#narrow/stream/217875/topic/Multiplicity.20in.20the.20naturals We have lots of disparate ways of talking about the multiplicity of a prime in a natural number, including factors.count, padicValNat, multiplicity, and the material in Data/PNat/Factors. Move some of this material to this file, prove results about the relationships between these definitions, and (where appropriate) choose a uniform canonical way of expressing these ideas.

  • Moreover, the results here should be generalised to an arbitrary unique factorization monoid with a normalization function, and then deduplicated. The basics of this have been started in Mathlib/RingTheory/UniqueFactorizationDomain/.

  • Extend the inductions to any NormalizationMonoid with unique factorization.

Basic facts about factorization

Lemmas characterising when n.factorization p = 0

Lemmas about factorizations of products and powers

Criterion for a natural number or integer being a square through even factorization

Lemmas about factorizations of primes and prime powers

Equivalence between ℕ+ and ℕ →₀ ℕ with support in the primes.

Factorization and coprimes

Generalisation of the "even part" and "odd part" of a natural number

Factorization LCM definitions

def Nat.factorization E1 #

ℕ → ℕ →₀ ℕ

n.factorization is the finitely supported function ℕ →₀ ℕ mapping each prime factor of n to its multiplicity in n.

theorem Nat.support_factorization E0 #

∀ (n : ℕ), n.factorization.support = n.primeFactors

The support of n.factorization is exactly n.primeFactors.

theorem Nat.primeFactorsList_count_eq E0 #

∀ {n p : ℕ}, List.count p n.primeFactorsList = n.factorization p

theorem Nat.factorization_def E0 #

∀ (n : ℕ) {p : ℕ}, Nat.Prime p → n.factorization p = multiplicity p n

We can write both n.factorization p and n.factors.count p to represent the power of p in the factorization of n: we declare the former to be the simp-normal form.

theorem Nat.factorization_le_multiplicity E1 #

∀ {n p : ℕ}, n.factorization p ≤ multiplicity p n

theorem Nat.factorization_eq_primeFactorsList_multiset E1 #

∀ (n : ℕ), n.factorization = Multiset.toFinsupp ↑n.primeFactorsList

theorem Nat.Prime.factorization_pos_of_dvd E1 #

∀ {n p : ℕ}, Nat.Prime p → n ≠ 0 → p ∣ n → 0 < n.factorization p

theorem Nat.multiplicity_eq_factorization E1 #

∀ {n p : ℕ}, Nat.Prime p → multiplicity p n = n.factorization p

theorem Nat.prod_factorization_pow_eq_self E0 #

∀ {n : ℕ}, n ≠ 0 → (n.factorization.prod fun x1 x2 ↦ x1 ^ x2) = n

theorem Nat.factorization_inj E1 #

Set.InjOn Nat.factorization {x | x ≠ 0}

Every nonzero natural number has a unique prime factorization

theorem Nat.factorization_zero E0 #

Nat.factorization 0 = 0

theorem Nat.factorization_one E0 #

Nat.factorization 1 = 0

theorem Nat.factorization_eq_zero_iff E0 #

∀ (n p : ℕ), n.factorization p = 0 ↔ ¬Nat.Prime p ∨ ¬p ∣ n ∨ n = 0

theorem Nat.factorization_eq_zero_of_not_prime E0 #

∀ (n : ℕ) {p : ℕ}, ¬Nat.Prime p → n.factorization p = 0

theorem Nat.factorization_zero_right E0 #

∀ (n : ℕ), n.factorization 0 = 0

theorem Nat.factorization_one_right E0 #

∀ (n : ℕ), n.factorization 1 = 0

theorem Nat.factorization_eq_zero_of_not_dvd E1 #

∀ {n p : ℕ}, ¬p ∣ n → n.factorization p = 0

theorem Nat.factorization_eq_zero_of_remainder E1 #

∀ {p r : ℕ} (i : ℕ), ¬p ∣ r → (p * i + r).factorization p = 0

theorem Nat.factorization_mul E0 #

∀ {a b : ℕ}, a ≠ 0 → b ≠ 0 → (a * b).factorization = a.factorization + b.factorization

For nonzero a and b, the power of p in a * b is the sum of the powers in a and b

theorem Nat.factorization_le_iff_dvd E1 #

∀ {d n : ℕ}, d ≠ 0 → n ≠ 0 → (d.factorization ≤ n.factorization ↔ d ∣ n)

theorem Nat.factorization_prod E1 #

∀ {α : Type u_1} {S : Finset α} {g : α → ℕ},
  (∀ x ∈ S, g x ≠ 0) → (S.prod g).factorization = ∑ x ∈ S, (g x).factorization

For any p : ℕ and any function g : α → ℕ that's non-zero on S : Finset α, the power of p in S.prod g equals the sum over x ∈ S of the powers of p in g x. Generalises factorization_mul, which is the special case where #S = 2 and g = id.

theorem Nat.factorization_pow E0 #

∀ (n k : ℕ), (n ^ k).factorization = k • n.factorization

For any p, the power of p in n^k is k times the power in n

theorem Nat.isSquare_iff_even_factorization E1 #

∀ {n : ℕ}, IsSquare n ↔ ∀ (p : ℕ), Nat.Prime p → Even (n.factorization p)

n is a square if and only if for any prime p, the power of p in n is even.

theorem Int.isSquare_iff_nonneg_even_factorization E1 #

∀ {n : ℤ}, IsSquare n ↔ 0 ≤ n ∧ ∀ (p : ℕ), Nat.Prime p → Even (n.natAbs.factorization p)

The integer n is a square if and only if n is nonnegative, and for any p, the power of p in |n| is even.

theorem Nat.Prime.factorization E0 #

∀ {p : ℕ}, Nat.Prime p → p.factorization = fun₀ | p => 1

The only prime factor of prime p is p itself, with multiplicity 1

theorem Nat.Prime.factorization_pow E1 #

∀ {p k : ℕ}, Nat.Prime p → (p ^ k).factorization = fun₀ | p => k

For prime p the only prime factor of p^k is p with multiplicity k

theorem Nat.pow_succ_factorization_not_dvd E1 #

∀ {n p : ℕ}, n ≠ 0 → Nat.Prime p → ¬p ^ (n.factorization p + 1) ∣ n

theorem Nat.factorization_minFac_ne_zero E1 #

∀ {n : ℕ}, 1 < n → n.factorization n.minFac ≠ 0

theorem Nat.factorization_prod_pow_eq_self E1 #

∀ {f : ℕ →₀ ℕ}, (∀ p ∈ f.support, Nat.Prime p) → (f.prod fun x1 x2 ↦ x1 ^ x2).factorization = f

Any Finsupp f : ℕ →₀ ℕ whose support is in the primes is equal to the factorization of the product ∏ (a : ℕ) ∈ f.support, a ^ f a.

theorem Nat.factorization_prod_pow_eq_self_of_le_factorization E1 #

∀ {n : ℕ} {f : ℕ →₀ ℕ}, f ≤ n.factorization → (f.prod fun x1 x2 ↦ x1 ^ x2).factorization = f

theorem Nat.prod_pow_dvd_of_le_factorization E1 #

∀ {n : ℕ} {f : ℕ →₀ ℕ}, f ≤ n.factorization → (f.prod fun x1 x2 ↦ x1 ^ x2) ∣ n

theorem Nat.dvd_prod_pow_of_factorization_le E1 #

∀ {n : ℕ} {f : ℕ →₀ ℕ}, n ≠ 0 → n.factorization ≤ f → n ∣ f.prod fun x1 x2 ↦ x1 ^ x2

theorem Nat.dvd_iff_exists_le_factorization E1 #

∀ {n d : ℕ}, d ≠ 0 → n ≠ 0 → (d ∣ n ↔ ∃ f ≤ n.factorization, d = f.prod fun x1 x2 ↦ x1 ^ x2)

def Nat.factorizationEquiv E1 #

ℕ+ ≃ { f // ∀ p ∈ f.support, Nat.Prime p }

The equiv between ℕ+ and ℕ →₀ ℕ with support in the primes.

theorem Nat.factorizationEquiv_symm_apply_coe E0 #

∀ (x : { f // ∀ p ∈ f.support, Nat.Prime p }), ↑(Nat.factorizationEquiv.symm x) = (↑x).prod fun x1 x2 ↦ x1 ^ x2

theorem Nat.factorizationEquiv_apply E0 #

∀ (x : ℕ+),
  Nat.factorizationEquiv x =
    match x with
    | ⟨n, property⟩ => ⟨n.factorization, ⋯⟩

theorem Nat.factorization_mul_apply_of_coprime E1 #

∀ {p a b : ℕ}, a.Coprime b → (a * b).factorization p = a.factorization p + b.factorization p

For coprime a and b, the power of p in a * b is the sum of the powers in a and b

theorem Nat.factorization_mul_of_coprime E1 #

∀ {a b : ℕ}, a.Coprime b → (a * b).factorization = a.factorization + b.factorization

For coprime a and b, the power of p in a * b is the sum of the powers in a and b

def Nat.«termOrdProj[_]_» E1 #

Lean.ParserDescr

We introduce the notations ordProj[p] n for the largest power of the prime p that divides n and ordCompl[p] n for the complementary part. The ord naming comes from the $p$-adic order/valuation of a number, and proj and compl are for the projection and complementary projection. The term n.factorization p is the $p$-adic order itself. For example, ordProj[2] n is the even part of n and ordCompl[2] n is the odd part.

def Nat.«termOrdCompl[_]_» E1 #

Lean.ParserDescr

We introduce the notations ordProj[p] n for the largest power of the prime p that divides n and ordCompl[p] n for the complementary part. The ord naming comes from the $p$-adic order/valuation of a number, and proj and compl are for the projection and complementary projection. The term n.factorization p is the $p$-adic order itself. For example, ordProj[2] n is the even part of n and ordCompl[2] n is the odd part.

theorem Nat.ordProj_dvd E1 #

∀ (n p : ℕ), p ^ n.factorization p ∣ n

theorem Nat.ordProj_dvd_ordProj_iff_dvd E1 #

∀ {a b : ℕ}, a ≠ 0 → b ≠ 0 → ((∀ (p : ℕ), p ^ a.factorization p ∣ p ^ b.factorization p) ↔ a ∣ b)

def Nat.factorizationLCMLeft E1 #

ℕ → ℕ → ℕ

If a = ∏ pᵢ ^ nᵢ and b = ∏ pᵢ ^ mᵢ, then factorizationLCMLeft = ∏ pᵢ ^ kᵢ, where kᵢ = nᵢ if mᵢ ≤ nᵢ and 0 otherwise. Note that the product is over the divisors of lcm a b, so if one of a or b is 0 then the result is 1.

def Nat.factorizationLCMRight E1 #

ℕ → ℕ → ℕ

If a = ∏ pᵢ ^ nᵢ and b = ∏ pᵢ ^ mᵢ, then factorizationLCMRight = ∏ pᵢ ^ kᵢ, where kᵢ = mᵢ if nᵢ < mᵢ and 0 otherwise. Note that the product is over the divisors of lcm a b, so if one of a or b is 0 then the result is 1.

Note that factorizationLCMRight a b is not factorizationLCMLeft b a: the difference is that in factorizationLCMLeft a b there are the primes whose exponent in a is bigger or equal than the exponent in b, while in factorizationLCMRight a b there are the primes whose exponent in b is strictly bigger than in a. For example factorizationLCMLeft 2 2 = 2, but factorizationLCMRight 2 2 = 1.

Deprecated aliases

NameNew nameSince
Nat.factorization_le_padicValNatNat.factorization_le_multiplicity2026-10-28
Nat.prod_pow_factorization_eq_selfNat.factorization_prod_pow_eq_self2026-09-19