import OAI.Computability.FourierTransform.PrimeSelection namespace OAI /- Selection of length. Explicit loops. -/ namespace PowerSaving.Selection open Grove noncomputable section structure Cart where l : ℕ q : Axes vol : ℕ namespace Cart def empty : Cart := ⟨0,fun _ => 0,1⟩ def pushQ (x : Cart) (p : ℕ) : Cart := ⟨x.l+1,fun i => if i=x.l then p else x.q i, x.vol*p⟩ structure Square (x : Cart) : Prop where agrees : x.vol=extent x.q x.l isles : Isles x.q x.l lemma Square.empty : empty.Square := by exact ⟨rfl,fun i hi => by change i<0 at hi; omega, fun i hi => by change i<0 at hi; omega⟩ variable {x : Cart} lemma push_eq (p : ℕ) {i : ℕ} (h : i rfl | succ i ih => rw [extent,ih (by omega),extent,push_eq p (by omega)] lemma at_push (x : Cart) (p : ℕ) : (x.pushQ p).q x.l=p := ite_eq_left rfl lemma Square.pushQ (h : x.Square) {p : ℕ} (hp : p≥2) (hq : p.Coprime x.vol) : (x.pushQ p).Square := by let y := x.pushQ p refine ⟨?_,?_,?_⟩ · change x.vol*p=extent y.q x.l*y.q x.l rw [push_ext _ le_rfl,at_push,h.agrees] · intro i hi change i Cart.empty | p+1 => let x := reap N p if Bar p ∧ x.vol*p ≤ N then x.pushQ p else x structure Baler (N p : ℕ) (x : Cart) : Prop where square : x.Square odd : Odd x.vol top : x.vol ≤ N count : x.l≤p vars : ∀ i by change i<0 at hi; omega, fun p _ _ => Nat.coprime_one_right _,Or.inl bars_zero.symm⟩ lemma push (h : Baler N p x) (g : Bar p) (hp : x.vol*p ≤ N) : Baler N (p+1) (x.pushQ p) := by have ht : p>2 := g.2 refine ⟨h.square.pushQ ht.le (h.more p le_rfl g), h.odd.mul (g.1.odd_of_ne_two (by omega)),hp, Nat.add_le_add_right h.count 1,?_,?_,?_⟩ · intro i hi change i h.more q (by omega) hg,?_⟩ · intro i hi; have H := h.vars i hi exact ⟨H.1,H.2.trans (by omega)⟩ · by_cases H : Bar p · exact Or.inr ⟨p,by omega,Nat.lt_of_not_ge fun hx => g ⟨H,hx⟩⟩ · rw [bars_step,ite_eq_right H,Nat.add_zero] exact h.hit.imp id fun ⟨q,hq,hk⟩ => ⟨q,by omega,hk⟩ end Baler lemma reap_baler (N p : ℕ) (h : 0 exact Baler.empty _ h | succ p ih => simp only [reap]; split · rename_i hk; exact ih.push hk.1 hk.2 · rename_i hg; exact ih.pass hg def harvestN (n : ℕ) := silo (stackN n) def bed (n : ℕ) := reap (2*n) (harvestN n+1) -- choose dyad by doubling until ≥N def fill (N p : ℕ) : ℕ→ℕ | 0 => 1 | k+1 => let q := fill N p k if p*q rfl | succ i ih => rw [fill,pow_succ]; split <;> omega lemma fills (N p i : ℕ) : N ≤ p*fill N p i ∨ fill N p i=2^i := by induction i with | zero => exact Or.inr rfl | succ i ih => rw [fill,pow_succ]; split · rename_i H; right; rw [ih.resolve_left (not_le.mpr H)] · rename_i H; exact Or.inl (Nat.le_of_not_lt H) lemma radius (N p i : ℕ) (hp : p simp only [fill,mul_one]; omega | succ i ih => rw [fill]; split · rename_i hk; rw [← mul_assoc]; omega · exact ih lemma dyad (N p i : ℕ) : ∃ j,fill N p i=2^j := by induction i with | zero => exact ⟨0,rfl⟩ | succ i ih => rw [fill]; split · obtain ⟨j,hj⟩ := ih; exact ⟨j+1,by rw [pow_succ,hj]⟩ · exact ih end Fill def bead (n : ℕ) := fill (2*n) (bed n).vol (stackN n) def barge (n : ℕ) := (bed n).pushQ (bead n) def orderE (n : ℕ) := Nat.log 2 (2*harvestN n)+2 lemma orderE_big (n : ℕ) : 2*harvestN n ≤ 2^orderE n := (Nat.lt_pow_succ_log_self (by omega : 1<2) _).le.trans (Nat.pow_le_pow_right (by omega) (Nat.le_succ _)) lemma orderE_small (n : ℕ) : 2^orderE n ≤ 8*harvestN n := by have h : 0 not_lt.mpr (Nat.pow_le_pow_right (by omega : 0<2) H) (hh.trans_lt (hp.trans hK)) have hV : 1≤X.vol := HT.square.agrees.symm ▸ HT.square.isles.good.extent_pos have hf : R ≤ X.vol*f := by rcases Fill.fills R X.vol K with h|h · exact h · change f=2^K at h rw [h]; exact hK.le.trans (Nat.le_mul_of_pos_left _ hV) have hg : X.vol*f < 4*n := by have : X.vol*f < 2*(2*n) := Fill.radius R X.vol K hp omega have ht : f≥2 := Nat.le_of_not_lt fun H => not_lt.mpr hf (lt_of_le_of_lt (by simpa only [mul_one] using Nat.mul_le_mul_left X.vol (show f≤1 by omega)) hp) have hu : f ≤ 2*S := by have hf' : X.vol*f < 2*R := Fill.radius R X.vol K hp rcases HT.hit with h|⟨q,hq,H⟩ · have hy : K < bars (S+1) := bar_bounds _ omega · have hy : q ≤ S := by omega have hb : R