Lean / Reader
Lean reader

LazyLucasRuns.lean

173 lines14 declarationsno sorryimports LazyLucasCore

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports LazyLucasCore, which it does not have: you can read it there, but it will not check. Build the project itself to check it.

  1. import LazyLucasCore
  2. namespace LazyLucas
  3. private theorem cap_id (k : ℕ) : cap k + 1 = lucas (k+1) := by
  4. induction k with
  5. | zero => decide
  6. | succ k ih =>
  7. change cap k + lucas k + 1 = lucas k + lucas (k+1)
  8. omega
  9. private theorem lucas_pos (k : ℕ) : 0 < lucas k := by
  10. induction k using Nat.strong_induction_on with
  11. | h k ih =>
  12. rcases k with _|k
  13. · decide
  14. rcases k with _|k
  15. · decide
  16. change 0 < lucas k + lucas (k+1)
  17. have := ih k (by omega)
  18. omega
  19. private theorem lucas_next (k : ℕ) (hk : 1≤k) : lucas k < lucas (k+1) := by
  20. obtain ⟨j,rfl⟩ : ∃ j,k=j+1 := ⟨k-1,by omega⟩
  21. change lucas (j+1) < lucas j + lucas (j+1)
  22. have := lucas_pos j
  23. omega
  24. private theorem lucas_three (k : ℕ) (hk : 2≤k) : 3 ≤ lucas k := by
  25. induction k,hk using Nat.le_induction with
  26. | base => decide
  27. | succ k hk ih => exact ih.trans (lucas_next k (by omega)).le
  28. private theorem cap_ge (k : ℕ) : k ≤ cap k := by
  29. induction k with
  30. | zero => decide
  31. | succ k ih =>
  32. change k+1 ≤ cap k+lucas k
  33. have := lucas_pos k
  34. omega
  35. section
  36. variable (s : ℕ → ℕ)
  37. variable (rep : ∀ {k n : ℕ}, 2 ≤ k → n ≤ cap k → s n = bounded k n)
  38. include rep in
  39. private theorem upper_shift {k n : ℕ} (hk : 2≤k) (lo : cap k<n) (hi : n≤cap (k+1)) :
  40. s n = 1+s (n-lucas k) := by
  41. rw [rep (k:=k+1) (by omega) hi,bounded_step hk, if_neg (by omega),rep hk]
  42. change n ≤ cap k + lucas k at hi
  43. omega
  44. include rep in
  45. private theorem last_pair (k : ℕ) (hk : 3 ≤ k) :
  46. s (cap k-2) = s (cap k-1) := by
  47. induction k,hk using Nat.le_induction with
  48. | base =>
  49. rw [rep (k:=3) (by decide) (by decide),rep (k:=3) (by decide) (by decide)]
  50. decide
  51. | succ k hk ih =>
  52. have l := lucas_three k (by omega)
  53. have c := cap_ge k
  54. have e : cap (k+1) = cap k+lucas k := rfl
  55. rw [upper_shift s rep (k:=k) (by omega) (by omega) (by omega),
  56. upper_shift s rep (k:=k) (n:=cap (k+1)-1) (by omega) (by omega) (by omega)]
  57. simp only [e,show cap k+lucas k-2-lucas k=cap k-2 by omega,
  58. show cap k+lucas k-1-lucas k=cap k-1 by omega,ih]
  59. include rep in
  60. private theorem first_pair (k : ℕ) (hk : 2≤k) :
  61. s (cap k+1) = s (cap k+2) := by
  62. induction k using Nat.strong_induction_on with
  63. | h k ih =>
  64. by_cases hb : k≤3
  65. · interval_cases k <;> rw [rep (k:=5) (by decide) (by decide),rep (k:=5) (by decide) (by decide)] <;> decide
  66. · obtain ⟨j,rfl⟩ : ∃ j,k=j+2 := ⟨k-2,by omega⟩
  67. have l := lucas_three (j+2) (by omega)
  68. have e : cap (j+2+1)=cap (j+2)+lucas (j+2) := rfl
  69. have p : cap (j+2)+1=lucas (j+2)+lucas (j+1) := (cap_id _).trans (Nat.add_comm _ _)
  70. have q := cap_id j
  71. rw [upper_shift s rep (k:=j+2) (by omega) (by omega) (by omega),
  72. upper_shift s rep (k:=j+2) (n:=cap (j+2)+2) (by omega) (by omega) (by omega)]
  73. rw [show cap (j+2)+1-lucas (j+2)=cap j+1 by omega,
  74. show cap (j+2)+2-lucas (j+2)=cap j+2 by omega,ih j (by omega) (by omega)]
  75. private def HasAdjacentPair (s : ℕ → ℕ) (n : ℕ) : Prop :=
  76. s n = s (n+1) ∨ s (n+1) = s (n+2) ∨ s (n+2) = s (n+3)
  77. include rep in
  78. private theorem adjacent_prefix (k : ℕ) (hk : 3≤k) :
  79. ∀ n, n+3 ≤ cap k → HasAdjacentPair s n := by
  80. induction k,hk using Nat.le_induction with
  81. | base =>
  82. intro n hn
  83. have h3 : n≤3 := by change n+3≤6 at hn; omega
  84. interval_cases n <;> unfold HasAdjacentPair <;>
  85. simp only [rep (k:=3) (by decide) (by decide : 0≤cap 3),rep (k:=3) (by decide) (by decide : 1≤cap 3),
  86. rep (k:=3) (by decide) (by decide : 2≤cap 3),rep (k:=3) (by decide) (by decide : 3≤cap 3),
  87. rep (k:=3) (by decide) (by decide : 4≤cap 3),rep (k:=3) (by decide) (by decide : 5≤cap 3),
  88. rep (k:=3) (by decide) (by decide : 6≤cap 3),Nat.reduceAdd] <;> decide
  89. | succ k hk ih =>
  90. intro n hn
  91. by_cases low : n+3 ≤ cap k
  92. · exact ih n low
  93. by_cases high : cap k < n
  94. · have e : cap (k+1) = cap k + lucas k := rfl
  95. have ek := cap_id k
  96. have lk := lucas_next k (by omega)
  97. have h1 : lucas k ≤ n := by omega
  98. have prev := ih (n-lucas k) (by omega)
  99. unfold HasAdjacentPair at prev ⊢
  100. have shift (i : ℕ) (hi : i≤3) : s (n+i) = 1+s ((n-lucas k)+i) := by
  101. rw [upper_shift s rep (k:=k) (by omega) (by omega) (by omega)]
  102. congr 2
  103. omega
  104. have h0 := shift 0 (by decide)
  105. simp only [Nat.add_zero] at h0
  106. rw [h0,shift 1 (by decide),shift 2 (by decide),shift 3 (by decide)]
  107. simpa only [Nat.add_left_cancel_iff] using prev
  108. · unfold HasAdjacentPair
  109. have he := last_pair s rep k hk
  110. have hs := first_pair s rep k (by omega)
  111. have c := cap_ge k
  112. rcases (show n=cap k-2 ∨ n+1=cap k ∨ n=cap k by omega) with h|h|h
  113. · left; convert he using 1 <;> congr 1 <;> omega
  114. · right; right; convert hs using 1 <;> congr 1 <;> omega
  115. · right; left; simpa [h] using hs
  116. include rep in
  117. theorem adjacent_pair (n : ℕ) : HasAdjacentPair s n :=
  118. adjacent_prefix s rep (n+3) (by omega) n (cap_ge _)
  119. include rep in
  120. private theorem digit_lower_prefix (k : ℕ) (hk : 2≤k) :
  121. ∀ n, n≤cap k → (0<n → 0<s n) ∧ (3≤n → 2≤s n) := by
  122. induction k,hk using Nat.le_induction with
  123. | base =>
  124. intro n hn
  125. rw [rep (k:=2) (by decide) hn,bounded_two]
  126. change n≤3 at hn
  127. interval_cases n <;> decide
  128. | succ k hk ih =>
  129. intro n hn
  130. by_cases low : n≤cap k
  131. · exact ih n low
  132. · rw [upper_shift s rep (k:=k) hk (by omega) hn]
  133. have e : cap (k+1)=cap k+lucas k := rfl
  134. have lex := lucas_next k (by omega)
  135. have cid := cap_id k
  136. have h1 := (ih (n-lucas k) (by omega)).1 (by omega)
  137. omega
  138. private theorem adjacent_not_divisible {x d : ℕ} (hd : 2≤d) (h : d∣x) (h' : d∣x+1) : False := by
  139. have h1 : d∣1 := (Nat.dvd_add_right h).mp h'
  140. exact (by have := Nat.le_of_dvd (by decide : 0<1) h1; omega)
  141. include rep in
  142. theorem no_four_for_digit_count (n : ℕ) (hn : 1≤n) :
  143. ¬(s n∣n ∧ s (n+1)∣n+1 ∧ s (n+2)∣n+2 ∧ s (n+3)∣n+3) := by
  144. rintro ⟨h₀,h₁,h₂,h₃⟩
  145. by_cases h : n≤2
  146. · have bad : ¬ s 3 ∣ 3 := by rw [rep (k:=2) (by decide) (by decide)]; decide
  147. interval_cases n <;> contradiction
  148. · have lower (t : ℕ) (ht : n≤t) : 2≤s t :=
  149. (digit_lower_prefix s rep (t+2) (by omega) t (by have := cap_ge (t+2); omega)).2 (by omega)
  150. rcases adjacent_pair s rep n with h|h|h
  151. · exact adjacent_not_divisible (lower n le_rfl) h₀ (h ▸ h₁)
  152. · exact adjacent_not_divisible (lower _ (by omega)) h₁ (h ▸ h₂)
  153. · exact adjacent_not_divisible (lower _ (by omega)) h₂ (h ▸ h₃)
  154. end
  155. end LazyLucas