Grace/Cumulus/NumberTheory/GaussCount.g17

Open in Workbench Download source

Imports

Grace.Cumulus.NumberTheory.Wilson · Grace.Cumulus.NumberTheory.ResidueLists · Grace.Cumulus.Cardinal

Declarations

Source

1-- The half range 1..h, the Gauss set of k in it whose residue a k mod p2-- exceeds h, the folding of a residue list onto 1..h, the count of large3-- residues in a list, and the bridge between that list count and the set4-- cardinality.5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.NumberTheory.GaussCount11import Grace.Cumulus.NumberTheory.Wilson12import Grace.Cumulus.NumberTheory.ResidueLists13import Grace.Cumulus.Cardinal1415-- The half range {1, …, h}: a finite set of naturals with h members.16def halfRange (h : Nat) : Set := { k ∈ succ(h) | 0 < k }1718theorem halfRange.mem_iff (h k : Nat)19  shows k ∈ halfRange(h) ↔ 0 < k ∧ k ≤ h20proof21  constructor22  . intro member23    have parts := halfRange.membership.forward with member24    have below := Less.of_membership at k, succ(h) with parts.left25    have bound : k ≤ h by arith26    exact ⟨parts.right, bound⟩27  . intro both28    obtain ⟨positive, bound⟩ := both29    have below : k < succ(h) by arith30    have inSucc := membership.of_Less at k, succ(h) with *, *, below31    exact halfRange.membership.backward with ⟨inSucc, positive⟩3233-- A member of the half range is a natural.34theorem halfRange.member_nat (h : Nat) (k : Set)35  requires member : k ∈ halfRange(h)36  shows Nat(k)37proof38  have parts := halfRange.membership.forward with member39  exact natural.member at succ(h) with parts.left4041theorem halfRange.finite (h : Nat)42  shows Finite(halfRange(h))43proof44  have inside : halfRange(h) ⊆ succ(h)45    intros k member46    have parts := halfRange.membership.forward with member47    exact parts.left48  exact Finite.subset with (Finite.natural at succ(h)), inside4950theorem halfRange.succ (h : Nat)51  shows halfRange(succ(h)) = insert(succ(h), halfRange(h))52proof53  ext k54  constructor55  . intro member56    have parts := halfRange.membership.forward with member57    have kNat := natural.member at succ(succ(h)) with parts.left58    have both := (halfRange.mem_iff at succ(h), k).forward with member59    obtain ⟨positive, atMost⟩ := both60    have split := (LessEqual.iff_Less_or_eq at k, succ(h)).forward with atMost61    apply insert.membership.backward62    cases split with below | same63    . right64      have bound : k ≤ h by arith65      exact (halfRange.mem_iff at h, k).backward with ⟨positive, bound⟩66    . left67      exact same68  . intro member69    have split := insert.membership.forward with member70    cases split with same | inRange71    . rw same72      have positive : 0 < succ(h) by arith73      have bound : succ(h) ≤ succ(h) by arith74      exact (halfRange.mem_iff at succ(h), succ(h)).backward75        with ⟨positive, bound⟩76    . have parts := halfRange.membership.forward with inRange77      have kNat := natural.member at succ(h) with parts.left78      have both := (halfRange.mem_iff at h, k).forward with inRange79      obtain ⟨positive, atMost⟩ := both80      have bound : k ≤ succ(h) by arith81      exact (halfRange.mem_iff at succ(h), k).backward82        with ⟨positive, bound⟩8384theorem halfRange.cardinality (h : Nat)85  shows cardinality(halfRange(h)) = h86proof87  induction h88  | zero =>89    have empty : halfRange(0) = ∅90      ext k91      constructor92      . intro member93        exfalso94        have parts := halfRange.membership.forward with member95        have kNat := natural.member at succ(0) with parts.left96        have both := (halfRange.mem_iff at 0, k).forward with member97        obtain ⟨positive, bound⟩ := both98        have impossible : ¬k ≤ 0 by arith99        exact impossible with bound100      . intro member101        exfalso102        exact set.empty with member103    rw [empty, cardinality.empty]104    rfl105  | succ(j) using ih =>106    have finite := halfRange.finite at j107    have fresh : ¬succ(j) ∈ halfRange(j)108      intro member109      have both := (halfRange.mem_iff at j, succ(j)).forward with member110      have impossible : ¬succ(j) ≤ j by arith111      exact impossible with both.right112    have count := cardinality.insert_add_one with finite, fresh113    rw [halfRange.succ, count, ih]114    exact add.right_one at j115116-- The Gauss set: the k in 1..h whose least residue a k mod p lies above h.117def gaussSet (a p h : Nat) : Set :=118  { k ∈ succ(h) | 0 < k ∧ h < mod(mul(a, k), p) }119120theorem gaussSet.mem_iff (a p h k : Nat)121  shows k ∈ gaussSet(a, p, h) ↔ 0 < k ∧ k ≤ h ∧ h < (a * k) % p122proof123  constructor124  . intro member125    have parts := gaussSet.membership.forward with member126    have below := Less.of_membership at k, succ(h) with parts.left127    have bound : k ≤ h by arith128    exact ⟨parts.right.left, bound, parts.right.right⟩129  . intro all130    obtain ⟨positive, bound, large⟩ := all131    have below : k < succ(h) by arith132    have inSucc := membership.of_Less at k, succ(h) with *, *, below133    exact gaussSet.membership.backward with ⟨inSucc, positive, large⟩134135theorem gaussSet.subset (a p h : Nat)136  shows gaussSet(a, p, h) ⊆ halfRange(h)137proof138  intros k member139  have parts := gaussSet.membership.forward with member140  exact halfRange.membership.backward with ⟨parts.left, parts.right.left⟩141142theorem gaussSet.finite (a p h : Nat)143  shows Finite(gaussSet(a, p, h))144proof145  exact Finite.subset with (halfRange.finite at h), (gaussSet.subset at a, p, h)146147theorem gaussSet.cardinality_le (a p h : Nat)148  shows cardinality(gaussSet(a, p, h)) ≤ h149proof150  have bound := cardinality.monotone151    with (halfRange.finite at h), (gaussSet.subset at a, p, h)152  rwa halfRange.cardinality at bound153154-- The count of entries of a list that exceed h. An entry r is large when155-- r − h is nonzero, which the Boolean test isZero(r − h) decides.156def largeCount (h : Nat, xs : List(ℕ)) → Nat157  | h, [] => 0158  | h, r :: rest =>159    chooseNat(isZero(sub(r, h)), largeCount(h, rest), succ(largeCount(h, rest)))160161theorem largeCount.nil (h : Nat)162  shows largeCount(h, []) = 0163proof164  simp [largeCount]165166theorem isZero.sub_of_le (h r : Nat)167  requires small : r ≤ h168  shows isZero(r - h) = Bool.true169proof170  have zero := sub.eq_zero_of_le with small171  rw zero172  simp [isZero]173174theorem isZero.sub_of_Less (h r : Nat)175  requires large : h < r176  shows isZero(r - h) = Bool.false177proof178  have positive := sub.positive_of_Less with large179  have shape := natural.zero_or_successor at r - h180  cases shape with zero | successor181  . exfalso182    rw zero at positive183    exact Less.irreflexive at 0 with *, positive184  . obtain ⟨k, kNat, form⟩ := successor185    rw form186    simp [isZero]187188theorem largeCount.cons_small (h r : Nat) (rest : List(ℕ))189  requires small : r ≤ h190  shows largeCount(h, r :: rest) = largeCount(h, rest)191proof192  have test := isZero.sub_of_le with small193  simp [largeCount, test, chooseNat]194195theorem largeCount.cons_large (h r : Nat) (rest : List(ℕ))196  requires large : h < r197  shows largeCount(h, r :: rest) = succ(largeCount(h, rest))198proof199  have test := isZero.sub_of_Less with large200  simp [largeCount, test, chooseNat]201202theorem largeCount.natural (h : Nat) (xs : List(ℕ))203  shows Nat(largeCount(h, xs))204proof205  induction xs206  | List.nil =>207    rw largeCount.nil208    typecheck209  | List.cons(r, rest) using ih =>210    have split := Less.or_LessEqual at h, r211    cases split with large | small212    . rw largeCount.cons_large213      typecheck214    . rw largeCount.cons_small215      exact ih216217-- The folding of a list onto 1..h: an entry r stays when r ≤ h and becomes218-- p − r otherwise.219def foldedList (p h : Nat, xs : List(ℕ)) → List(ℕ)220  | p, h, [] => []221  | p, h, r :: rest =>222    chooseNat(isZero(sub(r, h)), r, sub(p, r)) :: foldedList(p, h, rest)223224theorem foldedList.nil (p h : Nat)225  shows foldedList(p, h, []) = []226proof227  simp [foldedList]228229theorem foldedList.cons_small (p h r : Nat) (rest : List(ℕ))230  requires small : r ≤ h231  shows foldedList(p, h, r :: rest) = r :: foldedList(p, h, rest)232proof233  have test := isZero.sub_of_le with small234  simp [foldedList, test, chooseNat]235236theorem foldedList.cons_large (p h r : Nat) (rest : List(ℕ))237  requires large : h < r238  shows foldedList(p, h, r :: rest) = (p - r) :: foldedList(p, h, rest)239proof240  have test := isZero.sub_of_Less with large241  simp [foldedList, test, chooseNat]242243theorem foldedList.list (p h : Nat) (xs : List(ℕ))244  shows List(ℕ, foldedList(p, h, xs))245proof246  induction xs247  | List.nil =>248    rw foldedList.nil249    typecheck250  | List.cons(r, rest) using ih =>251    have split := Less.or_LessEqual at h, r252    cases split with large | small253    . rw foldedList.cons_large254      typecheck255    . rw foldedList.cons_small256      typecheck257258theorem foldedList.length (p h : Nat) (xs : List(ℕ))259  shows length{ℕ}(foldedList(p, h, xs)) = length{ℕ}(xs)260proof261  induction xs262  | List.nil =>263    rw foldedList.nil264    rfl265  | List.cons(r, rest) using ih =>266    have right := length.cons at ℕ, r, rest with *267    have split := Less.or_LessEqual at h, r268    cases split with large | small269    . have left := length.cons at ℕ, p - r, foldedList(p, h, rest) with *270      rw [foldedList.cons_large, left, right, ih]271      rfl272    . have left := length.cons at ℕ, r, foldedList(p, h, rest) with *273      rw [foldedList.cons_small, left, right, ih]274      rfl275276-- Folding changes an entry below p by a sign, so the product of a list and277-- the product of its folding agree modulo p when the large count is even278-- and sum to 0 modulo p when it is odd. Both parities are carried through279-- the induction at once, since folding one more entry trades them: a small280-- entry keeps the count and the relation, a large entry p − r turns an281-- agreement modulo p into a vanishing sum and back.282theorem foldedList.product_sign (p h : Nat) (xs : List(ℕ))283  shows (∀ r, Nat(r) → 0 < multiplicity(r, xs) → r < p) →284    (Even(largeCount(h, xs)) →285      (∏ xs) % p = (∏ foldedList(p, h, xs)) % p) ∧286    (Odd(largeCount(h, xs)) → (∏ xs + ∏ foldedList(p, h, xs)) % p = 0)287proof288  induction xs289  | List.nil =>290    intro entries291    rw [foldedList.nil, largeCount.nil]292    constructor293    . intro even294      rfl295    . intro odd296      exfalso297      have even := Even.zero298      exact Even.not_odd at 0 with even, odd299  | List.cons(x, rest) using ih =>300    intro entries301    have xPositive : 0 < multiplicity(x, x :: rest)302      have self := multiplicity.cons_self at x, rest with *303      rw self304      arith305    have xNat : Nat(x) by typecheck306    have xBelow := entries with x, xNat, xPositive307    have nonzero : p ≠ 0308      intro zero309      rw zero at xBelow310      have impossible : ¬x < 0 by arith311      exact impossible with xBelow312    have restEntries : ∀ y, Nat(y) → 0 < multiplicity(y, rest) → y < p313      intros y yNat positive314      have lifted := multiplicity.positive_cons at y, x, rest with positive315      exact entries with y, yNat, lifted316    have previous := ih with restEntries317    have expandXs := listProduct.cons at x, rest with *318    have split := Less.or_LessEqual at h, x319    cases split with large | small320    . rw [largeCount.cons_large, foldedList.cons_large, expandXs]321      have expandFolded := listProduct.cons at p - x, foldedList(p, h, rest)322        with *323      rw expandFolded324      have xLe : x ≤ p by arith325      have splitP := mul.split_sub at p, x, ∏ foldedList(p, h, rest) with xLe326      constructor327      . intro even328        have shifted := (Even.succ_iff at largeCount(h, rest)).forward with even329        have restOdd := previous.right with shifted330        -- x R ≡ x R + p F = x (R + F) + (p − x) F, and x (R + F) ≡ 0.331        have identity : x * ∏ rest + p * ∏ foldedList(p, h, rest) =332          x * (∏ rest + ∏ foldedList(p, h, rest)) +333            (p - x) * ∏ foldedList(p, h, rest)334          rw splitP335          ring336        have multiple := mod.add_multiple at p, x * ∏ rest,337          ∏ foldedList(p, h, rest) with nonzero338        have modSplit := mod.add at p,339          x * (∏ rest + ∏ foldedList(p, h, rest)),340          (p - x) * ∏ foldedList(p, h, rest) with nonzero341        have step := mod.mul_right at p, x,342          ∏ rest + ∏ foldedList(p, h, rest) with nonzero343        have zero : x * 0 = 0 by ring344        have selfMod := mod.mod at p, (p - x) * ∏ foldedList(p, h, rest)345          with nonzero346        have zeroAdd : 0 + ((p - x) * ∏ foldedList(p, h, rest)) % p =347          ((p - x) * ∏ foldedList(p, h, rest)) % p by ring348        rw [← multiple, identity, modSplit, ← step, restOdd, zero,349          mod.zero_left, zeroAdd, selfMod]350        rfl351      . intro odd352        have shifted := (Odd.succ_iff at largeCount(h, rest)).forward with odd353        have restEven := previous.left with shifted354        -- x R + (p − x) F ≡ x F + (p − x) F = p F ≡ 0.355        have modSplit := mod.add at p, x * ∏ rest,356          (p - x) * ∏ foldedList(p, h, rest) with nonzero357        have left := mod.mul_right at p, x, ∏ rest with nonzero358        have right := mod.mul_right at p, x, ∏ foldedList(p, h, rest)359          with nonzero360        have merge := mod.add at p, x * ∏ foldedList(p, h, rest),361          (p - x) * ∏ foldedList(p, h, rest) with nonzero362        have total : x * ∏ foldedList(p, h, rest) +363            (p - x) * ∏ foldedList(p, h, rest) =364          0 + p * ∏ foldedList(p, h, rest)365          rw splitP366          ring367        have multiple := mod.add_multiple at p, 0, ∏ foldedList(p, h, rest)368          with nonzero369        rw [modSplit, ← left, restEven, right, ← merge, total, multiple]370        exact mod.zero_left at p371    . rw [largeCount.cons_small, foldedList.cons_small, expandXs]372      have expandFolded := listProduct.cons at x, foldedList(p, h, rest) with *373      rw expandFolded374      constructor375      . intro even376        have restEven := previous.left with even377        have left := mod.mul_right at p, x, ∏ rest with nonzero378        have right := mod.mul_right at p, x, ∏ foldedList(p, h, rest)379          with nonzero380        rw [← left, restEven, right]381        rfl382      . intro odd383        have restOdd := previous.right with odd384        have factor : x * ∏ rest + x * ∏ foldedList(p, h, rest) =385          x * (∏ rest + ∏ foldedList(p, h, rest)) by ring386        have step := mod.mul_right at p, x,387          ∏ rest + ∏ foldedList(p, h, rest) with nonzero388        have zero : x * 0 = 0 by ring389        rw [factor, ← step, restOdd, zero]390        exact mod.zero_left at p391392theorem foldedList.product_even (p h : Nat) (xs : List(ℕ))393  requires entries : ∀ r, Nat(r) → 0 < multiplicity(r, xs) → r < p394  requires even : Even(largeCount(h, xs))395  shows (∏ xs) % p = (∏ foldedList(p, h, xs)) % p396proof397  have both := foldedList.product_sign at p, h, xs with entries398  exact both.left with even399400theorem foldedList.product_odd (p h : Nat) (xs : List(ℕ))401  requires entries : ∀ r, Nat(r) → 0 < multiplicity(r, xs) → r < p402  requires odd : Odd(largeCount(h, xs))403  shows (∏ xs + ∏ foldedList(p, h, xs)) % p = 0404proof405  have both := foldedList.product_sign at p, h, xs with entries406  exact both.right with odd407408-- The residue list of 1..h scaled by a: the entries (a k) % p for409-- k = h, …, 1, a list of length h.410def gaussList (a p h : Nat) : Set := scaledList(a, p, factorialList(h))411412theorem gaussList.value (a p h : Nat)413  shows gaussList(a, p, h) = scaledList(a, p, factorialList(h))414proof415  unfold gaussList416  rfl417418theorem gaussList.list (a p h : Nat)419  shows List(ℕ, gaussList(a, p, h))420proof421  rw gaussList.value422  exact scaledList.list at a, p, factorialList(h) with (factorialList.list at h)423424theorem gaussList.length (a p h : Nat)425  shows length{ℕ}(gaussList(a, p, h)) = h426proof427  have list := factorialList.list at h428  rw [gaussList.value, scaledList.length, factorialList.length]429  rfl430431-- The bridge from lists to sets: the large entries of the residue list of432-- 1..h are counted by the Gauss set. The induction grows the range 1..m433-- while the threshold t stays, so the Gauss set is first taken with the434-- two kept apart, and the threshold h is set to the range at the end.435def gaussSetBelow (a p t m : Nat) : Set :=436  { k ∈ succ(m) | 0 < k ∧ t < mod(mul(a, k), p) }437438theorem gaussSetBelow.mem_iff (a p t m k : Nat)439  shows k ∈ gaussSetBelow(a, p, t, m) ↔440    0 < k ∧ k ≤ m ∧ t < (a * k) % p441proof442  constructor443  . intro member444    have parts := gaussSetBelow.membership.forward with member445    have below := Less.of_membership at k, succ(m) with parts.left446    have bound : k ≤ m by arith447    exact ⟨parts.right.left, bound, parts.right.right⟩448  . intro all449    obtain ⟨positive, bound, large⟩ := all450    have below : k < succ(m) by arith451    have inSucc := membership.of_Less at k, succ(m) with *, *, below452    exact gaussSetBelow.membership.backward with ⟨inSucc, positive, large⟩453454theorem gaussSetBelow.finite (a p t m : Nat)455  shows Finite(gaussSetBelow(a, p, t, m))456proof457  have inside : gaussSetBelow(a, p, t, m) ⊆ succ(m)458    intros k member459    have parts := gaussSetBelow.membership.forward with member460    exact parts.left461  exact Finite.subset with (Finite.natural at succ(m)), inside462463theorem gaussSetBelow.zero (a p t : Nat)464  shows gaussSetBelow(a, p, t, 0) = ∅465proof466  ext k467  constructor468  . intro member469    exfalso470    have parts := gaussSetBelow.membership.forward with member471    have kNat := natural.member at succ(0) with parts.left472    have all := (gaussSetBelow.mem_iff at a, p, t, 0, k).forward with member473    obtain ⟨positive, bound, large⟩ := all474    have impossible : ¬k ≤ 0 by arith475    exact impossible with bound476  . intro member477    exfalso478    exact set.empty with member479480theorem gaussSetBelow.fresh (a p t m : Nat)481  shows ¬succ(m) ∈ gaussSetBelow(a, p, t, m)482proof483  intro member484  have all := (gaussSetBelow.mem_iff at a, p, t, m, succ(m)).forward with member485  obtain ⟨positive, bound, large⟩ := all486  have impossible : ¬succ(m) ≤ m by arith487  exact impossible with bound488489theorem gaussSetBelow.succ_large (a p t m : Nat)490  requires large : t < (a * succ(m)) % p491  shows gaussSetBelow(a, p, t, succ(m)) =492    insert(succ(m), gaussSetBelow(a, p, t, m))493proof494  ext k495  constructor496  . intro member497    have parts := gaussSetBelow.membership.forward with member498    have kNat := natural.member at succ(succ(m)) with parts.left499    have all := (gaussSetBelow.mem_iff at a, p, t, succ(m), k).forward500      with member501    obtain ⟨positive, atMost, largeK⟩ := all502    have split := (LessEqual.iff_Less_or_eq at k, succ(m)).forward with atMost503    apply insert.membership.backward504    cases split with below | same505    . right506      have bound : k ≤ m by arith507      exact (gaussSetBelow.mem_iff at a, p, t, m, k).backward508        with ⟨positive, bound, largeK⟩509    . left510      exact same511  . intro member512    have split := insert.membership.forward with member513    cases split with same | inSet514    . rw same515      have positive : 0 < succ(m) by arith516      have bound : succ(m) ≤ succ(m) by arith517      exact (gaussSetBelow.mem_iff at a, p, t, succ(m), succ(m)).backward518        with ⟨positive, bound, large⟩519    . have parts := gaussSetBelow.membership.forward with inSet520      have kNat := natural.member at succ(m) with parts.left521      have all := (gaussSetBelow.mem_iff at a, p, t, m, k).forward with inSet522      obtain ⟨positive, atMost, largeK⟩ := all523      have bound : k ≤ succ(m) by arith524      exact (gaussSetBelow.mem_iff at a, p, t, succ(m), k).backward525        with ⟨positive, bound, largeK⟩526527theorem gaussSetBelow.succ_small (a p t m : Nat)528  requires small : (a * succ(m)) % p ≤ t529  shows gaussSetBelow(a, p, t, succ(m)) = gaussSetBelow(a, p, t, m)530proof531  ext k532  constructor533  . intro member534    have parts := gaussSetBelow.membership.forward with member535    have kNat := natural.member at succ(succ(m)) with parts.left536    have all := (gaussSetBelow.mem_iff at a, p, t, succ(m), k).forward537      with member538    obtain ⟨positive, atMost, largeK⟩ := all539    have split := (LessEqual.iff_Less_or_eq at k, succ(m)).forward with atMost540    cases split with below | same541    . have bound : k ≤ m by arith542      exact (gaussSetBelow.mem_iff at a, p, t, m, k).backward543        with ⟨positive, bound, largeK⟩544    . exfalso545      rw same at largeK546      have impossible : ¬t < (a * succ(m)) % p by arith547      exact impossible with largeK548  . intro member549    have parts := gaussSetBelow.membership.forward with member550    have kNat := natural.member at succ(m) with parts.left551    have all := (gaussSetBelow.mem_iff at a, p, t, m, k).forward with member552    obtain ⟨positive, atMost, largeK⟩ := all553    have bound : k ≤ succ(m) by arith554    exact (gaussSetBelow.mem_iff at a, p, t, succ(m), k).backward555      with ⟨positive, bound, largeK⟩556557theorem largeCount.gaussSetBelow (a p t m : Nat)558  shows largeCount(t, scaledList(a, p, factorialList(m))) =559    cardinality(gaussSetBelow(a, p, t, m))560proof561  induction m562  | zero =>563    rw [factorialList.zero, scaledList.nil, largeCount.nil, gaussSetBelow.zero,564      cardinality.empty]565    rfl566  | succ(j) using ih =>567    have restList := factorialList.list at j568    have scaledRest := scaledList.list at a, p, factorialList(j) with restList569    have finite := gaussSetBelow.finite at a, p, t, j570    have fresh := gaussSetBelow.fresh at a, p, t, j571    rw [factorialList.succ, scaledList.cons]572    have split := Less.or_LessEqual at t, (a * succ(j)) % p573    cases split with large | small574    . rw [largeCount.cons_large, gaussSetBelow.succ_large]575      have count := cardinality.insert_add_one with finite, fresh576      have one := add.right_one at cardinality(gaussSetBelow(a, p, t, j))577      rw [count, ih, one]578      rfl579    . rw [largeCount.cons_small, gaussSetBelow.succ_small]580      exact ih581582theorem largeCount.gaussSet (a p h : Nat)583  shows largeCount(h, gaussList(a, p, h)) = cardinality(gaussSet(a, p, h))584proof585  have same : gaussSet(a, p, h) = gaussSetBelow(a, p, h, h)586    ext k587    constructor588    . intro member589      have parts := gaussSet.membership.forward with member590      exact gaussSetBelow.membership.backward with parts591    . intro member592      have parts := gaussSetBelow.membership.forward with member593      exact gaussSet.membership.backward with parts594  rw [gaussList.value, same]595  exact largeCount.gaussSetBelow at a, p, h, h596