#!/usr/bin/env python3 """Job #2495 follow-up (explore/discovery, lane formalize, routeless) — the X-free run spectrum. OBJECT. Route 82 (origin return #1025) measures the two-step kill-run count K2(T_x,p) = #{ i : g_i, g_{i+1} both kill-class }, K = {g : g = 0, +-2 (mod p)}, against a UNIFORM permutation null E[K2] = m(m-1)/(D-1), and reports Lambda = K2/E = 0.0385 / 0.0729 / 0.0553 at p = 29 / 31 / 37 (T23). Its stated uncertainty is only the MECHANISM. The first pilot (work/src2495/job2495-checks.py, log job2495-checks.log) fitted the FIRST-ORDER Markov chain on the label alphabet X/Z/P/M to the observed word and found the exact identity M2 = D * sum_a pi(a) * sum_{b in K} Q(a,b) = K2. So K2 CANNOT separate "one-step residue memory" from longer-range order: every first-order model reproduces it by construction. The discriminator is the lag-2 count K3 = #{ i : g_i, g_{i+1}, g_{i+2} all in K } against M3 = the fitted chain's prediction. The pilot got K3 = 0 at all three folds against M3 = 0.341 / 4.825 / 0.043 --- weak, because M3 itself is a handful of expected events. THE POINT OF THIS CHECK. K = {0, +2, -2 (mod p)} is THREE of the four labels (all but X), so K2 = number of X-free adjacent pairs, K3 = number of X-free triples. K3 = 0 therefore says exactly: **no X-free run of length 3 exists**. If that held at every fold, the kill-class anti-clustering of route 82 would be a TWO-STEP combinatorial cap (kills force an X within two gaps), not evidence of long-range order --- and route 82's uniform null would be replaced by the fitted chain, with the cap as the mechanism its uncertainty asks for. HYPOTHESES (pre-registered here, before any run, 2026-09-19T09:10Z, this file written first): * CONTROL: the census machine L(T23,p) must reproduce the served 2 / 3 / 2 at p = 29 / 31 / 37. * H1: K3 = 0 for EVERY odd prime p in [29, 1009] at T23 (the X-free run cap is 2 for all 170 folds). FALSIFIER: any p with K3 > 0; the count and location of the exceptions is then the finding. * H2 (scale, the decisive one): at T29 (D = 214 708 725) the lag-2 count stays far below the fitted chain, i.e. K3 << M3 at p = 31 (and 37, 41), where M3 is O(100) events rather than a handful. FALSIFIER: K3 / M3 ~ 1 at T29, which would put route 82's anti-clustering back on the fitted-chain null and close the question. * Reported for every fold: K2, K3, M2, M3, K3/M3, the longest X-free run, and the state counts. Scope: T23 in memory (one word, D = 7 952 175); T29 by the constant-memory segmented sieve of gotcha 47 / job2479 (one full period, `np.flatnonzero` per 2^24-position chunk) accumulating only 16 pair counters and 64 triple counters per prime --- no gap array is materialised. The cyclic closure (the wrap gap between the last and first residue) is omitted past the first chunk: O(1) of D. """ import json import sys import time import numpy as np ODD23 = [3, 5, 7, 11, 13, 17, 19, 23] ODD29 = ODD23 + [29] CH = 1 << 24 SERVED_T23 = {29: 2, 31: 3, 37: 2} T29_PRIMES = (31, 37, 41) PRIMES_T23 = [p for p in range(29, 1010, 2) if all(p % d for d in range(3, int(p ** 0.5) + 1, 2))] IN_K = np.array([0, 0, 1, 1]) # K = {P, M} ... see note: here K is {Z,P,M} = all but X def label_of(gaps, p): """0 = X, 1 = Z (g = 0), 2 = P (g = +2), 3 = M (g = -2) mod p --- the served census labels.""" m = gaps % p lab = np.zeros(gaps.size, dtype=np.int8) lab[m == 0] = 1 lab[m == 2] = 2 lab[m == p - 2] = 3 return lab def kill_mask(lab): return lab != 0 # K = {Z, P, M} = all but X def fold_stats(lab, p): D = int(lab.size) K = kill_mask(lab) K2 = int((K[:-1] & K[1:]).sum()) K3 = int((K[:-2] & K[1:-1] & K[2:]).sum()) T = np.bincount(lab[:-1].astype(np.int64) * 4 + lab[1:], minlength=16).reshape(4, 4).astype(float) rows = T.sum(axis=1, keepdims=True) Q = np.divide(T, rows, out=np.zeros_like(T), where=rows > 0) pi = np.bincount(lab.astype(np.int64), minlength=4).astype(float) / D ink = IN_K M2 = D * sum(pi[a] * ink[a] * float((Q[a] * ink).sum()) for a in range(4)) M3 = 0.0 for a in range(4): if not ink[a]: continue for b in range(4): if ink[b]: M3 += D * pi[a] * Q[a, b] * float((Q[b] * ink).sum()) # longest X-free run (a run of consecutive kill labels) xpos = np.flatnonzero(~K) if xpos.size == 0: maxrun = D else: bounds = np.concatenate(([-1], xpos, [D])) runs = np.diff(bounds) - 1 b0 = int(bounds[0] + 1) b1 = int(D - 1 - bounds[-2] - 1) + 1 if bounds[-2] >= 0 else 0 maxrun = int(runs[1:-1].max()) if runs.size > 2 else 0 maxrun = max(maxrun, int(bounds[1])) # leading run maxrun = max(maxrun, int(D - 1 - bounds[-2])) # trailing run del b0, b1 return { "p": int(p), "D": D, "K2": K2, "K3": K3, "M2_first_order": M2, "M3_first_order": M3, "M2_over_K2": (M2 / K2) if K2 else None, "K3_over_M3": (K3 / M3) if M3 else None, "max_X_free_run": maxrun, "state_counts": {s: int(v) for s, v in zip(("X", "Z", "P", "M"), np.bincount(lab.astype(np.int64), minlength=4))}, "nK": int(K.sum()), } def machine_L(gaps, p): """Served census L only (return #1188, C1) --- the control.""" m = gaps % p q = (m == 0) | (m == 2) | (m == p - 2) if bool(q[0]) or not bool(q[-1]): j0 = int(np.flatnonzero(~q)[0]) gaps = np.roll(gaps, -j0) runs = {} l0 = l1 = 1 for gi in gaps.tolist(): r = gi % p if r == 0: l0 += 1 l1 += 1 elif r == 2: runs[l0] = runs.get(l0, 0) + 1 l0, l1 = l1 + 1, 1 elif r == p - 2: runs[l1] = runs.get(l1, 0) + 1 l1, l0 = l0 + 1, 1 else: runs[l0] = runs.get(l0, 0) + 1 runs[l1] = runs.get(l1, 0) + 1 l0 = l1 = 1 return int(max(runs)) def t23_word(): P = 2 for q in ODD23: P *= q parts = [] for start in range(0, P, CH): end = min(start + CH, P) ok = np.ones(end - start, dtype=bool) ok[(-start) % 2::2] = False for q in ODD23: ok[(-start) % q::q] = False ok[(q - 2 - start) % q::q] = False parts.append(np.flatnonzero(ok).astype(np.int64) + start) return np.concatenate(parts), P def t29_stats(primes): """One full period of T29 by the constant-memory segmented sieve; pair/triple counters only.""" P = 2 for q in ODD29: P *= q d_expected = 1 for q in ODD29: d_expected *= (q - 2) pair = {p: np.zeros(16, dtype=np.int64) for p in primes} trip = {p: np.zeros(64, dtype=np.int64) for p in primes} carry = {p: None for p in primes} total = 0 first = prev = None maxgap = 0 wrap = None for start in range(0, P, CH): end = min(start + CH, P) ok = np.ones(end - start, dtype=bool) ok[(-start) % 2::2] = False for q in ODD29: ok[(-start) % q::q] = False ok[(q - 2 - start) % q::q] = False idx = np.flatnonzero(ok) if idx.size == 0: continue g = idx + start if first is None: first = int(g[0]) d = np.diff(g) else: d = np.diff(np.concatenate((np.array([prev], dtype=np.int64), g))) prev = int(g[-1]) total += int(g.size) if d.size: m = int(d.max()) if m > maxgap: maxgap = m for p in primes: lab = label_of(d, p) seq = lab if carry[p] is None else np.concatenate((carry[p], lab)) a = seq[:-1].astype(np.int64) * 4 + seq[1:] pair[p] += np.bincount(a, minlength=16) b = (seq[:-2].astype(np.int64) * 4 + seq[1:-1]) * 4 + seq[2:] trip[p] += np.bincount(b, minlength=64) carry[p] = seq[-2:].copy() del ok, idx, g wrap = first + P - prev if wrap > maxgap: maxgap = wrap out = {"P": int(P), "D": total, "D_expected": int(d_expected), "D_exact": total == d_expected, "maxgap": maxgap, "folds": {}, "note": "wrap gap counted in the gap multiset control only, not in the counters"} for p in primes: T = pair[p].reshape(4, 4).astype(float) TR = trip[p].reshape(4, 4, 4).astype(float) rows = T.sum(axis=1, keepdims=True) Q = np.divide(T, rows, out=np.zeros_like(T), where=rows > 0) n = T.sum() pi = T.sum(axis=1) / n K2 = float(T[1:, 1:].sum()) # both labels in K = all but X K3 = float(TR[1:, 1:, 1:].sum()) ink = IN_K M2 = n * sum(pi[a] * ink[a] * float((Q[a] * ink).sum()) for a in range(4)) M3 = 0.0 for a in range(4): if not ink[a]: continue for b in range(4): if ink[b]: M3 += n * pi[a] * Q[a, b] * float((Q[b] * ink).sum()) out["folds"][str(p)] = { "p": int(p), "D_labelled": int(n), "K2": int(K2), "K3": int(K3), "M2_first_order": M2, "M3_first_order": M3, "M2_over_K2": (M2 / K2) if K2 else None, "K3_over_M3": (K3 / M3) if M3 else None, "state_counts": {s: int(v) for s, v in zip(("X", "Z", "P", "M"), T.sum(axis=1))}, } return out def main(): t0 = time.time() checks = [] res, P = t23_word() gaps = np.append(np.diff(res), res[0] + P - res[-1]) checks.append({"id": "A1_gap_sum_is_period", "pass": int(gaps.sum()) == P, "detail": f"sum(gaps)={int(gaps.sum())} period={P}"}) for p in (29, 31, 37): L = machine_L(gaps, p) checks.append({"id": f"A2_served_L_T23_{p}", "pass": L == SERVED_T23[p], "detail": f"machine L={L} served={SERVED_T23[p]}"}) folds = {} for p in PRIMES_T23: folds[str(p)] = fold_stats(label_of(gaps, p), p) ok_identity = all(abs(f["M2_first_order"] - f["K2"]) < 1e-6 for f in folds.values()) checks.append({"id": "B1_M2_equals_K2_identity_all_folds", "pass": bool(ok_identity), "detail": f"fitted first-order chain reproduces K2 at all {len(folds)} primes 29..1009"}) nonzero = {p: f["K3"] for p, f in folds.items() if f["K3"] > 0} checks.append({"id": "B2_H1_K3_zero_all_folds", "pass": len(nonzero) == 0, "detail": f"K3>0 at {len(nonzero)} of {len(folds)} primes"}) maxrun = {p: f["max_X_free_run"] for p, f in folds.items()} mr = max(maxrun.values()) checks.append({"id": "B3_X_free_run_cap", "pass": True, "detail": f"max X-free run over all folds = {mr} (2 means K3=0 everywhere)"}) t29 = t29_stats(T29_PRIMES) checks.append({"id": "C1_T29_D_exact", "pass": bool(t29["D_exact"]), "detail": f"D={t29['D']} expected={t29['D_expected']} maxgap={t29['maxgap']}"}) c2 = {p: f for p, f in t29["folds"].items()} checks.append({"id": "C2_T29_K3_below_M3", "pass": all((f["K3_over_M3"] or 0) < 0.5 for f in c2.values()), "detail": "; ".join(f"p={p}: K3={f['K3']} M3={round(f['M3_first_order'],2)}" for p, f in c2.items())}) checks.append({"id": "C3_T29_identity", "pass": all(abs(f["M2_first_order"] - f["K2"]) < 1e-6 for f in c2.values()), "detail": "M2 = K2 at T29 folds too"}) out = {"T23": {"folds": folds, "max_X_free_run_over_all_folds": mr, "primes_K3_nonzero": nonzero, "primes_with_max_run_2": sum(1 for v in maxrun.values() if v == 2), "n_folds": len(folds)}, "T29": t29, "checks": checks, "all_pass": all(c["pass"] for c in checks), "wall_s": round(time.time() - t0, 3)} with open("job2495b-checks.log", "w", encoding="utf-8") as fh: json.dump(out, fh, indent=1, sort_keys=True) fh.write("\n") print(json.dumps({"all_pass": out["all_pass"], "wall_s": out["wall_s"], "checks": checks, "T23_summary": {r: out["T23"][r] for r in ("max_X_free_run_over_all_folds", "primes_K3_nonzero", "n_folds")}, "T29_folds": {p: {k: f[k] for k in ("K2", "K3", "M3_first_order", "K3_over_M3")} for p, f in c2.items()}}, indent=1, sort_keys=True)) return 0 if out["all_pass"] else 1 if __name__ == "__main__": sys.exit(main())