#!/usr/bin/env python3 """Job #1074 (triage of route 16) -- is the supply identity Q = 2*(X - Z) local, or only aggregate? Route 16's core relation (return #442, Claim 5) is Q = 2*(X - Z) at four closed phases. Before buying a proof attempt, this asks the cheaper question the triage is for: does the balance hold *locally* - per rotation of the fold, and per cell of the (run length, boundary alignment, inner class) decomposition - or only after summing everything? A local version is a lemma with a mechanism; an aggregate-only balance is a coincidence to be reported as such. The fold factorises (return #442): with M = (x-1)# and idx the T_(x-1) slots of one period, every period shares the same gap array and only the dead flags move, so the x rotations are walked cheaply. Usage: python split.py (fold x = 29) Output: JSON on stdout, progress on stderr. Stdlib + numpy. """ import json import sys import numpy as np X = 29 PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43] def primorial(x): r = 1 for p in PRIMES: if p <= x: r *= p return r def main(): x = X M = primorial(x - 1) alive = np.ones(M, dtype=bool) for p in (q for q in PRIMES if q < x): alive[0::p] = False alive[(p - 2) % p::p] = False idx = np.flatnonzero(alive) N = len(idx) mod = 6 * x c = (6 * (2 * pow(6, -1, x) % x)) % mod gaps = np.concatenate([np.diff(idx), [idx[0] + M - idx[-1]]]) gq = np.isin(gaps % mod, np.array([0, c, mod - c])) gz = gaps % mod == 0 cum = np.concatenate(([0], np.cumsum(np.concatenate([gq, gq, gq])))) cumz = np.concatenate(([0], np.cumsum(np.concatenate([gz, gz, gz])))) per_rotation = [] cells = {} for k in range(x): off = [(k - 1) % x, k, (k + 1) % x] idx3 = np.concatenate([idx + o * M for o in off]) d3 = np.concatenate([(idx + o * M) % x for o in off]) dead3 = (d3 == 0) | (d3 == x - 2) # pairs of this period, for the aggregate channel counts j = N + np.arange(N) da, db = dead3[j], dead3[j + 1] both, one = da & db, da ^ db gp = idx3[j + 1] - idx3[j] qp = np.isin(gp % mod, np.array([0, c, mod - c])) Xk = int(both.sum()) Zk = int((both & (gp % mod == 0)).sum()) Qk = int((one & qp).sum()) di = np.diff(dead3.view(np.int8)) S = np.flatnonzero(di == 1) + 1 E = np.flatnonzero(di == -1) + 1 if dead3[0]: E = E[1:] if dead3[-1]: S = S[:-1] keep = (S >= N) & (S < 2 * N) S, E = S[keep], E[keep] a1, a2 = idx3[S - 1], idx3[E] r = E - S d1, dr = idx3[S] % x, idx3[E - 1] % x q1 = ((d1 == 0) & (a1 % x == 2)) | ((d1 == x - 2) & (a1 % x == x - 4)) q2 = ((dr == 0) & (a2 % x == 2)) | ((dr == x - 2) & (a2 % x == x - 4)) qs = (q1.astype(int) + q2.astype(int)) j0 = S % N j1 = (E - 1) % N ic = np.where(j1 >= j0, cum[N + j1] - cum[N + j0], cum[2 * N] - cum[N + j0] + cum[N + j1]) icz = np.where(j1 >= j0, cumz[N + j1] - cumz[N + j0], cumz[2 * N] - cumz[N + j0] + cumz[N + j1]) per_rotation.append(dict(k=k, X=Xk, Z=Zk, Q=Qk, residual=Qk - 2 * (Xk - Zk), runs=int(len(S)), runs_with_one_qualifying_boundary=int((qs == 1).sum()), runs_with_two=int((qs == 2).sum()))) for rr, qq, cc_ in zip(r, qs, ic - icz): key = "%d|%d|%d" % (int(rr), int(qq), int(cc_)) cells[key] = cells.get(key, 0) + 1 tot = dict(X=sum(p["X"] for p in per_rotation), Z=sum(p["Z"] for p in per_rotation), Q=sum(p["Q"] for p in per_rotation), runs=sum(p["runs"] for p in per_rotation)) tot["residual"] = tot["Q"] - 2 * (tot["X"] - tot["Z"]) out = dict(fold=x, rotations=len(per_rotation), total=tot, local_identity_holds=all(p["residual"] == 0 for p in per_rotation), per_rotation=per_rotation, cells={k: v for k, v in sorted(cells.items())}, cell_key="r|number of qualifying boundary gaps|number of non-class-0 inner gaps") sys.stderr.write("fold %d: total X=%d Z=%d Q=%d Q-2(X-Z)=%d\n" % (x, tot["X"], tot["Z"], tot["Q"], tot["residual"])) sys.stderr.write("per-rotation residuals zero in %d of %d rotations\n" % (sum(1 for p in per_rotation if p["residual"] == 0), len(per_rotation))) sys.stderr.write("runs with two qualifying boundaries: %d\n" % sum(p["runs_with_two"] for p in per_rotation)) print(json.dumps(out, indent=1)) if __name__ == "__main__": main()