Quelle Defs.lean
Sprache: Lean
|
|
/-
Copyright (c) 2021 Stuart Presnell. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Stuart Presnell
-/
module
public import Mathlib.Data.Finsupp.Multiset
public import Mathlib.Data.Finsupp.Order
public import Mathlib.Data.Nat.PrimeFin
public import Mathlib.NumberTheory.Padics.PadicVal.Defs
import Batteries.Data.List.Count
/-!
# 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.
-/
@[expose] public section
open Nat Finset List Finsupp
namespace Nat
variable {a b n p : ℕ}
/-- `n.factorization` is the finitely supported function `ℕ →₀ ℕ`
mapping each prime factor of `n` to its multiplicity in `n`. -/
def factorization (n : ℕ) : ℕ →₀ ℕ :=
Multiset.toFinsupp n.primeFactorsList
/-- The support of `n.factorization` is exactly `n.primeFactors`. -/
@[simp] lemma support_factorization (n : ℕ) : (factorization n).support = n.primeFactors := rfl
theorem primeFactorsList_count_eq {n p : ℕ} : n.primeFactorsList.count p = n.factorization p := by
simp [factorization]
/-- 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. -/
@[simp]
theorem factorization_def (n : ℕ) {p : ℕ} (pp : p.Prime) :
n.factorization p = multiplicity p n := by
rw [← primeFactorsList_count_eq]
rcases n.eq_zero_or_pos with (rfl | hn0)
· simp [count]
apply _root_.le_antisymm
· rw [le_multiplicity_iff_replicate_subperm_primeFactorsList pp hn0.ne']
exact List.replicate_sublist_iff.mpr le_rfl |>.subperm
· rw [← Nat.lt_add_one_iff, lt_iff_not_ge,
le_multiplicity_iff_replicate_subperm_primeFactorsList pp hn0.ne']
intro h
have := h.count_le p
simp at this
theorem factorization_le_multiplicity {n p : ℕ} : n.factorization p ≤ multiplicity p n := by
by_cases pp : p.Prime
· exact (factorization_def n pp).le
· simp [n.factorization.notMem_support_iff.mp (mt prime_of_mem_primeFactors pp)]
@[deprecated (since := "2026-10-28")] alias factorization_le_padicValNat :=
factorization_le_multiplicity
theorem factorization_eq_primeFactorsList_multiset (n : ℕ) :
n.factorization = Multiset.toFinsupp (n.primeFactorsList : Multiset ℕ) :=
rfl
theorem Prime.factorization_pos_of_dvd {n p : ℕ} (hp : p.Prime) (hn : n ≠ 0) (h : p ∣ n) :
0 < n.factorization p := by
rwa [← primeFactorsList_count_eq, count_pos_iff, mem_primeFactorsList_iff_dvd hn hp]
theorem multiplicity_eq_factorization {n p : ℕ} (pp : p.Prime) :
multiplicity p n = n.factorization p := by
rw [factorization_def n pp]
/-! ### Basic facts about factorization -/
@[simp]
theorem prod_factorization_pow_eq_self {n : ℕ} (hn : n ≠ 0) : n.factorization.prod (· ^ ·) = n := by
rw [factorization_eq_primeFactorsList_multiset n]
simp only [← prod_toMultiset, Multiset.prod_coe, Multiset.toFinsupp_toMultiset]
exact prod_primeFactorsList hn
theorem eq_of_factorization_eq {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0)
(h : ∀ p : ℕ, a.factorization p = b.factorization p) : a = b :=
eq_of_perm_primeFactorsList ha hb
(by simpa only [List.perm_iff_count, primeFactorsList_count_eq] using h)
theorem eq_of_factorization_eq' {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0)
(h : a.factorization = b.factorization) : a = b :=
eq_of_factorization_eq ha hb (congrFun (congrArg DFunLike.coe h))
/-- Every nonzero natural number has a unique prime factorization -/
theorem factorization_inj : Set.InjOn factorization { x : ℕ | x ≠ 0 } := fun a ha b hb h =>
eq_of_factorization_eq ha hb fun p => by simp [h]
@[simp]
theorem factorization_zero : factorization 0 = 0 := by ext; simp [factorization]
@[simp]
theorem factorization_one : factorization 1 = 0 := by ext; simp [factorization]
/-! ## Lemmas characterising when `n.factorization p = 0` -/
theorem factorization_eq_zero_iff (n p : ℕ) :
n.factorization p = 0 ↔ ¬p.Prime ∨ ¬p ∣ n ∨ n = 0 := by
simp_rw [← notMem_support_iff, support_factorization, mem_primeFactors, not_and_or, not_ne_iff]
@[simp]
theorem factorization_eq_zero_of_not_prime (n : ℕ) {p : ℕ} (hp : ¬p.Prime) :
n.factorization p = 0 := by simp [factorization_eq_zero_iff, hp]
@[simp]
theorem factorization_zero_right (n : ℕ) : n.factorization 0 = 0 :=
factorization_eq_zero_of_not_prime _ not_prime_zero
@[simp]
theorem factorization_one_right (n : ℕ) : n.factorization 1 = 0 :=
factorization_eq_zero_of_not_prime _ not_prime_one
theorem factorization_eq_zero_of_not_dvd {n p : ℕ} (h : ¬p ∣ n) : n.factorization p = 0 := by
simp [factorization_eq_zero_iff, h]
theorem factorization_eq_zero_of_remainder {p r : ℕ} (i : ℕ) (hr : ¬p ∣ r) :
(p * i + r).factorization p = 0 := by
apply factorization_eq_zero_of_not_dvd
rwa [← Nat.dvd_add_iff_right (Dvd.intro i rfl)]
/-! ## Lemmas about factorizations of products and powers -/
/-- For nonzero `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/
@[simp]
theorem factorization_mul {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0) :
(a * b).factorization = a.factorization + b.factorization := by
ext p
simp only [Finsupp.add_apply, ← primeFactorsList_count_eq,
perm_iff_count.mp (perm_primeFactorsList_mul ha hb) p, count_append]
theorem factorization_le_iff_dvd {d n : ℕ} (hd : d ≠ 0) (hn : n ≠ 0) :
d.factorization ≤ n.factorization ↔ d ∣ n := by
refine ⟨fun hdn ↦ ?_, fun ⟨c, h⟩ ↦ ?_⟩
· rw [← prod_factorization_pow_eq_self hn, ← prod_factorization_pow_eq_self hd]
exact prod_dvd_prod_of_subset_of_dvd (support_mono hdn) fun a _ ↦ pow_dvd_pow a (hdn a)
· subst h
rw [factorization_mul hd <| right_ne_zero_of_mul hn]
apply self_le_add_right
/-- 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 factorization_prod {α : Type*} {S : Finset α} {g : α → ℕ} (hS : ∀ x ∈ S, g x ≠ 0) :
(S.prod g).factorization = S.sum fun x => (g x).factorization := by
classical
refine Finset.induction_on' S ?_ ?_
· simp
· intro x T hxS hTS hxT IH
have hT : T.prod g ≠ 0 := prod_ne_zero_iff.mpr fun x hx => hS x (hTS hx)
simp [prod_insert hxT, sum_insert hxT, IH, factorization_mul (hS x hxS) hT]
/-- For any `p`, the power of `p` in `n^k` is `k` times the power in `n` -/
@[simp]
theorem factorization_pow (n k : ℕ) : factorization (n ^ k) = k • n.factorization := by
induction k with
| zero => simp
| succ k ih =>
rcases eq_or_ne n 0 with (rfl | hn)
· simp
rw [Nat.pow_succ, mul_comm, factorization_mul hn (pow_ne_zero _ hn), ih,
add_smul, one_smul, add_comm]
/-! ## Criterion for a natural number or integer being a square through even factorization -/
/-- `n` is a square if and only if for any prime `p`, the power of `p` in `n` is even. -/
theorem isSquare_iff_even_factorization {n : ℕ} :
IsSquare n ↔ ∀ (p : ℕ), p.Prime → Even (n.factorization p) := by
by_cases h0 : n = 0
· simp [h0]
constructor
· intro ⟨m, hmn⟩ p hp
have hm : m ≠ 0 := mul_self_ne_zero.mp (hmn ▸ h0)
simp [hmn, factorization_mul hm hm]
· refine fun h ↦ ⟨n.factorization.prod fun a b ↦ a ^ (b / 2), ?_⟩
rw [← pow_two, ← powMonoidHom_apply, map_finsuppProd]
nth_rw 1 [← prod_factorization_pow_eq_self h0]
refine Finsupp.prod_congr fun p hp ↦ ?_
rw [powMonoidHom_apply, ← pow_mul, div_two_mul_two_of_even (h p (prime_of_mem_primeFactors hp))]
end Nat
namespace Int
/-- 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 isSquare_iff_nonneg_even_factorization {n : ℤ} :
IsSquare n ↔ 0 ≤ n ∧ ∀ (p : ℕ), p.Prime → Even (n.natAbs.factorization p) := by
constructor
· intro ⟨m, hmn⟩
exact ⟨by simpa [hmn] using Int.mul_nonneg_of_nonneg_or_nonpos (by omega),
fun p hp ↦ isSquare_iff_even_factorization.mp ⟨m.natAbs, by grind⟩ p hp⟩
· intro ⟨h0, h⟩
obtain ⟨r, hr⟩ := Nat.isSquare_iff_even_factorization.mpr h
exact ⟨r, (by rw [← natAbs_of_nonneg h0, hr, Nat.cast_mul])⟩
end Int
/-! ## Lemmas about factorizations of primes and prime powers -/
namespace Nat
variable {a b m n p : ℕ}
/-- The only prime factor of prime `p` is `p` itself, with multiplicity `1` -/
@[simp]
protected theorem Prime.factorization {p : ℕ} (hp : Prime p) : p.factorization = single p 1 := by
ext q
rw [← primeFactorsList_count_eq, primeFactorsList_prime hp, single_apply, count_singleton',
if_congr eq_comm] <;> rfl
/-- For prime `p` the only prime factor of `p^k` is `p` with multiplicity `k` -/
theorem Prime.factorization_pow {p k : ℕ} (hp : Prime p) : (p ^ k).factorization = single p k := by
simp [hp]
theorem pow_succ_factorization_not_dvd {n p : ℕ} (hn : n ≠ 0) (hp : p.Prime) :
¬p ^ (n.factorization p + 1) ∣ n := by
intro h
rw [← factorization_le_iff_dvd (pow_ne_zero _ hp.ne_zero) hn] at h
simpa [hp.factorization] using h p
lemma factorization_minFac_ne_zero {n : ℕ} (hn : 1 < n) :
n.factorization n.minFac ≠ 0 := by
refine mt (factorization_eq_zero_iff _ _).mp ?_
push Not
exact ⟨minFac_prime (by lia), minFac_dvd n, Nat.ne_zero_of_lt hn⟩
/-! ### Equivalence between `ℕ+` and `ℕ →₀ ℕ` with support in the primes. -/
variable {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 factorization_prod_pow_eq_self (hf : ∀ p ∈ f.support, Prime p) :
(f.prod (· ^ ·)).factorization = f := by
rw [Finsupp.prod, factorization_prod (pow_ne_zero _ <| hf · · |>.ne_zero),
sum_congr rfl (hf · · |>.factorization_pow)]
exact sum_single f
@[deprecated (since := "2026-09-19")]
alias prod_pow_factorization_eq_self := factorization_prod_pow_eq_self
theorem eq_factorization_iff (hn : n ≠ 0) (hf : ∀ p ∈ f.support, Prime p) :
f = n.factorization ↔ f.prod (· ^ ·) = n := by
constructor <;> rintro rfl
exacts [prod_factorization_pow_eq_self hn, factorization_prod_pow_eq_self hf |>.symm]
theorem factorization_prod_pow_eq_self_of_le_factorization (hf : f ≤ n.factorization) :
(f.prod (· ^ ·)).factorization = f :=
factorization_prod_pow_eq_self fun _ hp ↦ prime_of_mem_primeFactors <| support_mono hf hp
theorem prod_pow_dvd_of_le_factorization (hf : f ≤ n.factorization) : f.prod (· ^ ·) ∣ n := by
rcases eq_or_ne n 0 with (rfl | hn)
· simp
rwa [← factorization_le_iff_dvd ?_ hn, factorization_prod_pow_eq_self_of_le_factorization hf]
refine f.prod_ne_zero_iff.mpr fun _ hp ↦ ?_
exact pow_ne_zero _ (prime_of_mem_primeFactors <| support_mono hf hp).ne_zero
theorem dvd_prod_pow_of_factorization_le (hn : n ≠ 0) (hf : n.factorization ≤ f) :
n ∣ f.prod (· ^ ·) := by
rw [← add_tsub_cancel_of_le hf, Finsupp.prod_add_index' (by simp) Nat.pow_add,
prod_factorization_pow_eq_self hn]
apply n.dvd_mul_right
theorem dvd_iff_exists_le_factorization {d : ℕ} (hd : d ≠ 0) (hn : n ≠ 0) :
d ∣ n ↔ ∃ f ≤ n.factorization, d = f.prod (· ^ ·) := by
rw [← factorization_le_iff_dvd hd hn]
refine ⟨fun h ↦ ⟨_, h, prod_factorization_pow_eq_self hd |>.symm⟩, fun ⟨f, hle, hprod⟩ ↦ ?_⟩
rwa [hprod, factorization_prod_pow_eq_self_of_le_factorization hle]
/-- The equiv between `ℕ+` and `ℕ →₀ ℕ` with support in the primes. -/
@[simps]
def factorizationEquiv : ℕ+ ≃ { f : ℕ →₀ ℕ // ∀ p ∈ f.support, Prime p } where
toFun := fun ⟨n, _⟩ => ⟨n.factorization, fun _ => prime_of_mem_primeFactors⟩
invFun := fun ⟨f, hf⟩ =>
⟨f.prod _, prod_pow_pos_of_zero_notMem_support fun H => not_prime_zero (hf 0 H)⟩
left_inv := fun ⟨_, hx⟩ => Subtype.ext <| prod_factorization_pow_eq_self hx.ne.symm
right_inv := fun ⟨_, hf⟩ => Subtype.ext <| factorization_prod_pow_eq_self hf
/-! ### Factorization and coprimes -/
/-- For coprime `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/
theorem factorization_mul_apply_of_coprime {p a b : ℕ} (hab : Coprime a b) :
(a * b).factorization p = a.factorization p + b.factorization p := by
simp only [← primeFactorsList_count_eq,
perm_iff_count.mp (perm_primeFactorsList_mul_of_coprime hab), count_append]
/-- For coprime `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/
theorem factorization_mul_of_coprime {a b : ℕ} (hab : Coprime a b) :
(a * b).factorization = a.factorization + b.factorization := by
ext q
rw [Finsupp.add_apply, factorization_mul_apply_of_coprime hab]
/-! ### Generalisation of the "even part" and "odd part" of a natural number -/
/-- 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. -/
notation "ordProj[" p "] " n:arg => p ^ Nat.factorization n p
@[inherit_doc «termOrdProj[_]_»]
notation "ordCompl[" p "] " n:arg => n / ordProj[p] n
theorem ordProj_dvd (n p : ℕ) : ordProj[p] n ∣ n := by
if hp : p.Prime then ?_ else simp [hp]
rw [← primeFactorsList_count_eq]
apply dvd_of_primeFactorsList_subperm (pow_ne_zero _ hp.ne_zero)
rw [hp.primeFactorsList_pow, List.subperm_ext_iff]
intro q hq
simp [List.eq_of_mem_replicate hq]
lemma ordProj_dvd_ordProj_iff_dvd (ha : a ≠ 0) (hb : b ≠ 0) :
(∀ p : ℕ, ordProj[p] a ∣ ordProj[p] b) ↔ a ∣ b := by
rw [← factorization_le_iff_dvd ha hb, Finsupp.le_def]
congr! 1 with p
obtain _ | _ | p := p <;> simp [Nat.pow_dvd_pow_iff_le_right]
/-! ### Factorization LCM definitions -/
/-- 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 factorizationLCMLeft (a b : ℕ) : ℕ :=
(Nat.lcm a b).factorization.prod fun p n ↦
if b.factorization p ≤ a.factorization p then p ^ n else 1
/-- 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`. -/
def factorizationLCMRight (a b : ℕ) :=
(Nat.lcm a b).factorization.prod fun p n ↦
if b.factorization p ≤ a.factorization p then 1 else p ^ n
end Nat
¤ Dauer der Verarbeitung: 0.5 Sekunden
(vorverarbeitet am 2026-11-24)
¤
*© Formatika GbR, Deutschland
|
|
2026-11-24
|