#!/usr/bin/env python3 """Question (job #1018, new statistic): the retained censuses keep the twin-slot GAP MULTISET (counts per size). A_1 = G2(x#) is a function of that multiset alone, so no census channel can decide anything about ADJACENCY. Design one finite statistic that can, and pilot it where a full period is exactly computable. Statistic (adjacency excess): A_k(x) = max over cyclic windows of k consecutive T_x gaps of their sum D(x) = A_2(x) - A_1(x) >= 0; 0 iff no two adjacent gaps beat the single largest gap. Why D and not A_2 alone: under the only null that preserves what a census keeps -- a uniform permutation of the same gap multiset -- A_1 is INVARIANT (it is the maximum of the multiset, a constant), so its null has zero variance and cannot carry any adjacency signal. D removes that constant part, so all of D's null variability is caused by adjacency. Matched control: fixed-multiset permutation, seeded. Every level here is one FULL period (x#), so there is no window or boundary error: the gap sequence is exact and cyclic. Cost note: the permutation null is O(nperm * slots) in pure Python; numpy is absent on this machine, so only x <= 19 is pilot-reachable here. x = 23 needs ~440x the x=19 work. """ import json import random import sys import time PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] def gaps_period(x): """Exact cyclic gap sequence of the T_x ladder over one full period x#.""" ps = [p for p in PRIMES if p <= x] N = 1 for p in ps: N *= p alive = bytearray(b"\x01") * N for p in ps: for start in (0, (-2) % p): alive[start::p] = bytearray(len(range(start, N, p))) s = [n for n in range(N) if alive[n]] return [s[i + 1] - s[i] for i in range(len(s) - 1)] + [s[0] + N - s[-1]], N, len(s) def max1(g): return max(g) def max2(g): """max over cyclic adjacent pairs, single pass (no slicing).""" best = g[-1] + g[0] for i in range(len(g) - 1): v = g[i] + g[i + 1] if v > best: best = v return best def maxk(g, k): L = len(g) ext = g + g[:k - 1] best = sum(ext[:k]) for i in range(1, L): v = sum(ext[i:i + k]) if v > best: best = v return best def level(x, nperm): t0 = time.time() g, N, nslots = gaps_period(x) t1 = time.time() A1, A2 = max1(g), max2(g) A3, A4 = (maxk(g, 3), maxk(g, 4)) if len(g) >= 8 else (0, 0) D = A2 - A1 rng = random.Random(1018 + x) s2 = 0 s4 = 0 for _ in range(nperm): rng.shuffle(g) v = max2(g) s2 += v s4 += v * v m2 = s2 / nperm sd2 = ((s4 - nperm * m2 * m2) / (nperm - 1)) ** 0.5 zD = (D - (m2 - A1)) / sd2 if sd2 > 0 else float("nan") gap_counts = {} for v in g: gap_counts[v] = gap_counts.get(v, 0) + 1 top = sorted(gap_counts)[-3:] print(f"x={x:>2} period={N:>12} slots={nslots:>9} gaps={len(g):>9} " f"(sieve {t1 - t0:.2f}s, null {time.time() - t1:.2f}s for {nperm} perms)") print(f" A_1={A1:>5} A_2={A2:>5} A_3={A3:>5} A_4={A4:>5} D=A_2-A_1={D:>5}") print(f" census channel: multiset only, two largest gaps sum " f"{sum(sorted(g)[-2:]):>5}; top-3 sizes {top}") print(f" null A_2: mean={m2:.3f} sd={sd2:.3f} -> z(A_2)={((A2 - m2) / sd2):+.2f} " f"z(D)={zD:+.2f} (A_1 null: invariant, sd=0)") return {"x": x, "period": N, "slots": nslots, "A": {"A1": A1, "A2": A2, "A3": A3, "A4": A4}, "D": D, "multiset_two_largest_sum": sum(sorted(g)[-2:]), "top3_sizes": top, "null_A2": {"mean": m2, "sd": sd2, "nperm": nperm}, "z_A2": (A2 - m2) / sd2, "z_D": zD, "sieve_seconds": round(t1 - t0, 3), "null_seconds": round(time.time() - t1, 3)} def main(): plan = [(13, 8000), (17, 2000), (19, 150)] out = [level(x, n) for x, n in plan] json.dump(out, open("adjacency.json", "w"), indent=1) print() print("z(D) by level:", {r["x"]: round(r["z_D"], 2) for r in out}) print("adjacency excess D by level:", {r["x"]: r["D"] for r in out}) print("census-only quantity (two largest) by level:", {r["x"]: r["multiset_two_largest_sum"] for r in out}) return 0 if __name__ == "__main__": sys.exit(main())