import OAI.NumberTheory.GaussianMoat.AccurateScales namespace OAI universe uIota namespace GaussianMoat section open scoped BigOperators Classical open Filter noncomputable def topParameter : ℝ := 1/1000000 def topIterations : ℕ := 20 noncomputable def topCoefficient (a : ℝ) : ℝ := (topParameter^2)^topIterations*a lemma topParameter_pos : 0 bandLength topParameter (a*Real.exp x) i.val)++post) 0 (bandSize s topParameter (a*Real.exp x) topIterations) (1/10000)) ∧ topCoefficient a*Real.exp x/(4*x)≤ (bandSize s topParameter (a*Real.exp x) topIterations:ℝ) := by have hc := topCoefficient_pos ha have hlarge := eventually_poly_le_exp (2*bandConstant D) (a := topParameter^5*topCoefficient a) (mul_pos (pow_pos topParameter_pos _) hc) 1 filter_upwards [hlarge,eventually_ge_atTop (2:ℝ)] with x hlarge hx intro s pre post hne hp hdense hpost hlen have hT2 : 2≤Real.exp x := by linarith only [Real.add_one_le_exp x,hx] have hm := s.meanLog_bounds hne (Real.exp_pos x) hp rw [Real.log_exp] at hm have hmean : 1≤ s.meanLog := by linarith only [hm.1,hx] have hmean0 : 0< s.meanLog := by linarith only [hmean] have hlog2 : Real.log 2≤x := by have hh := Real.log_le_sub_one_of_pos (by norm_num : (0:ℝ)<2) linarith only [hh,hx] have hmean' : s.meanLog≤2*x := by rw [Real.log_mul (by norm_num) (Real.exp_ne_zero _),Real.log_exp] at hm linarith only [hm.2,hlog2] have hscale : bandScale topParameter (a*Real.exp x) topIterations=topCoefficient a*Real.exp x := by unfold bandScale topCoefficient; ring have hmargin : bandConstant D*s.meanLog≤topParameter^5* bandScale topParameter (a*Real.exp x) topIterations := by have hh := mul_le_mul_of_nonneg_left hmean' (bandConstant_pos D).le rw [hscale] simp only [pow_one] at hlarge nlinarith only [hh,hlarge] have hcap := dense_sieve_cap s hne hT2 hp hdense (by nlinarith only [haδ,Real.exp_pos x] : 2*(a*Real.exp x)≤δ*Real.exp x) have hband := entropyBand_from_margin s pre post 0 topIterations hD topParameter_pos topParameter_small (mul_pos ha (Real.exp_pos _)) hmean hmargin hcap (by simpa only [hscale,mul_assoc] using hpost) (by simp only [Nat.cast_zero]; positivity) (by simpa only [hscale,mul_assoc] using hlen) top_deficit refine ⟨hband,?_⟩ have hg5 : topParameter^5≤1 := pow_le_one₀ topParameter_pos.le (by norm_num [topParameter]) have hVpos := bandScale_pos topParameter_pos (mul_pos ha (Real.exp_pos x)) topIterations have hn := mul_le_mul_of_nonneg_right hg5 hVpos.le have hC : 2≤bandConstant D := by unfold bandConstant linarith only [abs_nonneg (Real.log D),abs_nonneg (Real.log (max D ((latticeBall (4*D)).card:ℝ)))] have hm2 := mul_le_mul_of_nonneg_right hC hmean0.le have hV : 2*s.meanLog≤bandScale topParameter (a*Real.exp x) topIterations := by nlinarith only [hmargin,hn,hm2] have hh := bandSize_lower_of_scale hmean0 (show 0 bandLength topParameter (a*(2:ℝ)^j) i.val) noncomputable def topBlocks (a : ℝ) (J : Finset ℕ) : List ℕ := (J.sort (·≥·)).flatMap (topBlock a) noncomputable def accurateBlock (m : ℕ) (a : ℝ) : List ℕ := List.ofFn (fun i : Fin (accurateIterations m) => bandLength (accurateGrid m) (Real.exp (a*Real.exp m)) i.val) lemma topBlocks_length (a : ℝ) (J : Finset ℕ) : (topBlocks a J).length=J.card*topIterations := by unfold topBlocks rw [List.length_flatMap] simp only [topBlock,List.length_ofFn,List.map_const',List.sum_replicate,Finset.length_sort, smul_eq_mul] lemma topBlock_bound {a H : ℝ} (ha : 0≤a) (j : ℕ) (h : topParameter*a*(2:ℝ)^j≤H) : ∀ n∈topBlock a j, (n:ℝ)≤Real.exp H := by intro n hn obtain ⟨i,rfl⟩ := List.mem_ofFn.mp hn exact (bandLength_le_exp_start topParameter_pos.le (by norm_num [topParameter]) (mul_nonneg ha (by positivity)) i.val).trans (Real.exp_le_exp.mpr (by simpa only [mul_assoc] using h)) lemma accurateBlock_bound {a H : ℝ} {m : ℕ} (hg : accurateGrid m≤1) (h : accurateGrid m*Real.exp (a*Real.exp m)≤H) : ∀ n∈accurateBlock m a, (n:ℝ)≤Real.exp H := by intro n hn obtain ⟨i,rfl⟩ := List.mem_ofFn.mp hn exact (bandLength_le_exp_start (accurateGrid_pos m).le hg (Real.exp_nonneg _) i.val).trans (Real.exp_le_exp.mpr h) lemma sorted_split {J : Finset ℕ} {j : ℕ} (hj : j∈J) : ∃ pre post : List ℕ, J.sort (·≥·)=pre++j::post ∧ (∀ i∈post, i∈J ∧ i < j) ∧ (∀ i∈pre, i∈J ∧ j < i) := by have hm : j∈J.sort (·≥·) := (Finset.mem_sort (·≥·)).mpr hj obtain ⟨pre,post,he⟩ := List.mem_iff_append.mp hm have hp : (J.sort (·≥·)).Pairwise (·>·) := by exact (Finset.sortedGT_sort J).pairwise rw [he,List.pairwise_append,List.pairwise_cons] at hp refine ⟨pre,post,he,?_,?_⟩ · intro i hi refine ⟨(Finset.mem_sort (·≥·)).mp ?_,hp.2.1.1 i hi⟩ rw [he]; exact List.mem_append_right _ (List.mem_cons_of_mem _ hi) · intro i hi refine ⟨(Finset.mem_sort (·≥·)).mp ?_,hp.2.2 i hi j (List.mem_cons_self ..)⟩ rw [he]; exact List.mem_append_left _ hi lemma topBlocks_split {a : ℝ} {J : Finset ℕ} {j : ℕ} (hj : j∈J) : ∃ pre post : List ℕ, topBlocks a J=pre++topBlock a j++post ∧ post.length≤J.card*topIterations ∧ (∀ n∈post, ∃ i∈J, i topBlocks a (J w)++accurateBlock m ((100:ℝ)^w/4)) lemma windowBlocks_succ (a : ℝ) (m : ℕ) (J : ℕ → Finset ℕ) (W : ℕ) : windowBlocks a m J (W+1)=topBlocks a (J W)++accurateBlock m ((100:ℝ)^W/4)++ windowBlocks a m J W := by simp only [windowBlocks,List.range_succ,List.reverse_append,List.reverse_singleton, List.flatMap_append,List.flatMap_cons,List.flatMap_nil,List.append_nil,List.append_assoc] lemma windowBlocks_split (a : ℝ) (m : ℕ) (J : ℕ → Finset ℕ) {W w : ℕ} (hw : w omega | succ W ih => rw [windowBlocks_succ] by_cases he : w=W · subst w; exact ⟨[],by simp only [List.nil_append]⟩ · obtain ⟨pre,hp⟩ := ih (by omega) rw [hp] exact ⟨topBlocks a (J W)++accurateBlock m ((100:ℝ)^W/4)++pre,by simp only [List.append_assoc]⟩ lemma windowBlocks_mem (a : ℝ) (m : ℕ) (J : ℕ → Finset ℕ) {W n : ℕ} (hn : n∈windowBlocks a m J W) : ∃ w simp only [Finset.sum_empty]; positivity | @insert a J hmax ih => have ha : a∉J := fun h => (lt_irrefl a) (hmax a h) have hci : ∀ b∈J, b+K≤a := by intro b hb exact hsep b (Finset.mem_insert_of_mem hb) a (Finset.mem_insert_self ..) (hmax b hb) have hi := ih (fun b hb c hc hbc => hsep b (Finset.mem_insert_of_mem hb) c (Finset.mem_insert_of_mem hc) hbc) hci rw [Finset.sum_insert ha] have hp := pow_le_pow_right₀ (by norm_num : (1:ℝ)≤2) (hcap a (Finset.mem_insert_self ..)) have hd := (div_le_div_iff_of_pos_right hden).mpr hp rw [pow_add] at hd have he : (2:ℝ)^a+(2:ℝ)^a/((2:ℝ)^K-1)=(2:ℝ)^a*2^K/(2^K-1) := by field_simp; ring exact (add_le_add (le_refl ((2:ℝ)^a)) hi).trans (he ▸ hd) lemma residueWeight_union_le (F G : Finset GaussianInt) : residueWeight (F∪G)≤residueWeight F+residueWeight G := by classical have hh := Finset.sum_union_inter (s₁ := F) (s₂ := G) (f := fun f : GaussianInt => Real.log f.norm.natAbs) change residueWeight (F∪G)+residueWeight (F∩G)=residueWeight F+residueWeight G at hh linarith only [hh,residueWeight_nonneg (F∩G)] lemma residueWeight_biUnion_le {ι : Type uIota} (J : Finset ι) (F : ι → Finset GaussianInt) : residueWeight (J.biUnion F)≤∑ j∈J, residueWeight (F j) := by induction J using Finset.induction_on with | empty => simp [residueWeight] | @insert j J hj ih => rw [Finset.biUnion_insert,Finset.sum_insert hj] exact (residueWeight_union_le _ _).trans (add_le_add (le_refl _) ih) lemma separated_factor_cost (J : Finset ℕ) {K j : ℕ} (hK : 0 (dyadicSieve a).availableFactors))≤ (4*Real.log 4/((2:ℝ)^K-1))*(2:ℝ)^j := by have hsum := Finset.sum_le_sum (fun a (_ : a∈J) => dyadicSieve_available_weight a) rw [←Finset.mul_sum] at hsum have hh := mul_le_mul_of_nonneg_left (separated_power_sum J hK hsep hcap) (show 0≤4*Real.log 4 by positivity) have ht := (residueWeight_biUnion_le J (fun a => (dyadicSieve a).availableFactors)).trans (hsum.trans hh) calc _ ≤ 4*Real.log 4*((2:ℝ)^j/(2^K-1)) := ht _ = _ := by ring open scoped BigOperators Classical open Filter lemma log_two_lower : (1/2:ℝ)≤Real.log 2 := by have hh := Real.one_sub_inv_le_log_of_pos (by norm_num : (0:ℝ)<2) norm_num at hh exact hh lemma narrow_bins_card {X : ℝ} (hX : 2≤X) (J : Finset ℕ) (hJ : ∀ j∈J, (j:ℝ)*Real.log 2≤21/20*X) : (J.card:ℝ)≤3*X := by have hj : ∀ j∈J, (j:ℝ)≤21/10*X := by intro j hj have hh := mul_le_mul_of_nonneg_left log_two_lower (Nat.cast_nonneg j) linarith only [hh,hJ j hj] have hs : J⊆Finset.range (⌊21/10*X⌋₊+1) := by intro j hj' apply Finset.mem_range.mpr exact Nat.lt_succ_of_le ((Nat.le_floor_iff (by positivity)).mpr (hj j hj')) have hc : (J.card:ℝ)≤(⌊21/10*X⌋₊:ℝ)+1 := by exact_mod_cast (Finset.card_le_card hs).trans_eq (Finset.card_range _) have hf := Nat.floor_le (show 0≤21/10*X by linarith only [hX]) linarith only [hc,hf,hX] lemma topBlock_separated {a : ℝ} (ha : 0 (hJ i hi).2.1) have hlen' : (post.length+2:ℝ)≤Real.exp (topParameter*topCoefficient a*(2:ℝ)^j) := by have hp : (post.length:ℝ)≤(J.card:ℝ)*topIterations := by exact_mod_cast hpl simp only [pow_one,hT] at hlen have he := Real.add_one_le_exp (topParameter*topCoefficient a*(2:ℝ)^j) norm_num only [topIterations,Nat.cast_ofNat] at hp nlinarith only [hp,hcard,hlen,he,hlog,hX2] have hprime : ∀ p∈(dyadicSieve j).primes, Real.exp (j*Real.log 2)≤(p:ℝ) ∧ (p:ℝ)≤2*Real.exp (j*Real.log 2) := by intro p hp rw [hT] have hh := (PrimeLogMass.mem_dyadicBatch j p).mp hp constructor · exact_mod_cast hh.1 · have hh' : p≤2^(j+1) := hh.2.1.le rw [pow_succ] at hh' exact_mod_cast (by simpa only [mul_comm] using hh' : p≤2*2^j) have hdense : δ*Real.exp (j*Real.log 2)/Real.log (2*Real.exp (j*Real.log 2))≤ (dyadicSieve j).primes.card := by change δ*Real.exp (j*Real.log 2)/Real.log (2*Real.exp (j*Real.log 2))≤ (PrimeLogMass.dyadicBatch j).card simpa only [hT,pow_succ,mul_comm] using (hJ j hj).2.2 have hpst : ∀ n∈post, (n:ℝ)≤Real.exp (topParameter*topCoefficient a*Real.exp (j*Real.log 2)) := by intro n hnp obtain ⟨i,hi,hij,hni⟩ := hpm n hnp rw [hT] exact topBlock_separated ha hgap (hsep i hi j hj hij) n hni obtain ⟨hb,hbsize⟩ := hband (dyadicSieve j) (initial++pre) post hn hprime hdense hpst (by simpa only [hT] using hlen') refine ⟨?_,?_⟩ · simpa only [hT,he,topBlock,List.append_assoc] using hb · simpa only [hT,Real.log_pow] using hbsize end GaussianMoat end OAI