import Mathlib namespace OAI section open scoped BigOperators /-! Occurrence-preserving bounds for the canonical p−1 auxiliary tree in Section5. Equal labels under different parents remain separate list entries. -/ namespace ExactQuantumFactoring.AuxiliaryTree open scoped BigOperators def children (M : ℕ) : List ℕ := ((M.primeFactors.filter (fun p => 2
2
2
Nat.sub_le _ _)
_ ≤ ∏ p ∈ M.primeFactors, p := Finset.prod_le_prod_of_subset_of_one_le
(Finset.filter_subset _ _)
(fun p hp _ => (Nat.prime_of_mem_primeFactors hp).one_lt.le)
_ ≤ M := Nat.le_of_dvd hM (Nat.prod_primeFactors_dvd M)
/-- A level is a list, not a set: multiplicities of occurrences are retained. -/
def level (M : ℕ) : ℕ → List ℕ
| 0 => [M]
| k+1 => (children M).flatMap (fun d => level d k)
lemma level_bounds {M d k : ℕ} (hM : 2≤M) (hd : d ∈ level M k) : 2≤d ∧ d≤M := by
induction k generalizing M with
| zero =>
have he : d=M := by simpa [level] using hd
subst d
exact ⟨hM,le_rfl⟩
| succ k ih =>
obtain ⟨e,he,hd⟩ := List.mem_flatMap.mp hd
have hb := child_bounds he
have hh := ih hb.1 hd
exact ⟨hh.1,hh.2.trans hb.2.le⟩
lemma level_two_step {M d k : ℕ} (hd : d ∈ level M (2*k)) : 2^k*d≤M := by
induction k generalizing M with
| zero =>
have he : d=M := by simpa [level] using hd
simpa using he.le
| succ k ih =>
have heq : 2*(k+1)=2*k+1+1 := by omega
rw [heq,level] at hd
obtain ⟨e,he,hd⟩ := List.mem_flatMap.mp hd
rw [level] at hd
obtain ⟨f,hf,hd⟩ := List.mem_flatMap.mp hd
have hx := ih hd
have hh := grandchild_half he hf
rw [pow_succ]
nlinarith
lemma level_empty {M n : ℕ} (hM : 2≤M) (hbound : M<2^n) : level M (2*n)=[] := by
apply List.eq_nil_iff_forall_not_mem.mpr
intro d hd
have hx := level_two_step hd
have hy := (level_bounds hM hd).1
have hp : 0<2^n := by positivity
nlinarith
lemma prod_flatMap (f : ℕ→List ℕ) (xs : List ℕ) :
(xs.flatMap f).prod=(xs.map (fun x => (f x).prod)).prod := by
induction xs with
| nil => rfl
| cons x xs ih => simp [ih]
lemma level_prod_le {M k : ℕ} (hM : 2≤M) : (level M k).prod≤M := by
induction k generalizing M with
| zero => simp [level]
| succ k ih =>
rw [level,prod_flatMap]
calc
((children M).map (fun d => (level d k).prod)).prod ≤
((children M).map id).prod := List.prod_map_le_prod_map₀ _ _
(fun _ _ => Nat.zero_le _) (fun d hd => ih (child_bounds hd).1)
_ = (children M).prod := by simp
_ ≤ M := children_prod_le (by omega)
lemma pow_length_le_prod (xs : List ℕ) (hxs : ∀ x ∈ xs, 2≤x) : 2^xs.length≤xs.prod := by
induction xs with
| nil => simp
| cons x xs ih =>
have hx := hxs x (by simp)
have hi := ih (fun y hy => hxs y (by simp [hy]))
simp only [List.length_cons,List.prod_cons,pow_succ]
exact (Nat.mul_le_mul hi hx).trans_eq (Nat.mul_comm _ _)
lemma level_length_lt {M n k : ℕ} (hM : 2≤M) (hbound : M<2^n) :
(level M k).length