#!/usr/bin/env python3 """Job #1028 -- the END channel: is the extremal gap's excess over the interior floor carried by b1+b2? Context (served record). `research/a3-05-bound-L.md` proves the Run Cost Theorem: inside a kill run, any two adjacent gaps sum to at least 6p (equivalently, the run's inner gaps have the floor c_min(m) = 3pm - p - 2*eta for m odd and 3pm for m even, eta = +1 for p = 1 mod 6, -1 for p = 5 mod 6), and verifies equality at all eight folds it tabulates (7..31). Section 7 then shows that family of bounds is order G2(old)/p, linear in x on the fold diagonal, so it certifies the failing branch of the U-FRAME dichotomy; section 8 locates the wall at hypothesis H''. What that family prices is the RUN. A level-x gap is b1 + (run) + b2, and b1, b2 are the two BOUNDARY gaps - level-(x-1) gaps joining the run to slots that survive the fold. They are not in the run, so Corollary A1 says nothing about them, and (this job's rigidity.py) they lie in the class C_x only under a residue-pairing condition, i.e. they are generically NON-qualifying gaps. This script measures, over whole phases at the folds small enough to enumerate, which channel carries the extremal gap: the interior (floor-priced, bounded by a proven law) or the two ends (priced by nothing in the record). It is the cheap pilot of the route proposed in the return. Usage: python end_channel.py [x ...] default 17 19 23 Output: JSON on stdout, progress on stderr. Stdlib + numpy. """ import json import sys import numpy as np PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] LADDER = {11: 42, 13: 66, 17: 108, 19: 150, 23: 204, 29: 258, 31: 348, 37: 528, 41: 546, 43: 618} def primorial(x): r = 1 for p in PRIMES: if p <= x: r *= p return r def c_min(p, m): """Corollary A1 of research/a3-05-bound-L.md, exact form.""" if m <= 0: return 0 eta = 1 if p % 6 == 1 else -1 return 3 * p * m if m % 2 == 0 else 3 * p * m - p - 2 * eta def fold(x): W = primorial(x) alive = np.ones(W, 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) dead = (idx % x == 0) | (idx % x == x - 2) di = np.diff(dead.view(np.int8)) S = np.flatnonzero(di == 1) + 1 E = np.flatnonzero(di == -1) + 1 if dead[0]: E = E[1:] if dead[-1]: S = S[:-1] a1, a2 = idx[S - 1], idx[E] b1, b2 = idx[S] - a1, a2 - idx[E - 1] inner = idx[E - 1] - idx[S] r = E - S m = r - 1 floor = np.array([c_min(x, int(mm)) for mm in m], dtype=np.int64) g = a2 - a1 endsum = b1 + b2 slack = inner - floor i = int(np.argmax(g)) ends_sorted = np.sort(endsum) rank = int(np.searchsorted(ends_sorted, endsum[i])) res = dict(x=x, A1=int(g.max()), events=int(len(g)), r_max=int(r.max()), floor_attained_events=int((slack == 0).sum()), extremal=dict(r=int(r[i]), b1=int(b1[i]), b2=int(b2[i]), end_sum=int(endsum[i]), inner_sum=int(inner[i]), interior_floor=int(floor[i]), slack=int(slack[i]), share_end=round(float(endsum[i]) / float(g[i]), 4), gap=int(g[i]), global_rank_of_endsum=rank, n_events=int(len(g))), end_sum_max=int(endsum.max()), end_sum_mean=round(float(endsum.mean()), 2), end_sum_quantiles={q: int(np.quantile(endsum, q)) for q in (0.5, 0.9, 0.99, 0.999)}, top10_end_sum=[int(v) for v in ends_sorted[-10:]]) return res def main(): xs = [int(a) for a in sys.argv[1:]] or [17, 19, 23] out = {"folds": []} for x in xs: r = fold(x) assert r["A1"] == LADDER[x], "fold %d: phase max gap %d != recorded A_1 %d" % (x, r["A1"], LADDER[x]) e = r["extremal"] sys.stderr.write("fold %-3d A1=%-4d events=%-8d r_max=%d floor-attained events=%d (%.3f%%)\n" % (x, r["A1"], r["events"], r["r_max"], r["floor_attained_events"], 100.0 * r["floor_attained_events"] / r["events"])) sys.stderr.write(" extremal event r=%d b1=%d b2=%d end=%d inner=%d floor=%d slack=%d " "end share=%.3f rank(end)=%d/%d max end=%d\n" % (e["r"], e["b1"], e["b2"], e["end_sum"], e["inner_sum"], e["interior_floor"], e["slack"], e["share_end"], e["global_rank_of_endsum"], e["n_events"], r["end_sum_max"])) out["folds"].append(r) print(json.dumps(out, indent=1)) if __name__ == "__main__": main()