|
| 1 | +/- |
| 2 | +Copyright (c) 2021 Stuart Presnell. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Stuart Presnell |
| 5 | +-/ |
| 6 | +import data.nat.prime |
| 7 | +import data.nat.mul_ind |
| 8 | + |
| 9 | +/-! |
| 10 | +# Prime factorizations |
| 11 | +
|
| 12 | + `n.factorization` is the finitely supported function `ℕ →₀ ℕ` |
| 13 | + mapping each prime factor of `n` to its multiplicity in `n`. For example, since 2000 = 2^4 * 5^3, |
| 14 | + * `factorization 2000 2` is 4 |
| 15 | + * `factorization 2000 5` is 3 |
| 16 | + * `factorization 2000 k` is 0 for all other `k : ℕ`. |
| 17 | +
|
| 18 | +## TODO |
| 19 | +
|
| 20 | +* As discussed in this Zulip thread: |
| 21 | +https://leanprover.zulipchat.com/#narrow/stream/217875/topic/Multiplicity.20in.20the.20naturals |
| 22 | +We have lots of disparate ways of talking about the multiplicity of a prime |
| 23 | +in a natural number, including `factors.count`, `padic_val_nat`, `multiplicity`, |
| 24 | +and the material in `data/pnat/factors`. Move some of this material to this file, |
| 25 | +prove results about the relationships between these definitions, |
| 26 | +and (where appropriate) choose a uniform canonical way of expressing these ideas. |
| 27 | +
|
| 28 | +* Moreover, the results here should be generalised to an arbitrary unique factorization monoid |
| 29 | +with a normalization function, and then deduplicated. The basics of this have been started in |
| 30 | +`ring_theory/unique_factorization_domain`. |
| 31 | +
|
| 32 | +-/ |
| 33 | + |
| 34 | +open nat finset list finsupp |
| 35 | +open_locale big_operators |
| 36 | + |
| 37 | +namespace nat |
| 38 | + |
| 39 | +/-- `n.factorization` is the finitely supported function `ℕ →₀ ℕ` |
| 40 | + mapping each prime factor of `n` to its multiplicity in `n`. -/ |
| 41 | +noncomputable def factorization (n : ℕ) : ℕ →₀ ℕ := (n.factors : multiset ℕ).to_finsupp |
| 42 | + |
| 43 | +lemma factorization_eq_count {n p : ℕ} : n.factorization p = n.factors.count p := |
| 44 | +by simp [factorization] |
| 45 | +-- TODO: As part of the unification mentioned in the TODO above, |
| 46 | +-- consider making this a [simp] lemma from `n.factors.count` to `n.factorization` |
| 47 | + |
| 48 | +/-- Every nonzero natural number has a unique prime factorization -/ |
| 49 | +lemma factorization_inj : set.inj_on factorization { x : ℕ | x ≠ 0 } := |
| 50 | +λ a ha b hb h, eq_of_count_factors_eq |
| 51 | + (zero_lt_iff.mpr ha) (zero_lt_iff.mpr hb) (λ p, by simp [←factorization_eq_count, h]) |
| 52 | + |
| 53 | +@[simp] lemma factorization_zero : factorization 0 = 0 := |
| 54 | +by simp [factorization] |
| 55 | + |
| 56 | +@[simp] lemma factorization_one : factorization 1 = 0 := |
| 57 | +by simp [factorization] |
| 58 | + |
| 59 | +/-- The support of `n.factorization` is exactly `n.factors.to_finset` -/ |
| 60 | +@[simp] lemma support_factorization {n : ℕ} : |
| 61 | + n.factorization.support = n.factors.to_finset := |
| 62 | +by simpa [factorization, multiset.to_finsupp_support] |
| 63 | + |
| 64 | +lemma factor_iff_mem_factorization {n p : ℕ} : p ∈ n.factorization.support ↔ p ∈ n.factors := |
| 65 | +by simp only [support_factorization, list.mem_to_finset] |
| 66 | + |
| 67 | +/-- The only numbers with empty prime factorization are `0` and `1` -/ |
| 68 | +lemma factorization_eq_zero_iff (n : ℕ) : n.factorization = 0 ↔ n = 0 ∨ n = 1 := |
| 69 | +by simp [factorization, add_equiv.map_eq_zero_iff, multiset.coe_eq_zero] |
| 70 | + |
| 71 | +/-- For nonzero `a` and `b`, the power of `p` in `a * b` is the sum of the powers in `a` and `b` -/ |
| 72 | +@[simp] lemma factorization_mul {a b : ℕ} (ha : a ≠ 0) (hb : b ≠ 0) : |
| 73 | + (a * b).factorization = a.factorization + b.factorization := |
| 74 | +by { ext p, simp only [add_apply, factorization_eq_count, |
| 75 | + count_factors_mul_of_pos (zero_lt_iff.mpr ha) (zero_lt_iff.mpr hb)] } |
| 76 | + |
| 77 | +/-- For any `p`, the power of `p` in `n^k` is `k` times the power in `n` -/ |
| 78 | +lemma factorization_pow {n k : ℕ} : |
| 79 | + factorization (n^k) = k • n.factorization := |
| 80 | +by { ext p, simp [factorization_eq_count, factors_count_pow] } |
| 81 | + |
| 82 | +/-- The only prime factor of prime `p` is `p` itself, with multiplicity `1` -/ |
| 83 | +@[simp] lemma prime.factorization {p : ℕ} (hp : prime p) : |
| 84 | + p.factorization = single p 1 := |
| 85 | +begin |
| 86 | + ext q, |
| 87 | + rw [factorization_eq_count, factors_prime hp, single_apply, count_singleton', if_congr eq_comm]; |
| 88 | + refl, |
| 89 | +end |
| 90 | + |
| 91 | +/-- For prime `p` the only prime factor of `p^k` is `p` with multiplicity `k` -/ |
| 92 | +@[simp] lemma prime.factorization_pow {p k : ℕ} (hp : prime p) : |
| 93 | + factorization (p^k) = single p k := |
| 94 | +by simp [factorization_pow, hp.factorization] |
| 95 | + |
| 96 | +end nat |
0 commit comments