{
  "id": "6596",
  "ident": "NS.06596",
  "slug": "oeis-5132",
  "title": "Recamán's sequence",
  "status": "open",
  "statement_status": "reviewed",
  "statement_md": "“I conjecture that every number eventually appears” — N. J. A. Sloane,\n[OEIS A005132](https://oeis.org/A005132).\n\nSloane later added that he was no longer sure every number appears. The coverage question remains\nopen and is formalized here as surjectivity onto the natural numbers.",
  "subjects": [
    "math.NT",
    "msc:11"
  ],
  "tags": [],
  "collection": "oeis",
  "source_url": null,
  "solved_on_platform": false,
  "needs_answer": false,
  "version": 1,
  "decl_name": "OeisA5132.conjecture",
  "statement_lean": "theorem conjecture : Function.Surjective a",
  "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-/\n\nmodule\n\npublic import FormalConjecturesUtil\n\n/-!\n# Recamán's sequence\n\nStart with $a(0)=0$. At step $n>0$, subtract $n$ from the preceding term when the result is\nnonnegative and has not appeared earlier; otherwise add $n$.\n\n*References:*\n- [OEIS A005132](https://oeis.org/A005132)\n-/\n\n@[expose] public section\n\nnamespace OeisA5132\n\n/--\nChoose the next Recamán value from a finite history and a step size: use the positive unseen\nsubtraction candidate when possible, and the addition candidate otherwise.\n-/\ndef nextTerm (previous : List ℕ) (step : ℕ) : ℕ :=\n  let current := previous.getLastD 0\n  if step < current ∧ current - step ∉ previous then current - step else current + step\n\n/-- The complete list $[a(0),\\ldots,a(n)]$ of Recamán values through index $n$. -/\ndef sequencePrefix : ℕ → List ℕ\n  | 0 => [0]\n  | n + 1 =>\n      let previous := sequencePrefix n\n      previous ++ [nextTerm previous (n + 1)]\n\n/-- The finite prefix through index $n$ has exactly $n+1$ entries. -/\n@[simp, category API, AMS 11]\ntheorem prefix_length : ∀ n, (sequencePrefix n).length = n + 1\n  | 0 => rfl\n  | n + 1 => by simp [sequencePrefix, prefix_length n]\n\n/-- Zero remains in every generated prefix. -/\n@[simp, category API, AMS 11]\ntheorem zero_mem_prefix : ∀ n, 0 ∈ sequencePrefix n\n  | 0 => by simp [sequencePrefix]\n  | n + 1 => by simp [sequencePrefix, zero_mem_prefix n]\n\n/-- Recamán's sequence value at index $n$, read from the end of its complete finite prefix. -/\ndef a (n : ℕ) : ℕ :=\n  (sequencePrefix n).getLastD 0\n\n/-- The OEIS nonnegative subtraction condition is equivalent to the strict-positive version used\nby `nextTerm`, because a zero candidate has already appeared in every prefix. -/\n@[category API, AMS 11]\ntheorem subtraction_condition_iff (n : ℕ) :\n    (n + 1 ≤ a n ∧ a n - (n + 1) ∉ sequencePrefix n) ↔\n      (n + 1 < a n ∧ a n - (n + 1) ∉ sequencePrefix n) := by\n  constructor\n  · rintro ⟨hle, hunseen⟩\n    refine ⟨?_, hunseen⟩\n    by_contra hlt\n    have hzero : a n - (n + 1) = 0 := by omega\n    apply hunseen\n    rw [hzero]\n    exact zero_mem_prefix n\n  · rintro ⟨hlt, hunseen⟩\n    exact ⟨by omega, hunseen⟩\n\n/-- The prefix at the next index is obtained by appending exactly one recurrence step. -/\n@[simp, category API, AMS 11]\ntheorem prefix_succ (n : ℕ) :\n    sequencePrefix (n + 1) =\n      sequencePrefix n ++ [nextTerm (sequencePrefix n) (n + 1)] := by\n  rfl\n\n/--\nThe public sequence satisfies the defining Recamán recurrence, including its positive and unseen\nsubtraction conditions.\n-/\n@[simp, category API, AMS 11]\ntheorem a_succ (n : ℕ) :\n    a (n + 1) =\n      if n + 1 < a n ∧ a n - (n + 1) ∉ sequencePrefix n then\n        a n - (n + 1)\n      else\n        a n + (n + 1) := by\n  simp [a, sequencePrefix, nextTerm]\n\n/-- The public recurrence in the exact nonnegative-subtraction grammar used by OEIS. -/\n@[category API, AMS 11]\ntheorem a_succ_oeis (n : ℕ) :\n    a (n + 1) =\n      if n + 1 ≤ a n ∧ a n - (n + 1) ∉ sequencePrefix n then\n        a n - (n + 1)\n      else\n        a n + (n + 1) := by\n  simpa only [subtraction_condition_iff] using a_succ n\n\n/-- The current sequence value occurs in the complete prefix through its index. -/\n@[category API, AMS 11]\ntheorem a_mem_prefix (n : ℕ) : a n ∈ sequencePrefix n := by\n  cases n with\n  | zero => simp [a, sequencePrefix]\n  | succ n => simp [a, sequencePrefix, nextTerm]\n\n@[category test, AMS 11]\ntheorem a_0 : a 0 = 0 := by decide\n\n@[category test, AMS 11]\ntheorem a_1 : a 1 = 1 := by decide\n\n@[category test, AMS 11]\ntheorem a_2 : a 2 = 3 := by decide\n\n@[category test, AMS 11]\ntheorem a_3 : a 3 = 6 := by decide\n\n@[category test, AMS 11]\ntheorem a_4 : a 4 = 2 := by decide\n\n@[category test, AMS 11]\ntheorem a_5 : a 5 = 7 := by decide\n\n@[category test, AMS 11]\ntheorem a_6 : a 6 = 13 := by decide\n\n/-- The value at index $20$ is $42$, which is repeated later. -/\n@[category test, AMS 11]\ntheorem a_20 : a 20 = 42 := by\n  set_option maxRecDepth 100000 in\n    decide\n\n/-- The value $42$ occurs again at index $24$. -/\n@[category test, AMS 11]\ntheorem a_24 : a 24 = 42 := by\n  set_option maxRecDepth 100000 in\n    decide\n\n/-- Recamán's sequence is not injective: its entries at indices $20$ and $24$ are both $42$. -/\n@[category test, AMS 11]\ntheorem not_injective : ¬ Function.Injective a := by\n  intro h\n  have : (20 : ℕ) = 24 := h (a_20.trans a_24.symm)\n  omega\n\n/--\n“I conjecture that every number eventually appears” — N. J. A. Sloane,\n[OEIS A005132](https://oeis.org/A005132).\n\nSloane later added that he was no longer sure every number appears. The coverage question remains\nopen and is formalized here as surjectivity onto the natural numbers.\n-/\n@[category research open, AMS 11]\ntheorem conjecture : Function.Surjective a := by\n  sorry\n\nend OeisA5132\n",
  "statement_hash": "dcf97280659c7aee193890964804d9862d6070b99bbb89a4151226bed1254279",
  "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."
}