/- 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 /-! # The cup-of-coffee sequence The sequence starts with $1, 2$. Each later term is the least unused positive integer such that, for every prime, runs of consecutive multiples have length exactly two. The local rule requires prime factors that have just appeared to occur once more, and forbids prime factors that occurred in both of the preceding terms. *References:* - [A280864](https://oeis.org/A280864) -/ @[expose] public section namespace OeisA280864 /-- Prime factors whose run began at `previous` and must continue at the next term. -/ def requiredFactors (previousPrevious previous : ℕ) : Finset ℕ := previous.primeFactors \ previousPrevious.primeFactors /-- Prime factors whose run has length two and must end before the next term. -/ def forbiddenFactors (previousPrevious previous : ℕ) : Finset ℕ := previous.primeFactors ∩ previousPrevious.primeFactors /-- Whether `candidate` obeys the prime-factor run rule after the two preceding values. -/ def Eligible (previousPrevious previous candidate : ℕ) : Prop := 0 < candidate ∧ (∀ p ∈ requiredFactors previousPrevious previous, p ∣ candidate) ∧ (∀ p ∈ forbiddenFactors previousPrevious previous, ¬p ∣ candidate) @[category test, AMS 11] theorem not_eligible_zero : ¬Eligible 1 2 0 := by simp [Eligible] @[category test, AMS 11] theorem not_eligible_missing_required_factor : ¬Eligible 1 2 3 := by norm_num [Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] @[category test, AMS 11] theorem not_eligible_with_forbidden_factor : ¬Eligible 2 4 8 := by norm_num [Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] @[category test, AMS 11] theorem eligible_3_6_8 : Eligible 3 6 8 := by norm_num [Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] /-- The value `candidate` has not appeared before index `n`. -/ def UnusedBefore (a : ℕ → ℕ) (n candidate : ℕ) : Prop := ∀ i < n, a i ≠ candidate /-- At index `n`, the sequence takes the least unused eligible positive value. -/ def GreedyAt (a : ℕ → ℕ) (n : ℕ) : Prop := Eligible (a (n - 2)) (a (n - 1)) (a n) ∧ UnusedBefore a n (a n) ∧ ∀ candidate, Eligible (a (n - 2)) (a (n - 1)) candidate → UnusedBefore a n candidate → a n ≤ candidate /-- A total sequence obeys the initial conditions and every subsequent greedy step. -/ def FollowsRule (a : ℕ → ℕ) : Prop := a 0 = 1 ∧ a 1 = 2 ∧ ∀ n, 2 ≤ n → GreedyAt a n /-- A later value in a rule-following sequence differs from every earlier value. -/ @[category API, AMS 11] theorem ne_of_lt_of_followsRule {a : ℕ → ℕ} (ha : FollowsRule a) {i j : ℕ} (hij : i < j) : a i ≠ a j := by rcases ha with ⟨ha0, ha1, ha⟩ match j with | 0 => omega | 1 => have hi : i = 0 := by omega subst i simp [ha0, ha1] | j + 2 => exact (ha (j + 2) (by omega)).2.1 i hij /-- Unusedness at every greedy step makes every rule-following sequence injective. -/ @[category API, AMS 11] theorem followsRule_injective {a : ℕ → ℕ} (ha : FollowsRule a) : Function.Injective a := by intro i j hij by_contra hne rcases lt_or_gt_of_ne hne with hlt | hgt · exact ne_of_lt_of_followsRule ha hlt hij · exact ne_of_lt_of_followsRule ha hgt hij.symm /-- `candidate` is the least unused eligible value following the two displayed final values. -/ def LeastEligibleUnused (history : List ℕ) (previousPrevious previous candidate : ℕ) : Prop := Eligible previousPrevious previous candidate ∧ candidate ∉ history ∧ ∀ smaller ∈ Finset.Ico 1 candidate, ¬Eligible previousPrevious previous smaller ∨ smaller ∈ history /-- A finite list generated by the same greedy rule, without assuming an infinite continuation. -/ inductive ValidPrefix : List ℕ → Prop | nil : ValidPrefix [] | one : ValidPrefix [1] | two : ValidPrefix [1, 2] | append {history : List ℕ} {previousPrevious previous candidate : ℕ} (valid : ValidPrefix (history ++ [previousPrevious, previous])) (least : LeastEligibleUnused (history ++ [previousPrevious, previous]) previousPrevious previous candidate) : ValidPrefix (history ++ [previousPrevious, previous, candidate]) /-- A value occurs at the official OEIS index $n\geq 1$ in a finite valid prefix. The total sequences in `FollowsRule` use index $n-1$ for the same term. -/ def TermAt (n value : ℕ) : Prop := 0 < n ∧ ∃ xs, ValidPrefix xs ∧ xs[n - 1]? = some value /-- Index zero is outside the official OEIS domain. -/ @[category test, AMS 11] theorem not_term_at_zero (value : ℕ) : ¬TermAt 0 value := by simp [TermAt] private def initialPrefix : List ℕ := [1, 2, 4, 3, 6, 8, 5, 10, 12, 9] @[category API, AMS 11] private theorem initialPrefix_valid : ValidPrefix initialPrefix := by have h2 : ValidPrefix [1, 2] := ValidPrefix.two have h3 : ValidPrefix [1, 2, 4] := by simpa using ValidPrefix.append (history := []) (previousPrevious := 1) (previous := 2) (candidate := 4) h2 (by norm_num [LeastEligibleUnused, Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] intro smaller hsmall hlt interval_cases smaller <;> norm_num) have h4 : ValidPrefix [1, 2, 4, 3] := by simpa using ValidPrefix.append (history := [1]) h3 (by norm_num [LeastEligibleUnused, Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] intro smaller hsmall hlt interval_cases smaller <;> norm_num) have h5 : ValidPrefix [1, 2, 4, 3, 6] := by simpa using ValidPrefix.append (history := [1, 2]) h4 (by norm_num [LeastEligibleUnused, Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] intro smaller hsmall hlt interval_cases smaller <;> norm_num) have h6 : ValidPrefix [1, 2, 4, 3, 6, 8] := by simpa using ValidPrefix.append (history := [1, 2, 4]) h5 (by norm_num [LeastEligibleUnused, Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] intro smaller hsmall hlt interval_cases smaller <;> norm_num) have h7 : ValidPrefix [1, 2, 4, 3, 6, 8, 5] := by simpa using ValidPrefix.append (history := [1, 2, 4, 3]) h6 (by norm_num [LeastEligibleUnused, Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] intro smaller hsmall hlt interval_cases smaller <;> norm_num) have h8 : ValidPrefix [1, 2, 4, 3, 6, 8, 5, 10] := by simpa using ValidPrefix.append (history := [1, 2, 4, 3, 6]) h7 (by norm_num [LeastEligibleUnused, Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] intro smaller hsmall hlt interval_cases smaller <;> norm_num) have h9 : ValidPrefix [1, 2, 4, 3, 6, 8, 5, 10, 12] := by simpa using ValidPrefix.append (history := [1, 2, 4, 3, 6, 8]) h8 (by norm_num [LeastEligibleUnused, Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] intro smaller hsmall hlt interval_cases smaller <;> norm_num) have h10 : ValidPrefix initialPrefix := by simpa [initialPrefix] using ValidPrefix.append (history := [1, 2, 4, 3, 6, 8, 5]) h9 (by norm_num [LeastEligibleUnused, Eligible, requiredFactors, forbiddenFactors, Nat.primeFactors, Nat.primeFactorsList] intro smaller hsmall hlt interval_cases smaller <;> norm_num) exact h10 @[category test, AMS 11] theorem a_1 : TermAt 1 1 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ @[category test, AMS 11] theorem a_2 : TermAt 2 2 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ @[category test, AMS 11] theorem a_3 : TermAt 3 4 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ @[category test, AMS 11] theorem a_4 : TermAt 4 3 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ @[category test, AMS 11] theorem a_5 : TermAt 5 6 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ @[category test, AMS 11] theorem a_6 : TermAt 6 8 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ @[category test, AMS 11] theorem a_7 : TermAt 7 5 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ @[category test, AMS 11] theorem a_8 : TermAt 8 10 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ @[category test, AMS 11] theorem a_9 : TermAt 9 12 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ @[category test, AMS 11] theorem a_10 : TermAt 10 9 := by exact ⟨by decide, initialPrefix, initialPrefix_valid, by decide⟩ /-- Conjectured to be a permutation of the positive integers. -/ @[category research open, AMS 11] theorem conjecture : ∃ a : ℕ → ℕ, FollowsRule a ∧ ∀ k > 0, ∃ n, a n = k := by sorry end OeisA280864