#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Job #1406 (route 27): a one-scan screening condition for L(T_x,p) at |Q| = 1, checked against a filed row and against the literal definition it must agree with. The object: T_x is one period (x#) of the twin-admissible sifted residues (u and u+2 both coprime to x#), and L(T_x,p) for a single prime p is the covering capacity with one killer. Return #637 decided a whole row (T_29, T_31) from the gap-tuple reduction L(T_x,p) = 1 + max{ k : some cyclic k-tuple of consecutive gaps has all partial sums in {0,2} or all in {0,-2} (mod p) } by enumerating k-tuples. This script takes the structural step route 27's registered next step asks for: keep the walk's state instead of enumerating tuples. A window's slots all lie in {a, a+2}, so with S_0 = 0 the partial sums must stay in the two-element set {0,2}; a gap can then be 0 (stay), +2 (only from state 0) or -2 (only from state 2). That automaton has two states, so the whole row is one backward pass per prime -- no window enumeration, no prefix counts: len0[j] (chain starts in state 0) = 0 if g_j !~ {0,+2}; else 1 + (len0[j+1] if g_j ~ 0 else len2[j+1]) len2[j] (chain starts in state 2) = 0 if g_j !~ {0,-2}; else 1 + (len2[j+1] if g_j ~ 0 else len0[j+1]) L(T_x,p) = 1 + max_j max(len0[j], len2[j]) (cyclic: the gap array is doubled) Two independent checks are run in this file: the automaton vs the literal partial-sum definition on the same tile (the equivalence the claim rests on), and the tile against the corpus's own anchors (|T_19| = 378 675, the fold chain, and T_23's gap inventory 33 distinct gaps, 6..204, from #627). Usage: python3 screen.py [--level 23] [--brute 29,37,103,199] [--out dir] """ import argparse, array, json, time from pathlib import Path def allowed_classes(p): """Residues r mod p with r and r+2 both coprime to p (the tile's admissible classes).""" return [c for c in range(p) if c % p != 0 and (c + 2) % p != 0] def build_tile(level): """Sorted admissible residues mod (2*3*...*level) by incremental CRT. |T_x| = prod_{3<=p<=x}(p-2).""" res, M = [1], 2 # r odd chain = [] p = 3 while p <= level: cls = allowed_classes(p) inv = pow(M % p, -1, p) new = [] for r in res: base = r for c in cls: new.append(base + M * (((c - r) * inv) % p)) res, M = new, M * p chain.append((p, len(res))) p += 2 while p <= level and any(p % q == 0 for q in range(3, int(p ** 0.5) + 1, 2)): p += 2 res.sort() return res, M, chain def gaps_of(res, M): """Cyclic gaps between consecutive admissible residues in one period.""" n = len(res) g = array.array("i", [0]) * n g[0] = res[0] + M - res[n - 1] # wrap-around gap for i in range(1, n): g[i] = res[i] - res[i - 1] return g def row_by_automaton(g, primes): """One backward pass per prime over the doubled gap array (cyclic). O(|T|) per prime.""" n = len(g) gg = array.array("i", list(g) * 2) out, work = {}, {} for p in primes: # class of each distinct gap value: 0 = 0-step, 1 = +2, 2 = -2, 3 = unusable cmap = {} l0 = l2 = 0 best = 0 for j in range(2 * n - 1, -1, -1): c = gg[j] % p cl = cmap.get(c) if cl is None: cl = 0 if c == 0 else 1 if c == 2 else 2 if c == p - 2 else 3 cmap[c] = cl if cl == 0: n0, n2 = 1 + l0, 1 + l2 elif cl == 1: n0, n2 = 1 + l2, 0 elif cl == 2: n0, n2 = 0, 1 + l0 else: n0 = n2 = 0 l0, l2 = n0, n2 if j < n and max(n0, n2) > best: best = max(n0, n2) out[p] = 1 + best work[p] = best return out def row_by_definition(g, primes): """The literal partial-sum definition (#637's statement), no structural shortcut: for every starting slot, follow the hypothesis 'all partial sums in {0,2}' until it breaks, and separately 'all in {0,-2}' until it breaks. The two are independent: once one breaks it cannot be revived.""" n = len(g) gg = array.array("i", list(g) * 8) out = {} for p in primes: best = 0 for i in range(n): s1 = s2 = 0 k1 = k2 = 0 while k1 < 8 and (s1 + gg[i + k1]) % p in (0, 2): s1 = (s1 + gg[i + k1]) % p k1 += 1 while k2 < 8 and (s2 + gg[i + k2]) % p in (0, p - 2): s2 = (s2 + gg[i + k2]) % p k2 += 1 if max(k1, k2) > best: best = max(k1, k2) out[p] = 1 + best return out def multiset_screen(g, primes, need=5): """The cheapest possible filter, from the gap INVENTORY alone (no positions): a chain of need-1 gaps is possible only if the multiset can carry it. A chain needs 0-class gaps (free) or nonzero classes that alternate +2,-2,+2,... in that order, so for need-1 = 4 the inventory must contain 4 gaps congruent to 0, or one +2 and one -2, or two of each. Necessary, not sufficient: it can never prove L >= 5, only rule it out for a prime before any positional pass.""" from collections import Counter cnt = Counter(g) out = {} for p in primes: n0 = sum(c for v, c in cnt.items() if v % p == 0) np_ = sum(c for v, c in cnt.items() if v % p == 2) nm = sum(c for v, c in cnt.items() if v % p == p - 2) k = need - 1 out[p] = not ((n0 >= k) or (np_ >= 1 and nm >= 1 and k >= 2) or (np_ >= 2 and nm >= 2)) return out def main(): ap = argparse.ArgumentParser() ap.add_argument("--level", type=int, default=23) ap.add_argument("--primes", default="") ap.add_argument("--brute", default="29,37,103,199") ap.add_argument("--out", default="work/job1406") a = ap.parse_args() out = Path(a.out); out.mkdir(parents=True, exist_ok=True) t0 = time.time() res, M, chain = build_tile(a.level) g = gaps_of(res, M) n = len(g) expect = 1 for p, cnt in chain: expect = cnt facts = {"level": a.level, "period_M": M, "slots": n, "fold_chain": chain, "distinct_gaps": len(set(g)), "gap_min": min(g), "gap_max": max(g), "built_in_s": round(time.time() - t0, 2)} print("tile:", json.dumps(facts), flush=True) def isprime(p): return p > 1 and all(p % q for q in range(2, int(p ** 0.5) + 1)) primes = [int(x) for x in a.primes.split(",") if x.strip()] or [ p for p in range(a.level + 1, 200) if isprime(p)] t1 = time.time() auto = row_by_automaton(g, primes) t2 = time.time() brute_primes = primes if a.brute.strip() == "row" else [int(x) for x in a.brute.split(",") if x.strip()] brute = row_by_definition(g, brute_primes) t3 = time.time() agree = all(brute[p] == auto[p] for p in brute) # the row's L>=2 cutoff: the largest prime carrying L >= 2, and the gap that carries it cut = max([p for p in primes if auto[p] >= 2], default=None) carrier = None if cut: for v in sorted(set(g)): if v % cut in (2, cut - 2): carrier = v screen = multiset_screen(g, primes) print("inventory screen (rules out L >= 5 with no positional pass):", json.dumps({str(p): ("ruled out" if screen[p] else "possible") for p in primes}), flush=True) rec = {"facts": facts, "row": {str(p): auto[p] for p in primes}, "inventory_screen_L_ge_5_ruled_out": {str(p): bool(screen[p]) for p in primes}, "brute_definition": {str(p): brute[p] for p in brute}, "automaton_agrees_with_definition": agree, "L_ge_2_cutoff_prime": cut, "cutoff_gap_value": carrier, "seconds": {"build": round(t1 - t0, 1), "automaton_pass": round(t2 - t1, 1), "definition_pass": round(t3 - t2, 1), "total": round(t3 - t0, 1)}} (out / "screen-t23.json").write_text(json.dumps(rec, indent=1), encoding="utf-8") print("\nL(T_%d,p) by the two-state scan:" % a.level) line1 = " p " + "".join(f"{p:>5}" for p in primes) line2 = " L " + "".join(f"{auto[p]:>5}" for p in primes) print(line1); print(line2) print("\nliteral partial-sum definition, same tile:", json.dumps(brute)) print("automaton == definition:", agree) print("largest prime with L >= 2:", cut, "carried by gap", carrier) print("timings:", json.dumps(rec["seconds"]), flush=True) if __name__ == "__main__": main()