import Std /-! # The five-term recurrence for OEIS A202883 Run `lean A202883.lean` with Lean 4.15.0. No mathlib dependency. `a n` filters the actual list of boards by the local neighbour condition. The main results are `recurrence`, `initial`, and `generating_function_coefficients`. -/ namespace A202883 /-- The two digits are the entries in the left and right cells, respectively. -/ inductive Row where | r00 | r01 | r02 | r10 | r11 | r12 | r20 | r21 | r22 deriving DecidableEq, Repr open Row def rows : List Row := [r00,r01,r02,r10,r11,r12,r20,r21,r22] theorem rows_complete (r : Row) : r ∈ rows := by cases r <;> decide theorem rows_nodup : rows.Nodup := by decide def left : Row → Nat | r00 | r01 | r02 => 0 | r10 | r11 | r12 => 1 | r20 | r21 | r22 => 2 def right : Row → Nat | r00 | r10 | r20 => 0 | r01 | r11 | r21 => 1 | r02 | r12 | r22 => 2 def supported (cell across up down : Nat) : Bool := cell == 0 || decide (cell ≤ across) || decide (cell ≤ up) || decide (cell ≤ down) def rowOK (up here down : Row) : Bool := supported (left here) (right here) (left up) (left down) && supported (right here) (left here) (right up) (right down) /-- A missing neighbour has value zero, which can never support a positive cell. -/ def legalFrom (up here : Row) : List Row → Bool | [] => rowOK up here r00 | r :: rs => rowOK up here r && legalFrom here r rs def boards : Nat → List (List Row) | 0 => [[]] | n+1 => rows.flatMap fun r => (boards n).map (r :: ·) theorem mem_boards (n : Nat) (b : List Row) : b ∈ boards n ↔ b.length = n := by induction n generalizing b with | zero => cases b <;> simp [boards] | succ n ih => cases b with | nil => simp [boards, List.mem_flatMap, List.mem_map] | cons r rs => simp [boards, List.mem_flatMap, List.mem_map, rows_complete, ih] theorem boards_nodup (n : Nat) : (boards n).Nodup := by induction n with | zero => decide | succ n ih => apply List.pairwise_flatMap.mpr constructor · intro r _ exact ih.map (r :: ·) (by intro x y h eq; exact h (List.cons.inj eq).2) · apply rows_nodup.imp intro r s hrs x hx y hy eq obtain ⟨x', _, rfl⟩ := List.mem_map.mp hx obtain ⟨y', _, rfl⟩ := List.mem_map.mp hy exact hrs (List.cons.inj eq).1 /-- The extra top row 00 is always legal and supplies no positive support. -/ def a (n : Nat) : Nat := ((boards n).filter (legalFrom r00 r00)).length private def total {α : Type} (xs : List α) (f : α → Nat) : Nat := match xs with | [] => 0 | x :: tail => f x + total tail f private theorem total_append {α : Type} (xs ys : List α) (f : α → Nat) : total (xs ++ ys) f = total xs f + total ys f := by induction xs with | nil => simp [total] | cons x xs ih => simp [total, ih, Nat.add_assoc] private theorem total_map {α β : Type} (xs : List α) (g : α → β) (f : β → Nat) : total (xs.map g) f = total xs (fun x => f (g x)) := by induction xs with | nil => rfl | cons x xs ih => simp [total, ih] private theorem total_flatMap {α β : Type} (xs : List α) (g : α → List β) (f : β → Nat) : total (xs.flatMap g) f = total xs (fun x => total (g x) f) := by induction xs with | nil => rfl | cons x xs ih => simp [total, total_append, ih] private theorem total_zero {α : Type} (xs : List α) : total xs (fun _ => 0) = 0 := by induction xs with | nil => rfl | cons x xs ih => simp [total, ih] private theorem total_indicator {α : Type} (xs : List α) (p : α → Bool) : (xs.filter p).length = total xs (fun x => if p x then 1 else 0) := by induction xs with | nil => rfl | cons x xs ih => cases h : p x <;> simp [List.filter, total, h, ih, Nat.add_comm] abbrev Values := Row → Row → Nat def advance (f : Values) (up here : Row) : Nat := total rows fun r => if rowOK up here r then f here r else 0 def suffix : Nat → Values | 0 => fun up here => if rowOK up here r00 then 1 else 0 | n+1 => advance (suffix n) private theorem enumeration (n : Nat) (up here : Row) : total (boards n) (fun b => if legalFrom up here b then 1 else 0) = suffix n up here := by induction n generalizing up here with | zero => simp [boards, total, legalFrom, suffix] | succ n ih => simp only [boards, total_flatMap, total_map, suffix, advance] apply congrArg (total rows) funext r cases h : rowOK up here r <;> simp [legalFrom, h, total_zero, ih] theorem a_eq_suffix (n : Nat) : a n = suffix n r00 r00 := (total_indicator _ _).trans (enumeration n r00 r00) /-- Five consecutive steps can be certified by a small table. The columns are 00, 11, 22, then the settled and pending forms of 01, 02, 12, in that order. None of the entries is assumed: the next three lemmas check each local identity. -/ private def column (up here : Row) : Nat := match here with | r00 => 0 | r11 => 1 | r22 => 2 | r01 => if 1 ≤ right up then 3 else 4 | r10 => if 1 ≤ left up then 3 else 4 | r02 => if 2 ≤ right up then 5 else 6 | r20 => if 2 ≤ left up then 5 else 6 | r12 => if 2 ≤ right up then 7 else 8 | r21 => if 2 ≤ left up then 7 else 8 private def certificate (k : Nat) : List Nat := match k with | 0 => [1,1,1,1,0,1,0,1,0] | 1 => [3,5,9,4,3,6,3,7,3] | 2 => [35,37,51,36,27,43,22,44,22] | 3 => [265,283,369,274,190,317,138,326,138] | 4 => [1849,2017,2751,1933,1340,2300,1012,2384,1012] | 5 => [13345,14531,19851,13938,9737,16598,7435,17191,7435] | _ => [] private def sample (k : Nat) (up here : Row) : Nat := (certificate k)[column up here]?.getD 0 private theorem sample_zero : suffix 0 = sample 0 := by funext u r cases u <;> cases r <;> rfl set_option maxHeartbeats 3000000 in private theorem sample_step (k : Nat) (hk : k < 5) : advance (sample k) = sample (k+1) := by funext u r have h : k = 0 ∨ k = 1 ∨ k = 2 ∨ k = 3 ∨ k = 4 := by omega rcases h with h | h | h | h | h <;> subst k <;> cases u <;> cases r <;> rfl private theorem small (k : Nat) (hk : k ≤ 5) : suffix k = sample k := by induction k with | zero => exact sample_zero | succ k ih => exact (congrArg advance (ih (by omega))).trans (sample_step k (by omega)) private def combine (a b c d e : Nat) := 6*a + 2*b + 46*c + 25*d + 36*e private theorem sample_identity (u r : Row) : sample 5 u r = combine (sample 4 u r) (sample 3 u r) (sample 2 u r) (sample 1 u r) (sample 0 u r) := by cases u <;> cases r <;> rfl private theorem total_linear {α : Type} (xs : List α) (a b c d e : α → Nat) : total xs (fun x => combine (a x) (b x) (c x) (d x) (e x)) = combine (total xs a) (total xs b) (total xs c) (total xs d) (total xs e) := by induction xs with | nil => rfl | cons x xs ih => simp only [total] rw [ih] unfold combine omega private theorem advance_linear (a b c d e : Values) (u r : Row) : advance (fun i j => combine (a i j) (b i j) (c i j) (d i j) (e i j)) u r = combine (advance a u r) (advance b u r) (advance c u r) (advance d u r) (advance e u r) := by unfold advance rw [← total_linear] apply congrArg (total rows) funext s cases h : rowOK u r s <;> simp [h, combine] private theorem suffix_recurrence (n : Nat) (u r : Row) : suffix (n+5) u r = combine (suffix (n+4) u r) (suffix (n+3) u r) (suffix (n+2) u r) (suffix (n+1) u r) (suffix n u r) := by induction n generalizing u r with | zero => simpa only [small 5 (by decide), small 4 (by decide), small 3 (by decide), small 2 (by decide), small 1 (by decide), small 0 (by decide), Nat.zero_add] using sample_identity u r | succ n ih => have eq : suffix (n+5) = fun i j => combine (suffix (n+4) i j) (suffix (n+3) i j) (suffix (n+2) i j) (suffix (n+1) i j) (suffix n i j) := by funext i j; exact ih i j exact (congrArg (fun f => advance f u r) eq).trans (advance_linear (suffix (n+4)) (suffix (n+3)) (suffix (n+2)) (suffix (n+1)) (suffix n) u r) /-- The main theorem, quantified over all heights starting at five. -/ theorem recurrence (n : Nat) : a (n+5) = 6*a (n+4) + 2*a (n+3) + 46*a (n+2) + 25*a (n+1) + 36*a n := by simpa only [a_eq_suffix, combine] using suffix_recurrence n r00 r00 theorem initial : (a 0, a 1, a 2, a 3, a 4) = (1, 3, 35, 265, 1849) := by simp only [a_eq_suffix, small 0 (by decide), small 1 (by decide), small 2 (by decide), small 3 (by decide), small 4 (by decide)] rfl /-- Coefficient of x^n in x^d times B(x), where B(x) starts with a(1). -/ def shifted : Nat → Nat → Nat | 0, n => a (n+1) | _+1, 0 => 0 | d+1, n+1 => shifted d n def numeratorCoeff : Nat → Nat | 0 => 3 | 1 => 17 | 2 => 49 | 3 => 51 | 4 => 36 | _ => 0 /-- Q(x)B(x)=P(x) coefficient by coefficient, with negative terms moved to the right. Here B(x)=a(1)+a(2)x+..., P=3+17x+49x²+51x³+36x⁴, and Q=1-6x-2x²-46x³-25x⁴-36x⁵. -/ theorem generating_function_coefficients (n : Nat) : a (n+1) = 6*shifted 1 n + 2*shifted 2 n + 46*shifted 3 n + 25*shifted 4 n + 36*shifted 5 n + numeratorCoeff n := by match n with | 0 | 1 | 2 | 3 | 4 => simp only [shifted, numeratorCoeff, a_eq_suffix, Nat.reduceAdd, small 0 (by decide), small 1 (by decide), small 2 (by decide), small 3 (by decide), small 4 (by decide), small 5 (by decide)] <;> rfl | n+5 => simp only [shifted, numeratorCoeff, Nat.add_zero] exact recurrence (n+1) #print axioms recurrence #print axioms initial #print axioms generating_function_coefficients end A202883