{
  "id": "8079",
  "ident": "NS.08079",
  "slug": "wikipedia-keithnumber",
  "title": "Keith numbers",
  "status": "solved",
  "statement_status": "reviewed",
  "statement_md": "Klazar–Luca counting bound (2007, Theorem 1.2): there exists a constant $C > 0$ such that for every\ninteger $x \\ge 2$, the number of base-$10$ Keith integers at most $x$ satisfies\n$$\\#\\{N \\le x : N \\text{ is a Keith number}\\} \\le \\frac{C x}{\\sqrt{\\log x}}.$$",
  "subjects": [
    "math.NT",
    "msc:11"
  ],
  "tags": [],
  "collection": "wikipedia",
  "source_url": null,
  "solved_on_platform": false,
  "needs_answer": false,
  "version": 1,
  "decl_name": "KeithNumber.klazar_luca_keith_counting_bound",
  "statement_lean": "theorem klazar_luca_keith_counting_bound :\n    ∃ C : ℝ, 0 < C ∧ ∀ x : ℕ, 2 ≤ x →\n      ({N : ℕ | N ≤ x ∧ IsKeithNumber N}.ncard : ℝ) ≤\n        C * ((x : ℝ) / Real.sqrt (Real.log (x : ℝ)))",
  "challenge_source": "/-\nCopyright 2026 The Formal Conjectures Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    https://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n-/\nmodule\n\npublic import FormalConjecturesUtil\n\n/-!\n# Keith numbers\n\nDefined by Mike Keith (1987) as *repfigit* numbers, an integer $N \\ge b$ written in base $b \\ge 2$\nusing $k \\ge 2$ digits $d_0, \\dots, d_{k-1}$ (most-significant digit first) is a base-$b$ Keith\ninteger when $N$ appears in the $k$-step Fibonacci-style recurrence initialized by $a_i = d_i$\n($0 \\le i < k$) and $a_m = \\sum_{j=1}^k a_{m-j}$ ($m \\ge k$). Base $2$ admits infinitely many Keith\nintegers (including $2^r$ for every $r \\ge 1$). In base $10$ (`OEIS A007629`), Martin Klazar and\nFlorian Luca (2007, Theorem 1.2) proved the counting bound $\\#\\mathcal{K}(x) \\ll x / \\sqrt{\\log x}$\nfor $x \\ge 2$ (which extends to every fixed base $b \\ge 4$ and shows natural density zero, though it\ndoes not establish convergence of the reciprocal sum); whether infinitely many base-$10$ Keith\nintegers exist remains open.\n\n*References:*\n- [Wikipedia: Keith number](https://en.wikipedia.org/wiki/Keith_number)\n- [OEIS A007629](https://oeis.org/A007629)\n- M. Keith (1987), J. Recreational Math. **19**, no. 1, 41–42.\n- M. Klazar and F. Luca (2007), J. Integer Seq. **10**, Article 07.2.2.\n-/\n\n@[expose] public section\n\nnamespace KeithNumber\n\n/-- Advances the sliding $k$-term recurrence window by dropping its oldest entry and appending the\nwindow sum. -/\ndef keithWindowStep (w : List ℕ) : List ℕ :=\n  w.tail ++ [w.sum]\n\n/-- Predicate asserting that $N \\ge b$ in base $b \\ge 2$ appears in the $k$-step recurrence seeded\nby its base-$b$ digits (most-significant digit first). -/\ndef IsKeithNumberBase (b N : ℕ) : Prop :=\n  2 ≤ b ∧ b ≤ N ∧\n    ∃ m : ℕ, 1 ≤ m ∧ ((keithWindowStep^[m]) (Nat.digits b N).reverse).getLast? = some N\n\n/-- Base-$10$ Keith integers (`OEIS A007629`), requiring $N \\ge 10$ to exclude single-digit\nintegers. -/\nabbrev IsKeithNumber : ℕ → Prop := IsKeithNumberBase 10\n\n/-- `keithWindowStep` preserves the length of every nonempty window. -/\n@[category API, AMS 11]\ntheorem keithWindowStep_length {w : List ℕ} (hw : w ≠ []) :\n    (keithWindowStep w).length = w.length := by\n  rcases w with _ | ⟨_, _⟩\n  · exact False.elim (hw rfl)\n  · simp [keithWindowStep]\n\n/-- Once a two-element window `[a, b]` has `1 ≤ a` and `N < b`, no later iterate ends at `N`. -/\n@[category API, AMS 11]\ntheorem two_elem_window_overshoot {a b N : ℕ} (ha : 1 ≤ a) (hb : N < b) :\n    ∀ j : ℕ, ((keithWindowStep^[j]) [a, b]).getLast? ≠ some N := by\n  have hiter : ∀ j : ℕ, ∃ x y : ℕ, 1 ≤ x ∧ N < y ∧ (keithWindowStep^[j]) [a, b] = [x, y] := by\n    intro j\n    induction j with\n    | zero => exact ⟨a, b, ha, hb, rfl⟩\n    | succ j ih =>\n      obtain ⟨x, y, hx, hy, heq⟩ := ih\n      refine ⟨y, x + y, by omega, by omega, ?_⟩\n      rw [Function.iterate_succ_apply', heq]\n      rfl\n  intro j\n  obtain ⟨_, y, _, hy, heq⟩ := hiter j\n  rw [heq]\n  simp [ne_of_gt hy]\n\n/-- $14$ is the smallest base-$10$ Keith integer. -/\n@[category test, AMS 11]\ntheorem isKeithNumber_14 : IsKeithNumber 14 :=\n  ⟨by omega, by omega, 3, by omega, by decide⟩\n\n/-- $19$ is a base-$10$ Keith integer. -/\n@[category test, AMS 11]\ntheorem isKeithNumber_19 : IsKeithNumber 19 :=\n  ⟨by omega, by omega, 2, by omega, by decide⟩\n\n/-- $28$ is a base-$10$ Keith integer. -/\n@[category test, AMS 11]\ntheorem isKeithNumber_28 : IsKeithNumber 28 :=\n  ⟨by omega, by omega, 3, by omega, by decide⟩\n\n/-- $47$ is a base-$10$ Keith integer. -/\n@[category test, AMS 11]\ntheorem isKeithNumber_47 : IsKeithNumber 47 :=\n  ⟨by omega, by omega, 4, by omega, by decide⟩\n\n/-- $197$ is a three-digit base-$10$ Keith integer. -/\n@[category test, AMS 11]\ntheorem isKeithNumber_197 : IsKeithNumber 197 :=\n  ⟨by omega, by omega, 5, by omega, by decide⟩\n\n/-- $88$ is a base-$6$ Keith integer ($88 = (224)_6$). -/\n@[category test, AMS 11]\ntheorem isKeithNumberBase_6_88 : IsKeithNumberBase 6 88 :=\n  ⟨by omega, by omega, 5, by omega, by decide⟩\n\n/-- Single-digit integers $n < 10$ are excluded from `IsKeithNumber`. -/\n@[category test, AMS 11]\ntheorem not_isKeithNumber_of_lt_ten {n : ℕ} (hn : n < 10) : ¬ IsKeithNumber n :=\n  fun h ↦ absurd h.2.1 (by omega)\n\n/-- $10$ is not a base-$10$ Keith integer. -/\n@[category test, AMS 11]\ntheorem not_isKeithNumber_10 : ¬ IsKeithNumber 10 := by\n  rintro ⟨_, _, m, hm1, hlast⟩\n  rw [show (Nat.digits 10 10).reverse = [1, 0] by norm_num] at hlast\n  by_cases hm7 : m < 7\n  · interval_cases m <;> revert hlast <;> decide\n  · rw [show m = (m - 7) + 0 + 7 by omega, Function.iterate_add_apply] at hlast\n    exact two_elem_window_overshoot (a := 8) (b := 13) (by omega) (by omega) (m - 7) hlast\n\n/-- $15$ is not a base-$10$ Keith integer. -/\n@[category test, AMS 11]\ntheorem not_isKeithNumber_15 : ¬ IsKeithNumber 15 := by\n  rintro ⟨_, _, m, hm1, hlast⟩\n  rw [show (Nat.digits 10 15).reverse = [1, 5] by norm_num] at hlast\n  by_cases hm3 : m < 3\n  · interval_cases m <;> revert hlast <;> decide\n  · rw [show m = (m - 3) + 0 + 3 by omega, Function.iterate_add_apply] at hlast\n    exact two_elem_window_overshoot (a := 11) (b := 17) (by omega) (by omega) (m - 3) hlast\n\n/-- Infinitely many base-$2$ Keith integers exist (for example $2^r$ for every $r \\ge 1$). -/\n@[category textbook, AMS 11]\ntheorem keith_numbers_base_two_infinite :\n    {N : ℕ | IsKeithNumberBase 2 N}.Infinite := by\n  have hsum : ∀ j : ℕ, 1 + (List.map (2 ^ ·) (List.range j)).sum = 2 ^ j := fun j ↦ by\n    induction j with\n    | zero => simp\n    | succ j ih => rw [List.range_succ]; simp; omega\n  have hdig : ∀ k : ℕ, (Nat.digits 2 (2 ^ k)).reverse = 1 :: List.replicate k 0 := fun k ↦ by\n    induction k with\n    | zero => decide\n    | succ k ih =>\n      rw [Nat.digits_def' (by omega) (by positivity), pow_succ', Nat.mul_mod_right,\n        Nat.mul_div_cancel_left _ (by omega), List.reverse_cons, ih, List.cons_append,\n        ← List.replicate_succ']\n  refine Set.infinite_of_injective_forall_mem (f := fun r : ℕ ↦ 2 ^ (r + 1))\n    (fun a b h ↦ by simpa using Nat.pow_right_injective (by omega) h) fun r ↦ ?_\n  set k := r + 1\n  have hiter : ∀ j ≤ k, (keithWindowStep^[j]) (List.replicate k 0 ++ [1]) =\n      List.replicate (k - j) 0 ++ 1 :: List.map (2 ^ ·) (List.range j) := fun j hj ↦ by\n    induction j with\n    | zero => simp\n    | succ j ih =>\n      rw [Function.iterate_succ_apply', ih (by omega), show k - j = k - (j + 1) + 1 by omega,\n        List.replicate_succ, List.range_succ]\n      simp [keithWindowStep, ← hsum j]\n  refine ⟨le_rfl, Nat.le_self_pow (by omega) 2, k + 2, by omega, ?_⟩\n  rw [hdig, show k + 2 = (k + 1) + 1 by omega, Function.iterate_succ_apply',\n    Function.iterate_succ_apply]\n  have hstep1 : keithWindowStep (1 :: List.replicate k 0) = List.replicate k 0 ++ [1] := by\n    simp [keithWindowStep]\n  rw [hstep1, hiter k le_rfl]\n  simp [keithWindowStep, hsum k]\n\n/--\nKlazar–Luca counting bound (2007, Theorem 1.2): there exists a constant $C > 0$ such that for every\ninteger $x \\ge 2$, the number of base-$10$ Keith integers at most $x$ satisfies\n$$\\#\\{N \\le x : N \\text{ is a Keith number}\\} \\le \\frac{C x}{\\sqrt{\\log x}}.$$\n-/\n@[category research solved, AMS 11]\ntheorem klazar_luca_keith_counting_bound :\n    ∃ C : ℝ, 0 < C ∧ ∀ x : ℕ, 2 ≤ x →\n      ({N : ℕ | N ≤ x ∧ IsKeithNumber N}.ncard : ℝ) ≤\n        C * ((x : ℝ) / Real.sqrt (Real.log (x : ℝ))) := by\n  sorry\n\n/--\nDo infinitely many base-$10$ Keith integers exist ([OEIS A007629](https://oeis.org/A007629))?\n-/\n@[category research open, question, AMS 11]\ntheorem keith_numbers_infinite :\n    {N : ℕ | IsKeithNumber N}.Infinite := by\n  sorry\n\nend KeithNumber\n",
  "statement_hash": "1cfe84eeacfcbf914d4f3b19685de12d1dee4e7ab7bbf20b83778dc182d8b6e2",
  "toolchain": "leanprover/lean4:v4.33.1",
  "upstream_commit": "f9b3868d5ca994a7bf95039bcec05338c0c21e2f",
  "workspace": "formal-conjectures",
  "field": "math",
  "formalization": "formal",
  "verification": "lean-kernel",
  "license": "Apache-2.0",
  "attribution": "google-deepmind/formal-conjectures contributors",
  "metric_name": null,
  "metric_direction": null,
  "critiques": [],
  "public_partial_proofs": [],
  "research_threads": [],
  "instructions": "Read research_threads first: other people's public repositories and what they already tried. Write Solution.lean starting from challenge_source. Keep every definition identical, prove decl_name with exactly the same type, no sorry in its dependency closure, no new axioms, no native_decide, no metaprogramming. Submit with POST /api/v1/submissions."
}