/-
Copyright 2026 The Formal Conjectures Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
module
public import FormalConjecturesUtilDensity of ones in the Kolakoski sequence
The Kolakoski sequence is the self-describing sequence of ones and twos whose terms are the lengths of its successive runs. Its official indexing begins with $a(1) = 1$; here it is extended by $a(0) = 0$.
References:
E. S. Kupin and E. S. Rowland, "Bounds on the frequency of 1 in the Kolakoski word", arXiv:0809.2776 [math.CO] (2008).
@[expose] public sectionnamespace OeisA2
wordPrefix n is the finite Kolakoski word after expanding n runs beyond the seed [1, 2, 2].
At stage $n$, position $n+2$ supplies the next run length, and the emitted symbol is $1$ for
even $n$ and $2$ for odd $n$.
def wordPrefix : ℕ → List ℕ
| 0 => [1, 2, 2]
| n + 1 =>
let xs := wordPrefix n
let runLength := if xs.getD (n + 2) 1 = 2 then 2 else 1
let nextSymbol := if n % 2 = 0 then 1 else 2
xs ++ List.replicate runLength nextSymbolEvery generated prefix is long enough for the next unread run length.
@[category API, AMS 11 68]
theorem prefix_length (n : ℕ) : n + 3 ≤ (wordPrefix n).length := n:ℕ⊢ n + 3 ≤ (wordPrefix n).length
induction n with
⊢ 0 + 3 ≤ (wordPrefix 0).length All goals completed! 🐙
n:ℕih:n + 3 ≤ (wordPrefix n).length⊢ n + 1 + 3 ≤ (wordPrefix (n + 1)).length
n:ℕih:n + 3 ≤ (wordPrefix n).length⊢ n + 1 + 3 ≤ (wordPrefix n).length + if (wordPrefix n).getD (n + 2) 1 = 2 then 2 else 1
n:ℕih:n + 3 ≤ (wordPrefix n).lengthh✝:(wordPrefix n).getD (n + 2) 1 = 2⊢ n + 1 + 3 ≤ (wordPrefix n).length + 2n:ℕih:n + 3 ≤ (wordPrefix n).lengthh✝:¬(wordPrefix n).getD (n + 2) 1 = 2⊢ n + 1 + 3 ≤ (wordPrefix n).length + 1 n:ℕih:n + 3 ≤ (wordPrefix n).lengthh✝:(wordPrefix n).getD (n + 2) 1 = 2⊢ n + 1 + 3 ≤ (wordPrefix n).length + 2n:ℕih:n + 3 ≤ (wordPrefix n).lengthh✝:¬(wordPrefix n).getD (n + 2) 1 = 2⊢ n + 1 + 3 ≤ (wordPrefix n).length + 1 All goals completed! 🐙The read cursor used to generate the next run is always inside the generated prefix.
@[category API, AMS 11 68]
theorem cursor_lt_prefix_length (n : ℕ) : n + 2 < (wordPrefix n).length := n:ℕ⊢ n + 2 < (wordPrefix n).length
n:ℕh:n + 3 ≤ (wordPrefix n).length⊢ n + 2 < (wordPrefix n).length
All goals completed! 🐙
Every symbol in every generated prefix is either 1 or 2.
succ.inr n:ℕih:∀ x ∈ wordPrefix n, x = 1 ∨ x = 2x:ℕhx:x ∈ List.replicate (if (wordPrefix n).getD (n + 2) 1 = 2 then 2 else 1) (if n % 2 = 0 then 1 else 2)⊢ (if n % 2 = 0 then 1 else 2) = 1 ∨ (if n % 2 = 0 then 1 else 2) = 2
by_cases h : n % 2 = 0 pos n:ℕih:∀ x ∈ wordPrefix n, x = 1 ∨ x = 2x:ℕhx:x ∈ List.replicate (if (wordPrefix n).getD (n + 2) 1 = 2 then 2 else 1) (if n % 2 = 0 then 1 else 2)h:n % 2 = 0⊢ (if n % 2 = 0 then 1 else 2) = 1 ∨ (if n % 2 = 0 then 1 else 2) = 2neg n:ℕih:∀ x ∈ wordPrefix n, x = 1 ∨ x = 2x:ℕhx:x ∈ List.replicate (if (wordPrefix n).getD (n + 2) 1 = 2 then 2 else 1) (if n % 2 = 0 then 1 else 2)h:¬n % 2 = 0⊢ (if n % 2 = 0 then 1 else 2) = 1 ∨ (if n % 2 = 0 then 1 else 2) = 2 <;> pos n:ℕih:∀ x ∈ wordPrefix n, x = 1 ∨ x = 2x:ℕhx:x ∈ List.replicate (if (wordPrefix n).getD (n + 2) 1 = 2 then 2 else 1) (if n % 2 = 0 then 1 else 2)h:n % 2 = 0⊢ (if n % 2 = 0 then 1 else 2) = 1 ∨ (if n % 2 = 0 then 1 else 2) = 2neg n:ℕih:∀ x ∈ wordPrefix n, x = 1 ∨ x = 2x:ℕhx:x ∈ List.replicate (if (wordPrefix n).getD (n + 2) 1 = 2 then 2 else 1) (if n % 2 = 0 then 1 else 2)h:¬n % 2 = 0⊢ (if n % 2 = 0 then 1 else 2) = 1 ∨ (if n % 2 = 0 then 1 else 2) = 2 simp [h] All goals completed! 🐙
The Kolakoski sequence, using its official one-based indexing and extended by a 0 = 0.
def a : ℕ → ℕ
| 0 => 0
| n + 1 => (wordPrefix n).getD n 0Every positive-index term of the public sequence is one or two.
@[category API, AMS 11 68]
theorem a_one_or_two {n : ℕ} (hn : 0 < n) : a n = 1 ∨ a n = 2 := by n:ℕhn:0 < n⊢ a n = 1 ∨ a n = 2
obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by n:ℕhn:0 < n⊢ n ≠ 0 n:ℕhn:0 < n.succ⊢ a n.succ = 1 ∨ a n.succ = 2 omega All goals completed! 🐙 n:ℕhn:0 < n.succ⊢ a n.succ = 1 ∨ a n.succ = 2 : n ≠ 0) n:ℕhn:0 < n.succ⊢ a n.succ = 1 ∨ a n.succ = 2
have hlt : n < (wordPrefix n).length := by n:ℕhn:0 < n⊢ a n = 1 ∨ a n = 2 n:ℕhn:0 < n.succhlt:n < (wordPrefix n).length⊢ a n.succ = 1 ∨ a n.succ = 2
have h := prefix_length n n:ℕhn:0 < n.succh:n + 3 ≤ (wordPrefix n).length⊢ n < (wordPrefix n).length n:ℕhn:0 < n.succhlt:n < (wordPrefix n).length⊢ a n.succ = 1 ∨ a n.succ = 2
omega n:ℕhn:0 < n.succhlt:n < (wordPrefix n).length⊢ a n.succ = 1 ∨ a n.succ = 2 n:ℕhn:0 < n.succhlt:n < (wordPrefix n).length⊢ a n.succ = 1 ∨ a n.succ = 2
apply prefix_mem_one_or_two n n:ℕhn:0 < n.succhlt:n < (wordPrefix n).length⊢ a n.succ ∈ wordPrefix n
rw [a n:ℕhn:0 < n.succhlt:n < (wordPrefix n).length⊢ (wordPrefix n).getD n 0 ∈ wordPrefix n n:ℕhn:0 < n.succhlt:n < (wordPrefix n).length⊢ (wordPrefix n).getD n 0 ∈ wordPrefix n] n:ℕhn:0 < n.succhlt:n < (wordPrefix n).length⊢ (wordPrefix n).getD n 0 ∈ wordPrefix n
have hget : (wordPrefix n).getD n 0 = (wordPrefix n)[n] :=
List.getD_eq_getElem (l := wordPrefix n) (d := 0) hlt n:ℕhn:0 < n.succhlt:n < (wordPrefix n).lengthhget:(wordPrefix n).getD n 0 = (wordPrefix n)[n]⊢ (wordPrefix n).getD n 0 ∈ wordPrefix n
rw [hget n:ℕhn:0 < n.succhlt:n < (wordPrefix n).lengthhget:(wordPrefix n).getD n 0 = (wordPrefix n)[n]⊢ (wordPrefix n)[n] ∈ wordPrefix n n:ℕhn:0 < n.succhlt:n < (wordPrefix n).lengthhget:(wordPrefix n).getD n 0 = (wordPrefix n)[n]⊢ (wordPrefix n)[n] ∈ wordPrefix n] n:ℕhn:0 < n.succhlt:n < (wordPrefix n).lengthhget:(wordPrefix n).getD n 0 = (wordPrefix n)[n]⊢ (wordPrefix n)[n] ∈ wordPrefix n
exact List.getElem_mem hlt All goals completed! 🐙@[category test, AMS 11 68]
theorem a_0 : a 0 = 0 := by ⊢ a 0 = 0 rfl All goals completed! 🐙@[category test, AMS 11 68]
theorem a_1 : a 1 = 1 := by ⊢ a 1 = 1 norm_num [a, wordPrefix] All goals completed! 🐙@[category test, AMS 11 68]
theorem a_2 : a 2 = 2 := by ⊢ a 2 = 2 norm_num [a, wordPrefix] All goals completed! 🐙@[category test, AMS 11 68]
theorem a_3 : a 3 = 2 := by ⊢ a 3 = 2 norm_num [a, wordPrefix] All goals completed! 🐙@[category test, AMS 11 68]
theorem a_4 : a 4 = 1 := by ⊢ a 4 = 1 norm_num [a, wordPrefix] All goals completed! 🐙@[category test, AMS 11 68]
theorem a_5 : a 5 = 1 := by ⊢ a 5 = 1 norm_num [a, wordPrefix] All goals completed! 🐙@[category test, AMS 11 68]
theorem a_6 : a 6 = 2 := by ⊢ a 6 = 2 norm_num [a, wordPrefix] All goals completed! 🐙@[category test, AMS 11 68]
theorem a_7 : a 7 = 1 := by ⊢ a 7 = 1 norm_num [a, wordPrefix] All goals completed! 🐙@[category test, AMS 11 68]
theorem a_8 : a 8 = 2 := by ⊢ a 8 = 2 norm_num [a, wordPrefix] All goals completed! 🐙@[category test, AMS 11 68]
theorem a_9 : a 9 = 2 := by ⊢ a 9 = 2 norm_num [a, wordPrefix] All goals completed! 🐙@[category test, AMS 11 68]
theorem a_10 : a 10 = 1 := by ⊢ a 10 = 1 norm_num [a, wordPrefix] All goals completed! 🐙"It is an unsolved problem to show that the density of 1's is equal to 1/2." — A000002
@[category research open, AMS 11 68]
theorem density_one : {n : ℕ | a n = 1}.HasDensity (1 / 2 : ℝ) := by ⊢ {n | a n = 1}.HasDensity (1 / 2)
sorry All goals completed! 🐙end OeisA2