/-- Every whole number `n ≥ 1` is a product of primes (`1` is the empty product). This is the
factor tree, made precise: split any number that is not prime into two smaller factors, and
keep going. The numbers get smaller, so the splitting must stop, and then every leaf is prime.
Uniqueness of the primes (up to order) is *not* proved here. -/
theorem factorisation_exists (n : Nat) (h1 : 1 ≤ n) :
∃ ps : List Nat, (∀ p ∈ ps, IsPrime p) ∧ listProd ps = n := by
induction n using Nat.strongRecOn with
| _ n ih =>
by_cases hn1 : n = 1
· exact ⟨[], by simp, by simp [listProd, hn1]⟩
· have h2 : 2 ≤ n := by omega
by_cases hp : IsPrime n
· exact ⟨[n], by simpa using hp, by simp [listProd]⟩
· obtain ⟨a, b, ha2, han, hb2, hbn, hab⟩ := not_prime_split h2 hp
obtain ⟨xs, hxs, hxa⟩ := ih a han (by omega)
obtain ⟨ys, hys, hyb⟩ := ih b hbn (by omega)
refine ⟨xs ++ ys, ?_, ?_⟩
· intro p hp'
rcases List.mem_append.mp hp' with hx | hy
· exact hxs p hx
· exact hys p hy
· rw [listProd_append, hxa, hyb, hab]