Stage 08 · Step 39 of 50

Primes never run out.

Needs Mathlib12 minutesLean 4.36.0

01The idea

A road with a sign beyond every distance cannot end at a last sign. Primes have a similar property.

Lemmi · Your guide for this idea
Lemmi pointing

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

Beyond every bound there is a prime: look at bound! + 1The goal is ∃ p, bound ≤ p ∧ Nat.Prime p. The picture uses bound = 4. Number tiles 1 to 12 are shown, with a bar after 4 marking the bound. The tiles 2, 3 and 4 are shaded: each of them divides 4! = 1 × 2 × 3 × 4 = 24. So each leaves remainder 1 when it divides 4! + 1 = 25: 25 = 2 × 12 + 1, 25 = 3 × 8 + 1, 25 = 4 × 6 + 1. An arc goes from the tile 25 to the tile 5: the smallest prime factor of 25 is 5, which is beyond the bound. In general, every number from 2 up to the bound N divides N!, so none of them divides N! + 1, and the smallest prime factor of N! + 1 must be bigger than N. Mathlib's theorem Nat.exists_infinite_primes follows this route, and exact Nat.exists_infinite_primes bound closes the goal.⊢ ∃ p, bound ≤ p ∧ Nat.Prime psmallest prime factor of 25 is 5123456789101112⋯254! + 1bound = 42, 3 and 4 all divide 4! = 1 × 2 × 3 × 4 = 24, so each leaves remainder 1 on 25:25 = 2 × 12 + 125 = 3 × 8 + 125 = 4 × 6 + 1so none of themcan divide 25Any bound N: every number from 2 to N divides N!, so it leaves remainder 1 on N! + 1.So the smallest prime factor of N! + 1 cannot be N or less.It is a prime beyond the bound.exact Nat.exists_infinite_primes boundMathlib's proof takes this route

Swipe sideways to see the whole picture.

To beat any bound, Mathlib’s proof looks at the factorial of the bound, plus one. Every number from 2 up to the bound divides the factorial, so it leaves remainder 1 on factorial + 1 and cannot divide it. So the smallest prime factor of factorial + 1 lies beyond the bound: for bound 4 that is 5, the smallest prime factor of 25. Your proof just hands bound to this theorem with exact.

03In Lean

This code comes directly from the file checked by Lean.

E39.leanTry it live (opens official Lean editor)
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.

  1. Form the factorial of the bound, then add one. Choose the least prime factor of that result.
  2. That number is greater than one, so its smallest prime factor is prime.
  3. If the prime factor were at most the bound, it would divide the factorial.
  4. 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.

After line 7
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

Captured from Lean 4.36.0
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.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Prove that seven is prime.

  2. A little stretch

    Find a prime at or beyond bound plus one.

Exercises/E39.leanTry it live (opens official Lean editor)
module

import Mathlib.Data.Nat.Prime.Infinite

example : Nat.Prime 7 := by
  sorry

example (bound : ℕ) : ∃ p, bound + 1 ≤ p ∧ Nat.Prime p := by
  sorry

08A quick check

1. Does the theorem only find primes below a fixed maximum?

Read the answer and explanation

No; it works at every bound. For any proposed last prime, choose a larger bound.

2. Is one a prime number?

Read the answer and explanation

No. A prime natural number is at least two and has no positive divisors other than one and itself.

One more idea in your toolkit.

Take your time. Progress is yours to mark.