/- 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 FormalConjecturesUtil

Keith numbers

Defined by Mike Keith (1987) as repfigit numbers, an integer $N \ge b$ written in base $b \ge 2$ using $k \ge 2$ digits $d_0, \dots, d_{k-1}$ (most-significant digit first) is a base-$b$ Keith integer when $N$ appears in the $k$-step Fibonacci-style recurrence initialized by $a_i = d_i$ ($0 \le i < k$) and $a_m = \sum_{j=1}^k a_{m-j}$ ($m \ge k$). Base $2$ admits infinitely many Keith integers (including $2^r$ for every $r \ge 1$). In base $10$ (OEIS A007629), Martin Klazar and Florian Luca (2007, Theorem 1.2) proved the counting bound $#\mathcal{K}(x) \ll x / \sqrt{\log x}$ for $x \ge 2$ (which extends to every fixed base $b \ge 4$ and shows natural density zero, though it does not establish convergence of the reciprocal sum); whether infinitely many base-$10$ Keith integers exist remains open.

References:

@[expose] public sectionnamespace KeithNumber

Advances the sliding $k$-term recurrence window by dropping its oldest entry and appending the window sum.

def keithWindowStep (w : List ℕ) : List ℕ := w.tail ++ [w.sum]

Predicate asserting that $N \ge b$ in base $b \ge 2$ appears in the $k$-step recurrence seeded by its base-$b$ digits (most-significant digit first).

def IsKeithNumberBase (b N : ℕ) : Prop := 2 ≤ b ∧ b ≤ N ∧ ∃ m : ℕ, 1 ≤ m ∧ ((keithWindowStep^[m]) (Nat.digits b N).reverse).getLast? = some N

Base-$10$ Keith integers (OEIS A007629), requiring $N \ge 10$ to exclude single-digit integers.

abbrev IsKeithNumber : ℕ → Prop := IsKeithNumberBase 10

keithWindowStep preserves the length of every nonempty window.

@[category API, AMS 11] theorem keithWindowStep_length {w : List ℕ} (hw : w ≠ []) : (keithWindowStep w).length = w.length := w:List ℕhw:w ≠ []⊢ (keithWindowStep w).length = w.length hw:[] ≠ []⊢ (keithWindowStep []).length = [].lengthhead✝:ℕtail✝:List ℕhw:head✝ :: tail✝ ≠ []⊢ (keithWindowStep (head✝ :: tail✝)).length = (head✝ :: tail✝).length hw:[] ≠ []⊢ (keithWindowStep []).length = [].length All goals completed! 🐙 head✝:ℕtail✝:List ℕhw:head✝ :: tail✝ ≠ []⊢ (keithWindowStep (head✝ :: tail✝)).length = (head✝ :: tail✝).length All goals completed! 🐙

Once a two-element window [a, b] has 1 ≤ a and N < b, no later iterate ends at N.

a:ℕb:ℕN:ℕha:1 ≤ ahb:N < bhiter:∀ (j : ℕ), ∃ x y, 1 ≤ x ∧ N < y ∧ keithWindowStep^[j] [a, b] = [x, y]j:ℕw✝:ℕy:ℕleft✝:1 ≤ w✝hy:N < yheq:keithWindowStep^[j] [a, b] = [w✝, y]⊢ [w✝, y].getLast? ≠ some N All goals completed! 🐙

$14$ is the smallest base-$10$ Keith integer.

@[category test, AMS 11] theorem isKeithNumber_14 : IsKeithNumber 14 := ⟨⊢ 2 ≤ 10 All goals completed! 🐙, ⊢ 10 ≤ 14 All goals completed! 🐙, 3, ⊢ 1 ≤ 3 All goals completed! 🐙, ⊢ (keithWindowStep^[3] (Nat.digits 10 14).reverse).getLast? = some 14 All goals completed! 🐙⟩

$19$ is a base-$10$ Keith integer.

@[category test, AMS 11] theorem isKeithNumber_19 : IsKeithNumber 19 := ⟨⊢ 2 ≤ 10 All goals completed! 🐙, ⊢ 10 ≤ 19 All goals completed! 🐙, 2, ⊢ 1 ≤ 2 All goals completed! 🐙, ⊢ (keithWindowStep^[2] (Nat.digits 10 19).reverse).getLast? = some 19 All goals completed! 🐙⟩

$28$ is a base-$10$ Keith integer.

@[category test, AMS 11] theorem isKeithNumber_28 : IsKeithNumber 28 := ⟨⊢ 2 ≤ 10 All goals completed! 🐙, ⊢ 10 ≤ 28 All goals completed! 🐙, 3, ⊢ 1 ≤ 3 All goals completed! 🐙, ⊢ (keithWindowStep^[3] (Nat.digits 10 28).reverse).getLast? = some 28 All goals completed! 🐙⟩

$47$ is a base-$10$ Keith integer.

@[category test, AMS 11] theorem isKeithNumber_47 : IsKeithNumber 47 := ⟨⊢ 2 ≤ 10 All goals completed! 🐙, ⊢ 10 ≤ 47 All goals completed! 🐙, 4, ⊢ 1 ≤ 4 All goals completed! 🐙, ⊢ (keithWindowStep^[4] (Nat.digits 10 47).reverse).getLast? = some 47 All goals completed! 🐙⟩

$197$ is a three-digit base-$10$ Keith integer.

@[category test, AMS 11] theorem isKeithNumber_197 : IsKeithNumber 197 := ⟨⊢ 2 ≤ 10 All goals completed! 🐙, ⊢ 10 ≤ 197 All goals completed! 🐙, 5, ⊢ 1 ≤ 5 All goals completed! 🐙, ⊢ (keithWindowStep^[5] (Nat.digits 10 197).reverse).getLast? = some 197 All goals completed! 🐙⟩

$88$ is a base-$6$ Keith integer ($88 = (224)_6$).

@[category test, AMS 11] theorem isKeithNumberBase_6_88 : IsKeithNumberBase 6 88 := ⟨⊢ 2 ≤ 6 All goals completed! 🐙, ⊢ 6 ≤ 88 All goals completed! 🐙, 5, ⊢ 1 ≤ 5 All goals completed! 🐙, ⊢ (keithWindowStep^[5] (Nat.digits 6 88).reverse).getLast? = some 88 All goals completed! 🐙⟩

Single-digit integers $n < 10$ are excluded from IsKeithNumber.

@[category test, AMS 11] theorem not_isKeithNumber_of_lt_ten {n : ℕ} (hn : n < 10) : ¬ IsKeithNumber n := fun h ↦ absurd h.2.1 (n:ℕhn:n < 10h:IsKeithNumber n⊢ ¬10 ≤ n All goals completed! 🐙)

$10$ is not a base-$10$ Keith integer.

left✝¹:2 ≤ 10left✝:10 ≤ 10m:ℕhm1:1 ≤ mhlast:(keithWindowStep^[m - 7 + 0] (keithWindowStep^[7] [1, 0])).getLast? = some 10hm7:¬m < 7⊢ False exact two_elem_window_overshoot (a := 8) (b := 13) (left✝¹:2 ≤ 10left✝:10 ≤ 10m:ℕhm1:1 ≤ mhlast:(keithWindowStep^[m - 7 + 0] (keithWindowStep^[7] [1, 0])).getLast? = some 10hm7:¬m < 7⊢ 1 ≤ 8 All goals completed! 🐙) (left✝¹:2 ≤ 10left✝:10 ≤ 10m:ℕhm1:1 ≤ mhlast:(keithWindowStep^[m - 7 + 0] (keithWindowStep^[7] [1, 0])).getLast? = some 10hm7:¬m < 7⊢ 10 < 13 All goals completed! 🐙) (m - 7) hlast

$15$ is not a base-$10$ Keith integer.

left✝¹:2 ≤ 10left✝:10 ≤ 15m:ℕhm1:1 ≤ mhlast:(keithWindowStep^[m - 3 + 0] (keithWindowStep^[3] [1, 5])).getLast? = some 15hm3:¬m < 3⊢ False exact two_elem_window_overshoot (a := 11) (b := 17) (left✝¹:2 ≤ 10left✝:10 ≤ 15m:ℕhm1:1 ≤ mhlast:(keithWindowStep^[m - 3 + 0] (keithWindowStep^[3] [1, 5])).getLast? = some 15hm3:¬m < 3⊢ 1 ≤ 11 All goals completed! 🐙) (left✝¹:2 ≤ 10left✝:10 ≤ 15m:ℕhm1:1 ≤ mhlast:(keithWindowStep^[m - 3 + 0] (keithWindowStep^[3] [1, 5])).getLast? = some 15hm3:¬m < 3⊢ 15 < 17 All goals completed! 🐙) (m - 3) hlast

Infinitely many base-$2$ Keith integers exist (for example $2^r$ for every $r \ge 1$).

hsum:∀ (j : ℕ), 1 + (List.map (fun x ↦ 2 ^ x) (List.range j)).sum = 2 ^ jhdig:∀ (k : ℕ), (Nat.digits 2 (2 ^ k)).reverse = 1 :: List.replicate k 0r:ℕk:ℕ := r + 1hiter:∀ j ≤ k, keithWindowStep^[j] (List.replicate k 0 ++ [1]) = List.replicate (k - j) 0 ++ 1 :: List.map (fun x ↦ 2 ^ x) (List.range j)hstep1:keithWindowStep (1 :: List.replicate k 0) = List.replicate k 0 ++ [1]⊢ (keithWindowStep (List.replicate (k - k) 0 ++ 1 :: List.map (fun x ↦ 2 ^ x) (List.range k))).getLast? = some (2 ^ k) All goals completed! 🐙

Klazar–Luca counting bound (2007, Theorem 1.2): there exists a constant $C > 0$ such that for every integer $x \ge 2$, the number of base-$10$ Keith integers at most $x$ satisfies $$#{N \le x : N \text{ is a Keith number}} \le \frac{C x}{\sqrt{\log x}}.$$

@[category research solved, AMS 11] theorem klazar_luca_keith_counting_bound : ∃ C : ℝ, 0 < C ∧ ∀ x : ℕ, 2 ≤ x → ({N : ℕ | N ≤ x ∧ IsKeithNumber N}.ncard : ℝ) ≤ C * ((x : ℝ) / Real.sqrt (Real.log (x : ℝ))) := ⊢ ∃ C, 0 < C ∧ ∀ (x : ℕ), 2 ≤ x → ↑{N | N ≤ x ∧ IsKeithNumber N}.ncard ≤ C * (↑x / √(Real.log ↑x)) All goals completed! 🐙

Do infinitely many base-$10$ Keith integers exist (OEIS A007629)?

@[category research open, question, AMS 11] theorem keith_numbers_infinite : {N : ℕ | IsKeithNumber N}.Infinite := ⊢ {N | IsKeithNumber N}.Infinite All goals completed! 🐙end KeithNumber