Stage 08 · Step 39 of 50
Primes never run out.
01The idea
A road with a sign beyond every distance cannot end at a last sign. Primes have a similar property.
Mathlib provides a theorem finding a prime at or beyond every bound.
02In maths
For every natural-number bound, there exists a prime p at least as large as that bound.
Picture it
Swipe sideways to see the whole picture.
bound to this theorem with exact.03In Lean
This code comes directly from the file checked by Lean.
module
import Mathlib.Data.Nat.Prime.Infinite
#check Nat.exists_infinite_primes
example (bound : ℕ) : ∃ p, bound ≤ p ∧ Nat.Prime p := by
exact Nat.exists_infinite_primes bound
A prime natural number is at least two and has no positive divisors other than one and itself.
The displayed library theorem states an unboundedness property. For every bound, it gives a prime at or beyond that bound.
This lesson reads and applies Mathlib’s checked theorem. It does not pretend that a one-line application reproduces Euclid’s whole argument.
You can also read the pinned library proof. Here is its route:
A prime factor is a prime number that divides the number being studied. The least prime factor is the smallest such prime.
- Form the factorial of the bound, then add one. Choose the least prime factor of that result.
- That number is greater than one, so its smallest prime factor is prime.
- If the prime factor were at most the bound, it would divide the factorial.
- It also divides the factorial plus one. It would therefore divide one, which a prime cannot do.
This rules out every prime factor at or below the bound. The resulting prime is beyond it, so it also meets the theorem’s weaker at-least bound.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
bound : ℕ ⊢ ∃ p, bound ≤ p ∧ Nat.Prime p
After line 7: example (bound : ℕ) : ∃ p, bound ≤ p ∧ Nat.Prime p := by
bound : ℕ ⊢ ∃ p, bound ≤ p ∧ Nat.Prime p
After line 8: exact Nat.exists_infinite_primes bound
no goals
05Lean says
Nat.exists_infinite_primes (n : ℕ) : ∃ p, n ≤ p ∧ Nat.Prime p no goals
A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
module
import Mathlib.Data.Nat.Prime.Infinite
example : Nat.Prime 1 := by
decide
Tactic `decide` proved that the proposition Nat.Prime 1 is false
One is not a prime number.
The fix: Use the definition requiring a prime to be at least two.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Prove that seven is prime.
Show a hint
This is a concrete decidable statement.
One more hint
decide checks the finite divisibility conditions.
- A little stretch
Find a prime at or beyond bound plus one.
Show a hint
Use the library theorem at the larger bound.
One more hint
Pass bound plus one as its argument.
module
import Mathlib.Data.Nat.Prime.Infinite
example : Nat.Prime 7 := by
sorry
example (bound : ℕ) : ∃ p, bound + 1 ≤ p ∧ Nat.Prime p := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
module
import Mathlib.Data.Nat.Prime.Infinite
example : Nat.Prime 7 := by
decide
example (bound : ℕ) : ∃ p, bound + 1 ≤ p ∧ Nat.Prime p := by
exact Nat.exists_infinite_primes (bound + 1)
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.