#!/usr/bin/env python3 """Job #1028 (leads: new route) -- the class skeleton of a level-x gap, verified level-wide. Definitions. S_m = {n : gcd(n(n+2), m#) = 1} are the "slots" of level m; the level-m gap sequence is the sequence of differences of consecutive slots in one period m#. For a prime q, a level-(q-1) slot is DEAD at level q iff it is not a level-q slot, i.e. iff n = 0 or -2 (mod q); the level-q slots are the ACTIVE slots. Level-q gaps are the differences of consecutive ACTIVE slots; a level-q gap therefore spans a maximal run of r >= 0 dead slots, with r+1 level-(q-1) gaps: two BOUNDARY gaps (active-dead and dead-active) and r-1 INNER gaps (dead-dead). The theorem this script checks, instance by instance, over whole phases: (T1) every INNER gap of every level-x gap lies in C_x = {0, +-c(x)} mod 6x, c(x) = 6*(2*6^-1 mod x); (T2) E(gap) = (r-1) + q1 + q2, where E counts the level-(x-1) gaps of the level-x gap that lie in C_x, a1,a2 are the bounding active slots and D1,Dr the first and last dead slots of the run: q1 = [(D1 = 0) & (a1 = 2) or (D1 = -2) & (a1 = -4) (mod x)] and symmetrically for q2. Both are proved, not merely measured: every level-(x-1) slot is odd and = 2 (mod 3), so every slot gap is = 0 (mod 6); an inner gap is the difference of two dead slots, each = 0 or -2 (mod x), so it is = 0 or +-2 (mod x); and these two facts are exactly membership of C_x. A boundary gap joins an active slot (coprime to x, so never = 0 or -2 mod x) to a dead one, so it is in C_x exactly when the two residues are the aligned pair in (T2). The first version of (T2) in this job's history dropped the dead-slot residue from q1,q2 and was refuted by this very script (526 of 2897 runs at x=17); the corrected form is checked here. Consequence used by the return: with m(x) = min(c(x), 6x - c(x)) = 6*min(u, x-u), u = 2*6^-1 mod x, the r-1 inner gaps are each at least m(x), so a level-x gap of length g has L = r+1 level-(x-1) gaps and L - 2 <= (g - 12)/m(x); and its interior sum is at least (L-2)*m(x) -- a proven floor on route 10's interior share. Usage: python skeleton.py [x ...] default: 17 19 23 (full phases) and 29 (window) Output: JSON on stdout, progress on stderr. Stdlib + numpy. """ import json import sys from math import gcd import numpy as np PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] LADDER = {11: (42, 899), 13: (66, 731), 17: (108, 701), 19: (150, 659), 23: (204, 76166567), 29: (258, 1205437109), 31: (348, 8813641451), 37: (528, 544899485411), 41: (546, 3784200788231), 43: (618, 830330079152051)} def primorial(x): r = 1 for p in PRIMES: if p <= x: r *= p return r def class_c(x): return (6 * (2 * pow(6, -1, x) % x)) % (6 * x) def m_min(x): u = 2 * pow(6, -1, x) % x return 6 * min(u, x - u) def level_check(x, span="phase"): M = primorial(x - 1) W = primorial(x) if span == "phase" else 2 * M 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) mod = 6 * x c = class_c(x) gaps = np.diff(idx) incl = np.isin(gaps % mod, np.array([0, c, mod - c], dtype=np.int64)) dead = (idx % x == 0) | (idx % x == x - 2) di = np.diff(dead.view(np.int8)) starts = np.flatnonzero(di == 1) + 1 ends = np.flatnonzero(di == -1) + 1 if dead[0]: ends = ends[1:] if dead[-1]: starts = starts[:-1] assert len(starts) == len(ends) and (starts < ends).all() a1 = idx[starts - 1] a2 = idx[ends] r = ends - starts # dead slots in the run b1 = idx[starts] - a1 b2 = a2 - idx[ends - 1] g = a2 - a1 cum = np.concatenate(([0], np.cumsum(incl))) inner_in = cum[ends - 1] - cum[starts] # inner gaps (r-1 of them) that lie in C_x b1_in = incl[starts - 1] b2_in = incl[ends - 1] E_obs = inner_in + b1_in + b2_in D1 = idx[starts] % x Dr = idx[ends - 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)) E_pred = (r - 1) + q1 + q2 res = dict(x=x, span=span, window=W, periods=(W // M), slots=int(len(idx)), dead=int(dead.sum()), runs=int(len(starts)), violations_T1=int((inner_in != (r - 1)).sum()), violations_T2=int((E_obs != E_pred).sum()), violations_boundary=int((b1_in != q1).sum() + (b2_in != q2).sum()), r_max=int(r.max()), r_hist={str(k): int(v) for k, v in zip(*np.unique(r, return_counts=True))}, q1_hist={str(k): int(v) for k, v in zip(*np.unique(q1.astype(np.int8), return_counts=True))}, free_bits_hist={str(k): int(v) for k, v in zip(*np.unique((q1 + q2).astype(np.int8), return_counts=True))}, max_level_gap=int(g.max())) # the merge-depth bound at every run of this window L = r + 1 slack = np.where(L >= 3, (g - 12) - (L - 2) * m_min(x), 0) res["bound_violations"] = int((np.where(L >= 3, (g - 12) < (L - 2) * m_min(x), False)).sum()) res["bound_tight"] = int((np.where(L >= 3, (g - 12) == (L - 2) * m_min(x), False)).sum()) res["bound_min_slack"] = int(slack.min()) if span == "phase": assert res["max_level_gap"] == LADDER[x][0], \ "level %d: max level-gap %d != recorded A_1 %d" % (x, res["max_level_gap"], LADDER[x][0]) res["m_x"] = m_min(x) res["c_x"] = c return res, dict(g=g, L=r + 1, b1=b1, b2=b2, inner_sum=idx[ends - 1] - idx[starts]) def recorded_bound_table(): """Where the recorded extremal spans sit relative to the proven interior floor.""" rows = [] for x in sorted(LADDER): g, p = LADDER[x] # the spanning run of level-(x-1) slots between the certified endpoints P = primorial(x - 1) s = [n for n in range(p, p + g + 1) if gcd(n * (n + 2), P) == 1] assert s[0] == p and s[-1] == p + g vs = [s[i + 1] - s[i] for i in range(len(s) - 1)] L = len(vs) m = m_min(x) floor_int = (L - 2) * m int_sum = int(sum(vs[1:-1])) rows.append(dict(x=x, A1=g, L=L, gaps=vs, m=m, bound_L=(0 if L < 3 else (g - 12) // m + 2), interior_sum=int_sum, interior_floor=int(floor_int), share=round(int_sum / g, 4), floor_share=round(floor_int / g, 4), floor_attained=bool(L >= 3 and int_sum == floor_int), end_sum=int(vs[0] + vs[-1]))) return rows def main(): xs = [int(a) for a in sys.argv[1:]] or [17, 19, 23] out = {"theorem": "T1 inner gaps in C_x; T2 E = (r-1) + two endpoint residue indicators", "levels": []} for x in xs: span = "phase" if primorial(x) <= 400_000_000 else "window" res, _ = level_check(x, span) sys.stderr.write("x=%-3d %-6s window=%-12d periods=%-3d slots=%-9d runs=%-8d " "T1 viol=%d T2 viol=%d boundary viol=%d bound viol=%d maxg=%-5d (A1=%d)\n" % (x, span, res["window"], res["periods"], res["slots"], res["runs"], res["violations_T1"], res["violations_T2"], res["violations_boundary"], res["bound_violations"], res["max_level_gap"], LADDER[x][0])) out["levels"].append(res) out["recorded"] = recorded_bound_table() for r in out["recorded"]: sys.stderr.write("recorded x=%-3d L=%d interior=%d floor=%d share=%.4f floor_share=%.4f %s\n" % (r["x"], r["L"], r["interior_sum"], r["interior_floor"], r["share"], r["floor_share"], "FLOOR ATTAINED" if r["floor_attained"] else "")) print(json.dumps(out, indent=1)) if __name__ == "__main__": main()