#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Job #1677 ("Leads: new statistic"): the TWO-PRIME adjacent-kill statistic on T23. QUESTION (pre-registered in work/PRE-REGISTRATION-1677.md BEFORE this file was run): The served per-prime column L(T_x,p) and every published covering family fix ONE prime with one class pair, or take a maximum over the WHOLE prime set. Does a PAIR of primes cover a longer adjacent window than either prime alone -- and in particular can two primes that are each individually inert (no gap = 0, +-2 mod p) already buy a two-slot window? Falsifier (fixed before the run): for every prime pair p= 2 IFF L(T,p) >= 2 or L(T,q) >= 2. Refuted iff some both-inert pair has two adjacent covered slots. Sharper claim: Lambda_or(p,q) = max(L(p),L(q)) for every pair. Objects are all served, nothing is redefined: * tile builder and the single-prime automaton are job #1406 / return #640's reviewed code, imported read-only from that run's directory (reuse, not a rebuild); * the kill-step classes Z/M/P/X are N-1644's reconstruction of the served edge rule (sigma' - sigma = g_i mod p); * anchors checked: served gap inventory of T23 (#627, #1644), L(T19,p=23)=3 row and the L(T23,p) = 2,3,2,2 column at p = 29,31,37,41 (#1634). Deterministic, offline, no network. Run via sah.py exec with stdout redirected to a log file. """ import array import json import pathlib import random import sys import time import numpy as np PRIOR = (str(pathlib.Path(__file__).resolve().parents[3] / "run_20260916_111913_U5Xr-Q" / "work" / "job1406")) sys.path.insert(0, PRIOR) import screen # noqa: E402 (reviewed tile builder + single-prime automaton, job #1406) LEVEL = 23 PRIME_CAP = 211 # two primes past G2 = 204, where N-1644's bound forces L = 1 SHUFFLES = 20 SEED = 1677 def primes_upto(n): out = [] for c in range(3, n + 1, 2): if all(c % d for d in range(3, int(c ** 0.5) + 1, 2)): out.append(c) return out def cls(v, p): r = v % p if r == 0: return "Z" if r == 2: return "P" if r == p - 2: return "M" return "X" def support_values(vals, p): return {v for v in vals if cls(v, p) != "X"} def max_covered_run(gvals, mask): """Longest cyclic run of consecutive True slots in mask (0 if none).""" idx = np.flatnonzero(~mask) D = mask.size if idx.size == 0: return D runs = np.diff(np.append(idx, idx[0] + D)) - 1 return int(runs.max()) def pair_or_run(gvals, p, q): """Longest cyclic window all of whose gaps are covered by p OR by q (support level).""" m = np.fromiter((cls(v, p) != "X" or cls(v, q) != "X" for v in gvals), dtype=bool, count=gvals.size) return max_covered_run(gvals, m) def pair_and_run(gvals, p, q): """Longest window under the PRODUCT automaton: both primes' sigma must stay alive on every gap. A gap step is legal for p iff cls != 'X', and the state (s_p, s_q) advances independently: Z keeps, M forces 0 -> p-2, P forces p-2 -> 0. The joint run is therefore the longest window in which every gap is a legal step for p AND for q. """ m = np.fromiter((cls(v, p) != "X" and cls(v, q) != "X" for v in gvals), dtype=bool, count=gvals.size) return max_covered_run(gvals, m) def main(): t0 = time.time() facts = {} res, M, chain = screen.build_tile(LEVEL) D = len(res) g = np.array(screen.gaps_of(res, M), dtype=np.int64) vals, counts = np.unique(g, return_counts=True) G2 = int(vals.max()) facts["level"] = LEVEL facts["period_M"] = M facts["D"] = D facts["distinct_gaps"] = int(vals.size) facts["gap_min"] = int(vals.min()) facts["gap_max"] = G2 facts["build_s"] = round(time.time() - t0, 2) # --- served anchors ------------------------------------------------------------------- anchors = {} anchors["D_matches_served_T23"] = D == 7_952_175 anchors["gap_inventory_matches_627"] = (int(vals.size) == 33 and int(vals.min()) == 6 and G2 == 204) L19_res, L19_M, _ = screen.build_tile(19) L19_g = screen.gaps_of(L19_res, L19_M) anchors["T19_p23_row"] = screen.row_by_automaton(L19_g, [23])[23] == 3 # --- single-prime column at T23 -------------------------------------------------------- plist = [p for p in primes_upto(PRIME_CAP) if p >= 23] L1 = screen.row_by_automaton(g, plist) anchors["served_L_T23_p29_31_37_41"] = [L1[p] for p in (29, 31, 37, 41)] == [2, 3, 2, 2] anchors["bound_L_is_1_for_p_gt_G2"] = all(L1[p] == 1 for p in plist if p > G2) # --- ordered adjacent-pair counts (one pass) ------------------------------------------ u = np.searchsorted(vals, g) K = vals.size nxt = np.roll(u, -1) A = np.bincount(u * K + nxt, minlength=K * K).reshape(K, K) facts["adjacent_pairs_total"] = int(A.sum()) facts["adjacent_same_type_pairs"] = int(np.trace(A)) sup = {p: support_values(list(vals), p) for p in plist} facts["support_primes"] = sorted(p for p in plist if sup[p]) facts["inert_primes_count"] = sum(1 for p in plist if not sup[p]) # --- order-free pair support (the pre-registered falsifier) --------------------------- def adjacent_covered(p, q): cov = sup[p] | sup[q] if not cov: return False for i, a in enumerate(vals): if a in cov and any(b in cov for b in vals[A[i] > 0]): return True return False pairs = [(plist[i], plist[j]) for i in range(len(plist)) for j in range(i + 1, len(plist))] both_inert_gain = [] one_supported_gain = [] filter_pass = [] for p, q in pairs: if not adjacent_covered(p, q): continue filter_pass.append((p, q)) if L1[p] == 1 and L1[q] == 1: both_inert_gain.append((p, q)) else: one_supported_gain.append((p, q)) facts["pairs_total"] = len(pairs) facts["pairs_passing_adjacency_filter"] = len(filter_pass) facts["both_inert_pairs_passing"] = both_inert_gain facts["one_supported_pairs_passing"] = one_supported_gain # --- exact runs for the pairs that pass (few) ----------------------------------------- exact = [] for p, q in filter_pass: lo = pair_or_run(g, p, q) la = pair_and_run(g, p, q) exact.append({"p": p, "q": q, "L_p": L1[p], "L_q": L1[q], "Lambda_or": lo, "Lambda_and": la, "gain_over_max": lo - max(L1[p], L1[q])}) facts["exact_pairs"] = exact # --- falsifier verdicts --------------------------------------------------------------- facts["falsifier_pair_additivity_refuted"] = bool(both_inert_gain) facts["falsifier_no_gain_refuted"] = any(e["gain_over_max"] > 0 for e in exact) # --- control 1: cyclic rotations must be exactly invariant ---------------------------- rot_ok = True for r in (1, 12345, D // 3): gr = np.roll(g, r) ur = np.searchsorted(vals, gr) Ar = np.bincount(ur * K + np.roll(ur, -1), minlength=K * K).reshape(K, K) if not np.array_equal(Ar, A): rot_ok = False if gr.max() != G2 or np.array_equal(np.unique(gr), vals) is False: rot_ok = False facts["control_rotation_invariance"] = bool(rot_ok) # --- control 2: random permutation of the same multiset (adjacency destroyed) --------- rng = random.Random(SEED) perm_counts = [] for _ in range(SHUFFLES): gp = np.array(rng.sample(list(g.tolist()), D), dtype=np.int64) up = np.searchsorted(vals, gp) Ap = np.bincount(up * K + np.roll(up, -1), minlength=K * K).reshape(K, K) cnt = 0 for p, q in pairs: cov = sup[p] | sup[q] if not cov: continue hit = False for i, a in enumerate(vals): if a in cov and any(b in cov for b in vals[Ap[i] > 0]): hit = True break if hit: cnt += 1 perm_counts.append(cnt) facts["control_permutation_supporting_pair_counts"] = perm_counts facts["control_permutation_mean"] = round(float(np.mean(perm_counts)), 2) facts["control_permutation_sd"] = round(float(np.std(perm_counts)), 2) facts["wall_s"] = round(time.time() - t0, 2) facts["all_pass"] = all(anchors.values()) and rot_ok out = {"facts": facts, "anchors": anchors} print(json.dumps(out, indent=1, sort_keys=True)) return 0 if out["facts"]["all_pass"] else 1 if __name__ == "__main__": sys.exit(main())