doc-next-gen

Mathlib.Data.Nat.Prime.Defs

Module docstring

{"# Prime numbers

This file deals with prime numbers: natural numbers p ≥ 2 whose only divisors are p and 1.

Important declarations

  • Nat.Prime: the predicate that expresses that a natural number p is prime
  • Nat.Primes: the subtype of natural numbers that are prime
  • Nat.minFac n: the minimal prime factor of a natural number n ≠ 1
  • Nat.prime_iff: Nat.Prime coincides with the general definition of Prime
  • Nat.irreducible_iff_nat_prime: a non-unit natural number is only divisible by 1 iff it is prime

"}

Nat.Prime definition
(p : ℕ)
Full source
/-- `Nat.Prime p` means that `p` is a prime number, that is, a natural number
  at least 2 whose only divisors are `p` and `1`.
  The theorem `Nat.prime_def` witnesses this description of a prime number. -/
@[pp_nodot]
def Prime (p : ) :=
  Irreducible p
Prime natural number
Informal description
A natural number \( p \) is prime if it is irreducible, meaning \( p \geq 2 \) and its only positive divisors are \( 1 \) and \( p \) itself.
Nat.not_prime_zero theorem
: ¬Prime 0
Full source
theorem not_prime_zero : ¬ Prime 0
  | h => h.ne_zero rfl
Zero is Not Prime
Informal description
The natural number $0$ is not prime.
Nat.prime_zero_false theorem
: Prime 0 → False
Full source
/-- A copy of `not_prime_zero` stated in a way that works for `aesop`.

See https://github.com/leanprover-community/aesop/issues/197 for an explanation. -/
@[aesop safe destruct] theorem prime_zero_false : Prime 0 → False :=
  not_prime_zero
Contradiction from Prime Zero
Informal description
The statement that $0$ is a prime number implies a contradiction, i.e., $\text{Prime}(0) \to \text{False}$.
Nat.not_prime_one theorem
: ¬Prime 1
Full source
theorem not_prime_one : ¬ Prime 1
  | h => h.ne_one rfl
Non-primality of One
Informal description
The natural number $1$ is not prime.
Nat.prime_one_false theorem
: Prime 1 → False
Full source
/-- A copy of `not_prime_one` stated in a way that works for `aesop`.

See https://github.com/leanprover-community/aesop/issues/197 for an explanation. -/
@[aesop safe destruct] theorem prime_one_false : Prime 1 → False :=
  not_prime_one
Contradiction from Prime One
Informal description
The statement that $1$ is a prime number implies a contradiction, i.e., $\text{Prime}(1) \to \text{False}$.
Nat.Prime.ne_zero theorem
{n : ℕ} (h : Prime n) : n ≠ 0
Full source
theorem Prime.ne_zero {n : } (h : Prime n) : n ≠ 0 :=
  Irreducible.ne_zero h
Nonzero Property of Prime Numbers
Informal description
For any prime natural number $n$, we have $n \neq 0$.
Nat.Prime.pos theorem
{p : ℕ} (pp : Prime p) : 0 < p
Full source
theorem Prime.pos {p : } (pp : Prime p) : 0 < p :=
  Nat.pos_of_ne_zero pp.ne_zero
Positivity of Prime Numbers
Informal description
For any prime natural number $p$, we have $0 < p$.
Nat.Prime.two_le theorem
: ∀ {p : ℕ}, Prime p → 2 ≤ p
Full source
theorem Prime.two_le : ∀ {p : }, Prime p → 2 ≤ p
  | 0, h => (not_prime_zero h).elim
  | 1, h => (not_prime_one h).elim
  | _ + 2, _ => le_add_left 2 _
Lower Bound for Prime Numbers: $2 \leq p$
Informal description
For any prime natural number $p$, we have $2 \leq p$.
Nat.Prime.one_lt theorem
{p : ℕ} : Prime p → 1 < p
Full source
theorem Prime.one_lt {p : } : Prime p → 1 < p :=
  Prime.two_le
Prime Numbers Greater Than One: $1 < p$
Informal description
For any prime natural number $p$, we have $1 < p$.
Nat.Prime.one_le theorem
{p : ℕ} (hp : p.Prime) : 1 ≤ p
Full source
lemma Prime.one_le {p : } (hp : p.Prime) : 1 ≤ p := hp.one_lt.le
Primes are at least one: $1 \leq p$
Informal description
For any prime natural number $p$, we have $1 \leq p$.
Nat.Prime.one_lt' instance
(p : ℕ) [hp : Fact p.Prime] : Fact (1 < p)
Full source
instance Prime.one_lt' (p : ) [hp : Fact p.Prime] : Fact (1 < p) :=
  ⟨hp.1.one_lt⟩
Primes are Greater Than One
Informal description
For any prime natural number $p$, we have $1 < p$.
Nat.Prime.ne_one theorem
{p : ℕ} (hp : p.Prime) : p ≠ 1
Full source
theorem Prime.ne_one {p : } (hp : p.Prime) : p ≠ 1 :=
  hp.one_lt.ne'
Prime Numbers Are Not One
Informal description
For any prime natural number $p$, we have $p \neq 1$.
Nat.prime_def theorem
{p : ℕ} : Prime p ↔ 2 ≤ p ∧ ∀ m, m ∣ p → m = 1 ∨ m = p
Full source
@[inherit_doc Nat.Prime]
theorem prime_def {p : } : PrimePrime p ↔ 2 ≤ p ∧ ∀ m, m ∣ p → m = 1 ∨ m = p := by
  refine ⟨fun h => ⟨h.two_le, h.eq_one_or_self_of_dvd⟩, fun h => ?_⟩
  have h1 := Nat.one_lt_two.trans_le h.1
  refine ⟨mt Nat.isUnit_iff.mp h1.ne', ?_⟩
  rintro a b rfl
  simp only [Nat.isUnit_iff]
  refine (h.2 a <| dvd_mul_right ..).imp_right fun hab ↦ ?_
  rw [← mul_right_inj' (Nat.ne_zero_of_lt h1), ← hab, ← hab, mul_one]
Characterization of Prime Numbers via Divisibility
Informal description
A natural number $p$ is prime if and only if $p \geq 2$ and for every natural number $m$ dividing $p$, either $m = 1$ or $m = p$.
Nat.prime_def_lt theorem
{p : ℕ} : Prime p ↔ 2 ≤ p ∧ ∀ m < p, m ∣ p → m = 1
Full source
theorem prime_def_lt {p : } : PrimePrime p ↔ 2 ≤ p ∧ ∀ m < p, m ∣ p → m = 1 :=
  prime_def.trans <|
    and_congr_right fun p2 =>
      forall_congr' fun _ =>
        ⟨fun h l d => (h d).resolve_right (ne_of_lt l), fun h d =>
          (le_of_dvd (le_of_succ_le p2) d).lt_or_eq_dec.imp_left fun l => h l d⟩
Characterization of Prime Numbers via Divisibility by Smaller Numbers
Informal description
A natural number $p$ is prime if and only if $p \geq 2$ and for every natural number $m$ less than $p$, if $m$ divides $p$ then $m = 1$.
Nat.prime_def_lt' theorem
{p : ℕ} : Prime p ↔ 2 ≤ p ∧ ∀ m, 2 ≤ m → m < p → ¬m ∣ p
Full source
theorem prime_def_lt' {p : } : PrimePrime p ↔ 2 ≤ p ∧ ∀ m, 2 ≤ m → m < p → ¬m ∣ p :=
  prime_def_lt.trans <|
    and_congr_right fun p2 =>
      forall_congr' fun m =>
        ⟨fun h m2 l d => not_lt_of_ge m2 ((h l d).symm ▸ by decide), fun h l d => by
          rcases m with (_ | _ | m)
          · rw [eq_zero_of_zero_dvd d] at p2
            revert p2
            decide
          · rfl
          · exact (h (le_add_left 2 m) l).elim d⟩
Characterization of Prime Numbers via Non-Divisibility by Numbers Between 2 and $p-1$
Informal description
A natural number $p$ is prime if and only if $p \geq 2$ and for every natural number $m$ with $2 \leq m < p$, $m$ does not divide $p$.
Nat.prime_def_le_sqrt theorem
{p : ℕ} : Prime p ↔ 2 ≤ p ∧ ∀ m, 2 ≤ m → m ≤ sqrt p → ¬m ∣ p
Full source
theorem prime_def_le_sqrt {p : } : PrimePrime p ↔ 2 ≤ p ∧ ∀ m, 2 ≤ m → m ≤ sqrt p → ¬m ∣ p :=
  prime_def_lt'.trans <|
    and_congr_right fun p2 =>
      ⟨fun a m m2 l => a m m2 <| lt_of_le_of_lt l <| sqrt_lt_self p2, fun a =>
        have : ∀ {m k : ℕ}, m ≤ k → 1 < m → p ≠ m * k := fun {m k} mk m1 e =>
          a m m1 (le_sqrt.2 (e.symm ▸ Nat.mul_le_mul_left m mk)) ⟨k, e⟩
        fun m m2 l ⟨k, e⟩ => by
        rcases le_total m k with mk | km
        · exact this mk m2 e
        · rw [mul_comm] at e
          refine this km (Nat.lt_of_mul_lt_mul_right (a := m) ?_) e
          rwa [one_mul, ← e]⟩
Primality Criterion via Non-Divisibility up to Square Root
Informal description
A natural number $p$ is prime if and only if $p \geq 2$ and for every natural number $m$ with $2 \leq m \leq \sqrt{p}$, $m$ does not divide $p$.
Nat.prime_of_coprime theorem
(n : ℕ) (h1 : 1 < n) (h : ∀ m < n, m ≠ 0 → n.Coprime m) : Prime n
Full source
theorem prime_of_coprime (n : ) (h1 : 1 < n) (h : ∀ m < n, m ≠ 0 → n.Coprime m) : Prime n := by
  refine prime_def_lt.mpr ⟨h1, fun m mlt mdvd => ?_⟩
  have hm : m ≠ 0 := by
    rintro rfl
    rw [zero_dvd_iff] at mdvd
    exact mlt.ne' mdvd
  exact (h m mlt hm).symm.eq_one_of_dvd mdvd
Primality via Coprimality with All Smaller Nonzero Numbers
Informal description
Let $n$ be a natural number greater than $1$ such that for every natural number $m$ with $m < n$ and $m \neq 0$, $n$ is coprime with $m$. Then $n$ is a prime number.
Nat.decidablePrime instance
(p : ℕ) : Decidable (Prime p)
Full source
/--
This instance is set up to work in the kernel (`by decide`) for small values.

Below (`decidablePrime'`) we will define a faster variant to be used by the compiler
(e.g. in `#eval` or `by native_decide`).

If you need to prove that a particular number is prime, in any case
you should not use `by decide`, but rather `by norm_num`, which is
much faster.
-/
instance decidablePrime (p : ) : Decidable (Prime p) :=
  decidable_of_iff' _ prime_def_lt'
Decidability of Primality for Natural Numbers
Informal description
For any natural number $p$, the property of being prime is decidable.
Nat.prime_two theorem
: Prime 2
Full source
theorem prime_two : Prime 2 := by decide
Primality of Two
Informal description
The natural number $2$ is prime.
Nat.prime_three theorem
: Prime 3
Full source
theorem prime_three : Prime 3 := by decide
Primality of 3
Informal description
The natural number $3$ is prime.
Nat.prime_five theorem
: Prime 5
Full source
theorem prime_five : Prime 5 := by decide
Primality of Five
Informal description
The natural number $5$ is prime.
Nat.prime_seven theorem
: Prime 7
Full source
theorem prime_seven : Prime 7 := by decide
Primality of Seven
Informal description
The natural number $7$ is prime.
Nat.prime_eleven theorem
: Prime 11
Full source
theorem prime_eleven : Prime 11 := by decide
Primality of Eleven
Informal description
The natural number $11$ is prime.
Nat.minFac_lemma theorem
(n k : ℕ) (h : ¬n < k * k) : sqrt n - k < sqrt n + 2 - k
Full source
theorem minFac_lemma (n k : ) (h : ¬n < k * k) : sqrt n - k < sqrt n + 2 - k :=
  (Nat.sub_lt_sub_right <| le_sqrt.2 <| le_of_not_gt h) <| Nat.lt_add_of_pos_right (by decide)
Inequality for Square Root and Integer Shift
Informal description
For any natural numbers $n$ and $k$, if $n$ is not less than $k^2$, then the difference between $\sqrt{n}$ and $k$ is less than the difference between $\sqrt{n} + 2$ and $k$. That is, $\sqrt{n} - k < \sqrt{n} + 2 - k$.
Nat.minFacAux definition
(n : ℕ) : ℕ → ℕ
Full source
/--
If `n < k * k`, then `minFacAux n k = n`, if `k | n`, then `minFacAux n k = k`.
Otherwise, `minFacAux n k = minFacAux n (k+2)` using well-founded recursion.
If `n` is odd and `1 < n`, then `minFacAux n 3` is the smallest prime factor of `n`.

By default this well-founded recursion would be irreducible.
This prevents use `decide` to resolve `Nat.prime n` for small values of `n`,
so we mark this as `@[semireducible]`.

In future, we may want to remove this annotation and instead use `norm_num` instead of `decide`
in these situations.
-/
@[semireducible] def minFacAux (n : ) : 
  | k =>
    if n < k * k then n
    else
      if k ∣ n then k
      else
        minFacAux n (k + 2)
termination_by k => sqrt n + 2 - k
decreasing_by simp_wf; apply minFac_lemma n k; assumption
Auxiliary function for minimal prime factor
Informal description
The auxiliary function `minFacAux n k` computes the smallest prime factor of an odd natural number `n > 1` starting from `k`. If `n < k * k`, it returns `n` (indicating `n` is prime). If `k` divides `n`, it returns `k` (indicating `k` is the smallest prime factor). Otherwise, it recursively checks `k + 2`.
Nat.minFac definition
(n : ℕ) : ℕ
Full source
/-- Returns the smallest prime factor of `n ≠ 1`. -/
def minFac (n : ) :  :=
  if 2 ∣ n then 2 else minFacAux n 3
Minimal prime factor of a natural number
Informal description
The function `Nat.minFac` returns the smallest prime factor of a natural number $n \neq 1$. If $2$ divides $n$, it returns $2$; otherwise, it uses the auxiliary function `minFacAux` starting from $3$ to find the minimal prime factor.
Nat.minFac_zero theorem
: minFac 0 = 2
Full source
@[simp]
theorem minFac_zero : minFac 0 = 2 :=
  rfl
Minimal Prime Factor of Zero is Two
Informal description
The minimal prime factor of the natural number $0$ is $2$, i.e., $\text{minFac}(0) = 2$.
Nat.minFac_one theorem
: minFac 1 = 1
Full source
@[simp]
theorem minFac_one : minFac 1 = 1 := by
  simp [minFac, minFacAux]
Minimal Prime Factor of One is One
Informal description
The minimal prime factor of the natural number $1$ is $1$, i.e., $\text{minFac}(1) = 1$.
Nat.minFac_two theorem
: minFac 2 = 2
Full source
@[simp]
theorem minFac_two : minFac 2 = 2 := by
  simp [minFac, minFacAux]
Minimal Prime Factor of Two is Two
Informal description
The minimal prime factor of the natural number $2$ is $2$, i.e., $\text{minFac}(2) = 2$.
Nat.minFac_eq theorem
(n : ℕ) : minFac n = if 2 ∣ n then 2 else minFacAux n 3
Full source
theorem minFac_eq (n : ) : minFac n = if 2 ∣ n then 2 else minFacAux n 3 := rfl
Minimal Prime Factor Formula: $\text{minFac}(n) = \text{if } 2 \mid n \text{ then } 2 \text{ else } \text{minFacAux}(n, 3)$
Informal description
For any natural number $n$, the minimal prime factor of $n$ is equal to $2$ if $2$ divides $n$, and otherwise it is equal to the result of the auxiliary function `minFacAux` starting from $3$.
Nat.minFacAux_has_prop theorem
{n : ℕ} (n2 : 2 ≤ n) : ∀ k i, k = 2 * i + 3 → (∀ m, 2 ≤ m → m ∣ n → k ≤ m) → minFacProp n (minFacAux n k)
Full source
theorem minFacAux_has_prop {n : } (n2 : 2 ≤ n) :
    ∀ k i, k = 2 * i + 3 → (∀ m, 2 ≤ m → m ∣ n → k ≤ m) → minFacProp n (minFacAux n k)
  | k => fun i e a => by
    rw [minFacAux]
    by_cases h : n < k * k
    · have pp : Prime n :=
        prime_def_le_sqrt.2
          ⟨n2, fun m m2 l d => not_lt_of_ge l <| lt_of_lt_of_le (sqrt_lt.2 h) (a m m2 d)⟩
      simpa only [k, h] using
        ⟨n2, dvd_rfl, fun m m2 d => le_of_eq ((dvd_prime_two_le pp m2).1 d).symm⟩
    have k2 : 2 ≤ k := by
      subst e
      apply Nat.le_add_left
    simp only [k, h, ↓reduceIte]
    by_cases dk : k ∣ n <;> simp only [k, dk, ↓reduceIte]
    · exact ⟨k2, dk, a⟩
    · refine
        have := minFac_lemma n k h
        minFacAux_has_prop n2 (k + 2) (i + 1) (by simp [k, e, Nat.left_distrib, add_right_comm])
          fun m m2 d => ?_
      rcases Nat.eq_or_lt_of_le (a m m2 d) with me | ml
      · subst me
        contradiction
      apply (Nat.eq_or_lt_of_le ml).resolve_left
      intro me
      rw [← me, e] at d
      have d' : 2 * (i + 2) ∣ n := d
      have := a _ le_rfl (dvd_of_mul_right_dvd d')
      rw [e] at this
      exact absurd this (by contradiction)
  termination_by k => sqrt n + 2 - k
Minimal Prime Factor Property for Auxiliary Function
Informal description
For any natural number $n \geq 2$, and for any natural numbers $k$ and $i$ such that $k = 2i + 3$, if every divisor $m$ of $n$ with $2 \leq m$ satisfies $k \leq m$, then the auxiliary function `minFacAux n k` computes a minimal prime factor of $n$ (i.e., satisfies the property `minFacProp n`).
Nat.minFac_has_prop theorem
{n : ℕ} (n1 : n ≠ 1) : minFacProp n (minFac n)
Full source
theorem minFac_has_prop {n : } (n1 : n ≠ 1) : minFacProp n (minFac n) := by
  by_cases n0 : n = 0
  · simp [n0, minFacProp, GE.ge]
  have n2 : 2 ≤ n := by
    revert n0 n1
    rcases n with (_ | _ | _) <;> simp [succ_le_succ]
  simp only [minFac_eq, Nat.isUnit_iff]
  by_cases d2 : 2 ∣ n <;> simp only [d2, ↓reduceIte]
  · exact ⟨le_rfl, d2, fun k k2 _ => k2⟩
  · refine
      minFacAux_has_prop n2 3 0 rfl fun m m2 d => (Nat.eq_or_lt_of_le m2).resolve_left (mt ?_ d2)
    exact fun e => e.symm ▸ d
Minimal Prime Factor Property for Natural Numbers
Informal description
For any natural number $n \neq 1$, the minimal prime factor of $n$ (computed by `minFac n`) satisfies the property `minFacProp n`, meaning it is indeed the smallest prime factor of $n$.
Nat.minFac_le_of_dvd theorem
{n : ℕ} : ∀ {m : ℕ}, 2 ≤ m → m ∣ n → minFac n ≤ m
Full source
theorem minFac_le_of_dvd {n : } : ∀ {m : }, 2 ≤ m → m ∣ nminFac n ≤ m := by
  by_cases n1 : n = 1
  · exact fun m2 _ => n1.symmle_trans (by simp) m2
  · apply (minFac_has_prop n1).2.2
Minimal Prime Factor is the Smallest Divisor $\geq 2$
Informal description
For any natural number $n$ and any $m \geq 2$ such that $m$ divides $n$, the minimal prime factor of $n$ is less than or equal to $m$.
Nat.minFac_pos theorem
(n : ℕ) : 0 < minFac n
Full source
theorem minFac_pos (n : ) : 0 < minFac n := by
  by_cases n1 : n = 1
  · simp [n1]
  · exact (minFac_prime n1).pos
Positivity of Minimal Prime Factor
Informal description
For any natural number $n$, the minimal prime factor of $n$ is positive, i.e., $\text{minFac}(n) > 0$.
Nat.minFac_le theorem
{n : ℕ} (H : 0 < n) : minFac n ≤ n
Full source
theorem minFac_le {n : } (H : 0 < n) : minFac n ≤ n :=
  le_of_dvd H (minFac_dvd n)
Minimal Prime Factor Bound: $\text{minFac}(n) \leq n$ for $n > 0$
Informal description
For any positive natural number $n$, the minimal prime factor of $n$ is less than or equal to $n$, i.e., $\text{minFac}(n) \leq n$.
Nat.le_minFac theorem
{m n : ℕ} : n = 1 ∨ m ≤ minFac n ↔ ∀ p, Prime p → p ∣ n → m ≤ p
Full source
theorem le_minFac {m n : } : n = 1 ∨ m ≤ minFac nn = 1 ∨ m ≤ minFac n ↔ ∀ p, Prime p → p ∣ n → m ≤ p :=
  ⟨fun h p pp d =>
    h.elim (by rintro rfl; cases pp.not_dvd_one d) fun h =>
      le_trans h <| minFac_le_of_dvd pp.two_le d,
    fun H => or_iff_not_imp_left.2 fun n1 => H _ (minFac_prime n1) (minFac_dvd _)⟩
Characterization of Minimal Prime Factor via Divisibility by Primes
Informal description
For natural numbers $m$ and $n$, the following are equivalent: 1. Either $n = 1$ or $m \leq \text{minFac}(n)$ (the minimal prime factor of $n$). 2. For every prime number $p$ dividing $n$, we have $m \leq p$.
Nat.le_minFac' theorem
{m n : ℕ} : n = 1 ∨ m ≤ minFac n ↔ ∀ p, 2 ≤ p → p ∣ n → m ≤ p
Full source
theorem le_minFac' {m n : } : n = 1 ∨ m ≤ minFac nn = 1 ∨ m ≤ minFac n ↔ ∀ p, 2 ≤ p → p ∣ n → m ≤ p :=
  ⟨fun h p (pp : 1 < p) d =>
    h.elim (by rintro rfl; cases not_le_of_lt pp (le_of_dvd (by decide) d)) fun h =>
      le_trans h <| minFac_le_of_dvd pp d,
    fun H => le_minFac.2 fun p pp d => H p pp.two_le d⟩
Characterization of Minimal Prime Factor via Divisibility by Integers $\geq 2$
Informal description
For natural numbers $m$ and $n$, the following are equivalent: 1. Either $n = 1$ or $m \leq \text{minFac}(n)$ (the minimal prime factor of $n$). 2. For every integer $p \geq 2$ dividing $n$, we have $m \leq p$.
Nat.prime_def_minFac theorem
{p : ℕ} : Prime p ↔ 2 ≤ p ∧ minFac p = p
Full source
theorem prime_def_minFac {p : } : PrimePrime p ↔ 2 ≤ p ∧ minFac p = p :=
  ⟨fun pp =>
    ⟨pp.two_le,
      let ⟨f2, fd, _⟩ := minFac_has_prop <| ne_of_gt pp.one_lt
      ((dvd_prime pp).1 fd).resolve_left (ne_of_gt f2)⟩,
    fun ⟨p2, e⟩ => e ▸ minFac_prime (ne_of_gt p2)⟩
Characterization of Prime Numbers via Minimal Factor
Informal description
A natural number $p$ is prime if and only if $2 \leq p$ and the minimal prime factor of $p$ is $p$ itself.
Nat.Prime.minFac_eq theorem
{p : ℕ} (hp : Prime p) : minFac p = p
Full source
@[simp]
theorem Prime.minFac_eq {p : } (hp : Prime p) : minFac p = p :=
  (prime_def_minFac.1 hp).2
Minimal Prime Factor of a Prime Number is Itself
Informal description
For any prime natural number $p$, the minimal prime factor of $p$ is $p$ itself, i.e., $\mathrm{minFac}(p) = p$.
Nat.decidablePrime' definition
(p : ℕ) : Decidable (Prime p)
Full source
/--
This definition is faster in the virtual machine than `decidablePrime`,
but slower in the kernel.
-/
def decidablePrime' (p : ) : Decidable (Prime p) :=
  decidable_of_iff' _ prime_def_minFac
Decidability of primality via minimal factor
Informal description
The function `Nat.decidablePrime'` is a decidable predicate that determines whether a natural number \( p \) is prime, using the characterization that \( p \) is prime if and only if \( 2 \leq p \) and the minimal prime factor of \( p \) is \( p \) itself.
Nat.not_prime_iff_minFac_lt theorem
{n : ℕ} (n2 : 2 ≤ n) : ¬Prime n ↔ minFac n < n
Full source
theorem not_prime_iff_minFac_lt {n : } (n2 : 2 ≤ n) : ¬Prime n¬Prime n ↔ minFac n < n :=
  (not_congr <| prime_def_minFac.trans <| and_iff_right n2).trans <|
    (lt_iff_le_and_ne.trans <| and_iff_right <| minFac_le <| le_of_succ_le n2).symm
Characterization of Non-Prime Numbers via Minimal Factor: $\text{minFac}(n) < n$
Informal description
For any natural number $n \geq 2$, $n$ is not prime if and only if its minimal prime factor is strictly less than $n$ itself.
Nat.minFac_le_div theorem
{n : ℕ} (pos : 0 < n) (np : ¬Prime n) : minFac n ≤ n / minFac n
Full source
theorem minFac_le_div {n : } (pos : 0 < n) (np : ¬Prime n) : minFac n ≤ n / minFac n :=
  match minFac_dvd n with
  | ⟨0, h0⟩ => absurd pos <| by rw [h0, mul_zero]; decide
  | ⟨1, h1⟩ => by
    rw [mul_one] at h1
    rw [prime_def_minFac, not_and_or, ← h1, eq_self_iff_true, _root_.not_true, _root_.or_false,
      not_le] at np
    rw [le_antisymm (le_of_lt_succ np) (succ_le_of_lt pos), minFac_one, Nat.div_one]
  | ⟨x + 2, hx⟩ => by
    conv_rhs =>
      congr
      rw [hx]
    rw [Nat.mul_div_cancel_left _ (minFac_pos _)]
    exact minFac_le_of_dvd (le_add_left 2 x) ⟨minFac n, by rwa [mul_comm]⟩
Minimal Prime Factor Bound: $\text{minFac}(n) \leq n/\text{minFac}(n)$ for Composite $n$
Informal description
For any positive natural number $n$ that is not prime, the minimal prime factor of $n$ is less than or equal to $n$ divided by its minimal prime factor, i.e., $\text{minFac}(n) \leq n / \text{minFac}(n)$.
Nat.minFac_sq_le_self theorem
{n : ℕ} (w : 0 < n) (h : ¬Prime n) : minFac n ^ 2 ≤ n
Full source
/-- The square of the smallest prime factor of a composite number `n` is at most `n`.
-/
theorem minFac_sq_le_self {n : } (w : 0 < n) (h : ¬Prime n) : minFac n ^ 2 ≤ n :=
  have t : minFac n ≤ n / minFac n := minFac_le_div w h
  calc
    minFac n ^ 2 = minFac n * minFac n := sq (minFac n)
    _ ≤ n / minFac n * minFac n := Nat.mul_le_mul_right (minFac n) t
    _ ≤ n := div_mul_le_self n (minFac n)
Square of Minimal Prime Factor Bounds Composite Numbers
Informal description
For any positive natural number $n$ that is not prime, the square of its minimal prime factor is less than or equal to $n$, i.e., $(\text{minFac}(n))^2 \leq n$.
Nat.minFac_eq_one_iff theorem
{n : ℕ} : minFac n = 1 ↔ n = 1
Full source
@[simp]
theorem minFac_eq_one_iff {n : } : minFacminFac n = 1 ↔ n = 1 := by
  constructor
  · intro h
    by_contra hn
    have := minFac_prime hn
    rw [h] at this
    exact not_prime_one this
  · rintro rfl
    rfl
Minimal Prime Factor Equals One if and Only if $n$ Equals One
Informal description
For any natural number $n$, the minimal prime factor of $n$ equals $1$ if and only if $n = 1$.
Nat.minFac_eq_two_iff theorem
(n : ℕ) : minFac n = 2 ↔ 2 ∣ n
Full source
@[simp]
theorem minFac_eq_two_iff (n : ) : minFacminFac n = 2 ↔ 2 ∣ n := by
  constructor
  · intro h
    rw [← h]
    exact minFac_dvd n
  · intro h
    have ub := minFac_le_of_dvd (le_refl 2) h
    have lb := minFac_pos n
    refine ub.eq_or_lt.resolve_right fun h' => ?_
    have := le_antisymm (Nat.succ_le_of_lt lb) (Nat.lt_succ_iff.mp h')
    rw [eq_comm, Nat.minFac_eq_one_iff] at this
    subst this
    exact not_lt_of_le (le_of_dvd lb h) h'
Minimal Prime Factor is Two if and Only if Two Divides $n$
Informal description
For any natural number $n$, the minimal prime factor of $n$ equals $2$ if and only if $2$ divides $n$, i.e., $\text{minFac}(n) = 2 \leftrightarrow 2 \mid n$.
Nat.coprime_of_dvd theorem
{m n : ℕ} (H : ∀ k, Prime k → k ∣ m → ¬k ∣ n) : Coprime m n
Full source
theorem coprime_of_dvd {m n : } (H : ∀ k, Prime k → k ∣ m¬k ∣ n) : Coprime m n := by
  rw [coprime_iff_gcd_eq_one]
  by_contra g2
  obtain ⟨p, hp, hpdvd⟩ := exists_prime_and_dvd g2
  apply H p hp <;> apply dvd_trans hpdvd
  · exact gcd_dvd_left _ _
  · exact gcd_dvd_right _ _
Coprimality from Absence of Common Prime Divisors
Informal description
For any natural numbers $m$ and $n$, if for every prime number $k$ that divides $m$, $k$ does not divide $n$, then $m$ and $n$ are coprime.
Nat.Prime.coprime_iff_not_dvd theorem
{p n : ℕ} (pp : Prime p) : Coprime p n ↔ ¬p ∣ n
Full source
theorem Prime.coprime_iff_not_dvd {p n : } (pp : Prime p) : CoprimeCoprime p n ↔ ¬p ∣ n :=
  ⟨fun co d => pp.not_dvd_one <| co.dvd_of_dvd_mul_left (by simp [d]), fun nd =>
    coprime_of_dvd fun _ m2 mp => ((prime_dvd_prime_iff_eq m2 pp).1 mp).symm ▸ nd⟩
Coprimality Condition for Prime Numbers: $\text{Coprime}(p, n) \leftrightarrow p \nmid n$
Informal description
For any prime natural number $p$ and any natural number $n$, $p$ and $n$ are coprime if and only if $p$ does not divide $n$.
Nat.Prime.dvd_mul theorem
{p m n : ℕ} (pp : Prime p) : p ∣ m * n ↔ p ∣ m ∨ p ∣ n
Full source
theorem Prime.dvd_mul {p m n : } (pp : Prime p) : p ∣ m * np ∣ m * n ↔ p ∣ m ∨ p ∣ n :=
  ⟨fun H => or_iff_not_imp_left.2 fun h => (pp.coprime_iff_not_dvd.2 h).dvd_of_dvd_mul_left H,
    Or.rec (fun h : p ∣ m => h.mul_right _) fun h : p ∣ n => h.mul_left _⟩
Prime Divisibility of Product: $p \mid m \cdot n \leftrightarrow p \mid m \lor p \mid n$
Informal description
For any prime natural number $p$ and any natural numbers $m$ and $n$, $p$ divides the product $m \cdot n$ if and only if $p$ divides $m$ or $p$ divides $n$.
Nat.prime_iff theorem
{p : ℕ} : p.Prime ↔ _root_.Prime p
Full source
theorem prime_iff {p : } : p.Prime ↔ _root_.Prime p :=
  ⟨fun h => ⟨h.ne_zero, h.not_isUnit, fun _ _ => h.dvd_mul.mp⟩, Prime.irreducible⟩
Equivalence of Natural Number Primality Definitions
Informal description
A natural number $p$ is prime in the sense of `Nat.Prime` (i.e., $p \geq 2$ and its only positive divisors are $1$ and $p$ itself) if and only if it is prime in the general sense of `_root_.Prime` (i.e., it is not a unit and whenever it divides a product, it divides one of the factors).
Nat.irreducible_iff_prime theorem
{p : ℕ} : Irreducible p ↔ _root_.Prime p
Full source
theorem irreducible_iff_prime {p : } : IrreducibleIrreducible p ↔ _root_.Prime p :=
  prime_iff
Irreducibility Equivalence for Prime Natural Numbers: $\text{Irreducible}(p) \leftrightarrow \text{Prime}(p)$
Informal description
A natural number $p$ is irreducible (i.e., cannot be written as a product of two non-unit natural numbers) if and only if it is prime in the general sense (i.e., it is not a unit and whenever it divides a product, it divides one of the factors).
Nat.Primes definition
Full source
/-- The type of prime numbers -/
def Primes :=
  { p : ℕ // p.Prime }
  deriving DecidableEq
Prime natural numbers
Informal description
The type of prime numbers, consisting of natural numbers \( p \) that are prime, i.e., \( p \geq 2 \) and whose only positive divisors are \( 1 \) and \( p \) itself.
Nat.Primes.instRepr instance
: Repr Nat.Primes
Full source
instance : Repr Nat.Primes :=
  ⟨fun p _ => repr p.val⟩
Representation of Prime Natural Numbers
Informal description
The type of prime natural numbers has a representation function that allows it to be displayed in a human-readable format.
Nat.Primes.inhabitedPrimes instance
: Inhabited Primes
Full source
instance inhabitedPrimes : Inhabited Primes :=
  ⟨⟨2, prime_two⟩⟩
Existence of Prime Numbers
Informal description
The type of prime natural numbers is inhabited, meaning there exists at least one prime number.
Nat.Primes.coeNat instance
: Coe Nat.Primes ℕ
Full source
instance coeNat : Coe Nat.Primes  :=
  ⟨Subtype.val⟩
Embedding of Prime Numbers into Natural Numbers
Informal description
There is a canonical embedding from the type of prime natural numbers to the natural numbers, which maps each prime number to its underlying natural number value.
Nat.Primes.coe_nat_injective theorem
: Function.Injective ((↑) : Nat.Primes → ℕ)
Full source
theorem coe_nat_injective : Function.Injective ((↑) : Nat.Primes) :=
  Subtype.coe_injective
Injectivity of Prime Number Embedding into Natural Numbers
Informal description
The canonical embedding from the type of prime natural numbers to natural numbers is injective. In other words, if two prime numbers are equal as natural numbers, then they are equal as elements of the subtype `Nat.Primes`.
Nat.Primes.coe_nat_inj theorem
(p q : Nat.Primes) : (p : ℕ) = (q : ℕ) ↔ p = q
Full source
theorem coe_nat_inj (p q : Nat.Primes) : (p : ℕ) = (q : ℕ) ↔ p = q :=
  Subtype.ext_iff.symm
Injective Correspondence Between Prime Numbers and Their Natural Number Values
Informal description
For any two prime natural numbers $p$ and $q$, the underlying natural number values of $p$ and $q$ are equal if and only if $p$ and $q$ themselves are equal.
Nat.monoid.primePow instance
{α : Type*} [Monoid α] : Pow α Primes
Full source
instance monoid.primePow {α : Type*} [Monoid α] : Pow α Primes :=
  ⟨fun x p => x ^ (p : ℕ)⟩
Power Operation with Prime Exponents in Monoids
Informal description
For any monoid $\alpha$, there is a power operation $\alpha^{\text{Nat.Primes}}$ that allows raising elements of $\alpha$ to prime natural number exponents.