data.nat.prime

# Prime numbers #

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

## Important declarations #

All the following declarations exist in the namespace nat.

• prime: the predicate that expresses that a natural number p is prime
• primes: the subtype of natural numbers that are prime
• min_fac n: the minimal prime factor of a natural number n ≠ 1
• exists_infinite_primes: Euclid's theorem that there exist infinitely many prime numbers
• factors n: the prime factorization of n
• factors_unique: uniqueness of the prime factorisation
def nat.prime (p : ) :
Prop

prime p means that p is a prime number, that is, a natural number at least 2 whose only divisors are p and 1.

Equations
theorem nat.prime.two_le {p : } :
2 p
theorem nat.prime.one_lt {p : } :
1 < p
@[instance]
def nat.prime.one_lt' (p : ) [hp : fact (nat.prime p)] :
fact (1 < p)
theorem nat.prime.ne_one {p : } (hp : nat.prime p) :
p 1
theorem nat.prime_def_lt {p : } :
2 p ∀ (m : ), m < pm pm = 1
theorem nat.prime_def_lt' {p : } :
2 p ∀ (m : ), 2 mm < p¬m p
theorem nat.prime_def_le_sqrt {p : } :
2 p ∀ (m : ), 2 mm ¬m p
def nat.decidable_prime_1 (p : ) :

This instance is slower than the instance decidable_prime defined below, but has the advantage that it works in the kernel for small values.

If you need to prove that a particular number is prime, in any case you should not use dec_trivial, but rather by norm_num, which is much faster.

Equations
theorem nat.prime.ne_zero {n : } (h : nat.prime n) :
n 0
theorem nat.prime.pos {p : } (pp : nat.prime p) :
0 < p
theorem nat.prime_two  :
theorem nat.prime.pred_pos {p : } (pp : nat.prime p) :
0 < p.pred
theorem nat.succ_pred_prime {p : } (pp : nat.prime p) :
p.pred.succ = p
theorem nat.dvd_prime {p m : } (pp : nat.prime p) :
m p m = 1 m = p
theorem nat.dvd_prime_two_le {p m : } (pp : nat.prime p) (H : 2 m) :
m p m = p
theorem nat.prime_dvd_prime_iff_eq {p q : } (pp : nat.prime p) (qp : nat.prime q) :
p q p = q
theorem nat.prime.not_dvd_one {p : } (pp : nat.prime p) :
¬p 1
theorem nat.not_prime_mul {a b : } (a1 : 1 < a) (b1 : 1 < b) :
theorem nat.not_prime_mul' {a b n : } (h : a * b = n) (h₁ : 1 < a) (h₂ : 1 < b) :
def nat.min_fac_aux (n : ) :

If n < k * k, then min_fac_aux n k = n, if k | n, then min_fac_aux n k = k. Otherwise, min_fac_aux n k = min_fac_aux n (k+2) using well-founded recursion. If n is odd and 1 < n, then then min_fac_aux n 3 is the smallest prime factor of n.

Equations
def nat.min_fac  :

Returns the smallest prime factor of n ≠ 1.

Equations
@[simp]
theorem nat.min_fac_zero  :
@[simp]
theorem nat.min_fac_one  :
theorem nat.min_fac_eq (n : ) :
n.min_fac = ite (2 n) 2 (n.min_fac_aux 3)
theorem nat.min_fac_aux_has_prop {n : } (n2 : 2 n) (nd2 : ¬2 n) (k i : ) :
k = 2 * i + 3(∀ (m : ), 2 mm nk m)min_fac_prop n (n.min_fac_aux k)
theorem nat.min_fac_has_prop {n : } (n1 : n 1) :
min_fac_prop n n.min_fac
theorem nat.min_fac_dvd (n : ) :
theorem nat.min_fac_prime {n : } (n1 : n 1) :
theorem nat.min_fac_le_of_dvd {n m : } :
2 mm nn.min_fac m
theorem nat.min_fac_pos (n : ) :
theorem nat.min_fac_le {n : } (H : 0 < n) :
theorem nat.le_min_fac {m n : } :
n = 1 m n.min_fac ∀ (p : ), p nm p
theorem nat.le_min_fac' {m n : } :
n = 1 m n.min_fac ∀ (p : ), 2 pp nm p
theorem nat.prime_def_min_fac {p : } :
2 p p.min_fac = p
@[instance]
def nat.decidable_prime (p : ) :

This instance is faster in the virtual machine than decidable_prime_1, but slower in the kernel.

If you need to prove that a particular number is prime, in any case you should not use dec_trivial, but rather by norm_num, which is much faster.

Equations
theorem nat.not_prime_iff_min_fac_lt {n : } (n2 : 2 n) :
theorem nat.min_fac_le_div {n : } (pos : 0 < n) (np : ¬) :
theorem nat.min_fac_sq_le_self {n : } (w : 0 < n) (h : ¬) :
n.min_fac ^ 2 n

The square of the smallest prime factor of a composite number n is at most n.

@[simp]
theorem nat.min_fac_eq_one_iff {n : } :
n.min_fac = 1 n = 1
@[simp]
theorem nat.min_fac_eq_two_iff (n : ) :
n.min_fac = 2 2 n
theorem nat.exists_dvd_of_not_prime {n : } (n2 : 2 n) (np : ¬) :
∃ (m : ), m n m 1 m n
theorem nat.exists_dvd_of_not_prime2 {n : } (n2 : 2 n) (np : ¬) :
∃ (m : ), m n 2 m m < n
theorem nat.exists_prime_and_dvd {n : } (n2 : 2 n) :
∃ (p : ), p n
theorem nat.exists_infinite_primes (n : ) :
∃ (p : ), n p

Euclid's theorem on the infinitude of primes. Here given in the form: for every n, there exists a prime number p ≥ n.

theorem nat.prime.eq_two_or_odd {p : } (hp : nat.prime p) :
p = 2 p % 2 = 1
theorem nat.coprime_of_dvd {m n : } (H : ∀ (k : ), k m¬k n) :
theorem nat.coprime_of_dvd' {m n : } (H : ∀ (k : ), k mk nk 1) :
theorem nat.factors_lemma {k : } :
(k + 2) / (k + 2).min_fac < k + 2
def nat.factors  :

factors n is the prime factorization of n, listed in increasing order.

Equations
@[simp]
theorem nat.factors_zero  :
@[simp]
theorem nat.factors_one  :
theorem nat.prime_of_mem_factors {n p : } :
p n.factors
theorem nat.prod_factors {n : } :
0 < nn.factors.prod = n
theorem nat.factors_prime {p : } (hp : nat.prime p) :
p.factors = [p]
theorem nat.factors_chain {n a : } :
(∀ (p : ), p na p)
theorem nat.factors_chain_2 (n : ) :
theorem nat.factors_chain' (n : ) :
theorem nat.factors_sorted (n : ) :
theorem nat.factors_add_two (n : ) :
(n + 2).factors = (n + 2).min_fac :: ((n + 2) / (n + 2).min_fac).factors

factors can be constructed inductively by extracting min_fac, for sufficiently large n.

@[simp]
theorem nat.factors_eq_nil (n : ) :
n = 0 n = 1
theorem nat.prime.coprime_iff_not_dvd {p n : } (pp : nat.prime p) :
p.coprime n ¬p n
theorem nat.prime.dvd_iff_not_coprime {p n : } (pp : nat.prime p) :
p n ¬p.coprime n
theorem nat.prime.not_coprime_iff_dvd {m n : } :
¬m.coprime n ∃ (p : ), p m p n
theorem nat.prime.dvd_mul {p m n : } (pp : nat.prime p) :
p m * n p m p n
theorem nat.prime.not_dvd_mul {p m n : } (pp : nat.prime p) (Hm : ¬p m) (Hn : ¬p n) :
¬p m * n
theorem nat.prime.dvd_of_dvd_pow {p m n : } (pp : nat.prime p) (h : p m ^ n) :
p m
theorem nat.prime.pow_not_prime {x n : } (hn : 2 n) :
theorem nat.prime.mul_eq_prime_sq_iff {x y p : } (hp : nat.prime p) (hx : x 1) (hy : y 1) :
x * y = p ^ 2 x = p y = p
theorem nat.prime.dvd_factorial {n p : } (hp : nat.prime p) :
p n! p n
theorem nat.prime.coprime_pow_of_not_dvd {p m a : } (pp : nat.prime p) (h : ¬p a) :
a.coprime (p ^ m)
theorem nat.coprime_primes {p q : } (pp : nat.prime p) (pq : nat.prime q) :
p.coprime q p q
theorem nat.coprime_pow_primes {p q : } (n m : ) (pp : nat.prime p) (pq : nat.prime q) (h : p q) :
(p ^ n).coprime (q ^ m)
theorem nat.coprime_or_dvd_of_prime {p : } (pp : nat.prime p) (i : ) :
p.coprime i p i
theorem nat.dvd_prime_pow {p : } (pp : nat.prime p) {m i : } :
i p ^ m ∃ (k : ) (H : k m), i = p ^ k
theorem nat.eq_prime_pow_of_dvd_least_prime_pow {a p k : } (pp : nat.prime p) (h₁ : ¬a p ^ k) (h₂ : a p ^ (k + 1)) :
a = p ^ (k + 1)

If p is prime, and a doesn't divide p^k, but a does divide p^(k+1) then a = p^(k+1).

theorem nat.mem_list_primes_of_dvd_prod {p : } (hp : nat.prime p) {l : list } :
(∀ (p : ), p lp l.prodp l
theorem nat.mem_factors_iff_dvd {n p : } (hn : 0 < n) (hp : nat.prime p) :
p n.factors p n
theorem nat.mem_factors {n p : } (hn : 0 < n) :
theorem nat.factors_subset_right {n k : } (h : k 0) :
theorem nat.factors_subset_of_dvd {n k : } (h : n k) (h' : k 0) :
theorem nat.perm_of_prod_eq_prod {l₁ l₂ : list } :
l₁.prod = l₂.prod(∀ (p : ), p l₁(∀ (p : ), p l₂l₁ ~ l₂
theorem nat.factors_unique {n : } {l : list } (h₁ : l.prod = n) (h₂ : ∀ (p : ), p l) :

Fundamental theorem of arithmetic

theorem nat.succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : } (p_prime : nat.prime p) {m n k l : } (hpm : p ^ k m) (hpn : p ^ l n) (hpmn : p ^ (k + l + 1) m * n) :
p ^ (k + 1) m p ^ (l + 1) n
def nat.primes  :
Type

The type of prime numbers

Equations
@[instance]
Equations
@[instance]
Equations
@[instance]
Equations
theorem nat.primes.coe_nat_inj (p q : nat.primes) :
p = qp = q
@[instance]
def nat.monoid.prime_pow {α : Type u_1} [monoid α] :
Equations

### Primality prover #

theorem tactic.norm_num.is_prime_helper (n : ) (h₁ : 1 < n) (h₂ : n.min_fac = n) :
def tactic.norm_num.min_fac_helper (n k : ) :
Prop

A predicate representing partial progress in a proof of min_fac.

Equations
theorem tactic.norm_num.min_fac_helper.n_pos {n k : } (h : k) :
0 < n
theorem tactic.norm_num.min_fac_helper_0 (n : ) (h : 0 < n) :
theorem tactic.norm_num.min_fac_helper_1 {n k k' : } (e : k + 1 = k') (np : (bit1 n).min_fac bit1 k) (h : k) :
theorem tactic.norm_num.min_fac_helper_2 (n k k' : ) (e : k + 1 = k') (np : ¬nat.prime (bit1 k)) (h : k) :
theorem tactic.norm_num.min_fac_helper_3 (n k k' c : ) (e : k + 1 = k') (nc : bit1 n % bit1 k = c) (c0 : 0 < c) (h : k) :
theorem tactic.norm_num.min_fac_helper_4 (n k : ) (hd : bit1 n % bit1 k = 0) (h : k) :
theorem tactic.norm_num.min_fac_helper_5 (n k k' : ) (e : (bit1 k) * bit1 k = k') (hd : bit1 n < k') (h : k) :
meta def tactic.norm_num.prove_non_prime (e : expr) (n d₁ : ) :

Given e a natural numeral and d : nat a factor of it, return ⊢ ¬ prime e.

meta def tactic.norm_num.prove_min_fac_aux (a a1 : expr) (n1 : ) :

Given a,a1 := bit1 a, n1 the value of a1, b and p : min_fac_helper a b, returns (c, ⊢ min_fac a1 = c).

Given a a natural numeral, returns (b, ⊢ min_fac a = b).

def tactic.norm_num.factors_helper (n p : ) (l : list ) :
Prop

A partial proof of factors. Asserts that l is a sorted list of primes, lower bounded by a prime p, which multiplies to n.

Equations
theorem tactic.norm_num.factors_helper_cons' (n m a b : ) (l : list ) (h₁ : b * m = n) (h₂ : a b) (h₃ : b.min_fac = b) (H : l) :
(b :: l)
theorem tactic.norm_num.factors_helper_cons (n m a b : ) (l : list ) (h₁ : b * m = n) (h₂ : a < b) (h₃ : b.min_fac = b) (H : l) :
(b :: l)
theorem tactic.norm_num.factors_helper_sn (n a : ) (h₁ : a < n) (h₂ : n.min_fac = n) :
theorem tactic.norm_num.factors_helper_same (n m a : ) (l : list ) (h : a * m = n) (H : l) :
(a :: l)
theorem tactic.norm_num.factors_helper_end (n : ) (l : list ) (H : l) :

Given n and a natural numerals, returns (l, ⊢ factors_helper n a l).

Evaluates the prime and min_fac functions.

theorem nat.prime_three  :