#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Job #1289 (route 21 rescue): what the entering owner can actually co-kill. Reads nothing that was withheld and runs no published computation. It builds band arithmetic from the definitions route21 states ... D = the old-prime-admissible twin starts in a window (s and s+2 coprime to every prime <= p), weights w >= 0 fixed on them; Q_p= the primes q with p < q <= 2p; phase b kills K_q(b) = {s in D : q | s+b or q | s+b+2}; C_q = max_b w(K_q(b)); W = w(D); delta = W - sum_q C_q; fold p -> r (r the next prime): phase c deletes E_c = K_r(c); D_c = D \\ E_c; R = primes t with 2p < t <= 2r; C'_q(c) = max_b w(K_q(b) cap D_c). ... and checks two things exactly, in integers only: G1 the exact one-step margin identity (return #497's) reproduced term by term against a direct recomputation of both certificates; G2 the structural law this rescue adds: every admissible start is 5 mod 6, so a kill set of an entering owner t contains two slots only at distance d with 6 | d and d = 0 or +-2 (mod t); with |R| = 1 at p = 43 the whole entering cost is a maximum over the t class-pairs {x, x-2} and can be read off the support positions alone. Every number is an exact integer; rational weights are scaled to integers. """ import argparse import json import sys # ------------------------------------------------------------------ band helpers def primes_upto(n): s = [True] * (n + 1) s[0:2] = [False, False] for i in range(2, int(n ** 0.5) + 1): if s[i]: for j in range(i * i, n + 1, i): s[j] = False return [i for i, v in enumerate(s) if v] def allowed(starts, old): """old-prime admissibility of a twin start: s and s+2 coprime to every old prime.""" out = [] for s in starts: if all(s % q and (s + 2) % q for q in old): out.append(s) return out def band(p): P = primes_upto(4 * p + 10) old = [q for q in P if q <= p] Q = [q for q in P if p < q <= 2 * p] nxt = [q for q in P if q > p][0] R = [q for q in P if 2 * p < q <= 2 * nxt] return old, Q, R, nxt def kill(slots, q, b): return [s for s in slots if (s + b) % q == 0 or (s + b + 2) % q == 0] def cap(slots, w, q): return max(sumw(kill(slots, q, b), w) for b in range(q)) def sumw(slots, w): return sum(w[s] for s in slots) def co_kill_distances(t, L): """d in (0, L) with 6 | d and d = 0, +2 or -2 (mod t): the only distances at which two admissible starts (both 5 mod 6) can sit inside one kill set of owner t.""" return [d for d in range(1, L) if d % 6 == 0 and d % t in (0, 2, t - 2)] # ---------------------------------------------------------------------- fixtures CHECKS = [] def chk(name, cond, observed): CHECKS.append({"check": name, "pass": bool(cond), "observed": observed}) return bool(cond) def gate_g2_residue(L, old): """Every admissible twin start is 5 mod 6 whenever 2 and 3 are old primes.""" starts = list(range(1000, 1000 + L)) D = allowed(starts, old) res = sorted({s % 6 for s in D}) return chk("G2a all admissible twin starts are 5 mod 6 (old primes include 2,3)", res == [5] and len(D) > 0, {"residues": res, "slots": len(D)}) def gate_g2_distances(t, L): """The pairwise distance set is exactly the CRT set, checked against the class-pair definition of a kill set (both directions), on the full residue lattice.""" crt = co_kill_distances(t, L) lattice = [x for x in range(L) if x % 6 == 5] direct = set() for x in range(t): cls = [s for s in lattice if s % t in (x, (x - 2) % t)] for i in range(len(cls)): for j in range(i + 1, len(cls)): direct.add(abs(cls[i] - cls[j])) ok = direct == set(crt) maxsize = max(len([s for s in lattice if s % t in (x, (x - 2) % t)]) for x in range(t)) chk("G2b co-killed pair distances = {6 | d, d = 0/+-2 mod t} exactly, both directions", ok, {"distances": crt, "class_pair_max_size": maxsize, "modulus": t}) def gate_g1_saturation(): """Return #497's complete-window p3->5 counterexample, reproduced exactly.""" p = 3 old, Q, R, r = band(p) D = allowed(list(range(23, 42)), old) w = {s: 1 for s in D} W = sumw(D, w) C = {q: cap(D, w, q) for q in Q} # Q_3 = {5} delta = W - sum(C.values()) c = 0 E = kill(D, r, c) Dc = [s for s in D if s not in E] Wp = W - sumw(E, w) Cp = {q: cap(Dc, w, q) for q in R} # R = {7} direct = Wp - sum(Cp.values()) formula = delta + C[r] - sumw(E, w) + sum(C[q] - cap(Dc, w, q) for q in Q if q != r) \ - sum(Cp.values()) return chk("G1a p3->5 counterexample reproduces: W=4, C_5=2, delta=2, deletion 2, " "entering cost 2, margin 0 on both sides", (D == [23, 29, 35, 41] and W == 4 and C[5] == 2 and delta == 2 and sumw(E, w) == 2 and Cp[7] == 2 and direct == 0 and formula == direct), {"D": D, "delta": delta, "deleted": E, "entering_cost": Cp[7], "margin_direct": direct}) def gate_g1_identity(p, a, L, N, weights): """The exact one-step margin identity vs a direct recomputation, on a prescribed instance: every phase, both sides, integer equality. The new band's owners are (Q_p minus {r}) plus R: r is the NEXT prime after p and becomes an old prime, so it contributes a deletion (E_c = K_r(c)) and no owner term. """ old, Q, R, r = band(p) D = allowed(list(range(a, a + L)), old)[:N] if len(D) < N: return None w = {s: weights(s, D, Q, R) for s in D} W = sumw(D, w) C = {q: cap(D, w, q) for q in Q} delta = W - sum(C.values()) rows = [] for c in range(r): E = kill(D, r, c) Dc = [s for s in D if s not in E] Wp = W - sumw(E, w) CpQ = {q: cap(Dc, w, q) for q in Q if q != r} CpR = {t: cap(Dc, w, t) for t in R} direct = Wp - sum(CpQ.values()) - sum(CpR.values()) formula = (delta + C[r] - sumw(E, w) + sum(C[q] - CpQ[q] for q in CpQ) - sum(CpR.values())) rows.append({"c": c, "deleted": len(E), "deleted_w": sumw(E, w), "entering_cost": sum(CpR.values()), "common_savings": sum(C[q] - CpQ[q] for q in CpQ), "margin": direct, "margin_formula": formula}) same = all(x["margin"] == x["margin_formula"] for x in rows) worst = min(rows, key=lambda x: x["margin"]) return {"p": p, "r": r, "R": R, "Q": Q, "W": W, "delta": delta, "C_r": C[r], "phases": len(rows), "identity_exact": same, "min_margin": worst["margin"], "worst_phase": worst, "phases_with_margin_le_0": sum(1 for x in rows if x["margin"] <= 0), "max_entering_cost": max(x["entering_cost"] for x in rows), "phases_with_entering_cost": sum(1 for x in rows if x["entering_cost"] > 0), "old_band_certifies": delta > 0, "D": D, "w": w, "rows": rows} def main(): ap = argparse.ArgumentParser() ap.add_argument("--out", default="entering89.json") a = ap.parse_args() # gates -------------------------------------------------------------- gate_g2_residue(913, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]) gate_g2_distances(89, 913) gate_g2_distances(47, 913) gate_g1_saturation() # prescribed instances of the withheld case's SHAPE (p=43, r=47, N=20, L=913) at # window starts of our own choosing: nothing withheld is read, regenerated or fitted. unit = lambda s, D, Q, R: 1 # noqa: E731 cases, rows = {}, [] for start in (1003, 50021, 100003, 1000003): v = gate_g1_identity(43, start, 913, 20, unit) if v is None: rows.append({"window_start": start, "slots": 0}) continue # the largest kill set any 89-phase can present on this support peak_x, peak = None, [] for x in range(89): grp = [s for s in v["D"] if s % 89 in (x, (x - 2) % 89)] if len(grp) > len(peak): peak_x, peak = x, grp v["peak_89"] = {"x": peak_x, "group": peak, "size": len(peak)} v["diameter"] = v["D"][-1] - v["D"][0] cases[str(start)] = v rows.append({"window_start": start, "diameter": v["diameter"], "W": v["W"], "delta": v["delta"], "certifies": v["old_band_certifies"], "C_47": v["C_r"], "peak_89_size": len(peak), "max_entering_cost": v["max_entering_cost"], "min_margin": v["min_margin"], "phases_with_margin_le_0": v["phases_with_margin_le_0"], "identity_exact": v["identity_exact"]}) # concentration sweep on the first certifying instance: weight lambda on the single # heaviest 89-class-pair, 1 elsewhere (prescribed, not fitted) base_start = next((int(k) for k, c in cases.items() if c["old_band_certifies"]), None) base = cases.get(str(base_start)) if base_start is not None else None sweep = [] if base is not None: D, peak = base["D"], base["peak_89"]["group"] for lam in range(1, 9): wl = {s: (lam if s in peak else 1) for s in D} v = gate_g1_identity(43, int(base_start), 913, 20, lambda s, Dd, Q, R: wl[s]) sweep.append({"lambda": lam, "delta": v["delta"], "certifies": v["old_band_certifies"], "min_margin": v["min_margin"], "max_entering_cost": v["max_entering_cost"], "identity_exact": v["identity_exact"]}) inst = {"instances": rows, "concentration_sweep": sweep, "base_start": base_start if base is not None else None} chk("G2c entering owner at the fold 43->47 is exactly 89 (R = {89}) and the old band " "is the nine primes in (43, 86]", band(43)[2] == [89] and band(43)[1] == [47, 53, 59, 61, 67, 71, 73, 79, 83], {"R": band(43)[2], "Q_43": band(43)[1]}) chk("G1b the margin identity is exact at every phase of every prescribed family", all(r["identity_exact"] for r in inst["instances"] if r.get("W")) and all(x["identity_exact"] for x in inst["concentration_sweep"]), {"instances": [r.get("identity_exact") for r in inst["instances"]], "sweep": [x["identity_exact"] for x in inst["concentration_sweep"]]}) chk("G2e where the support's diameter is below the smallest co-kill distance " "(180 for t=89), no phase of the entering owner can kill two slots at once", all((r["diameter"] < 180 and r["peak_89_size"] == 1) or r["diameter"] >= 180 for r in inst["instances"] if r.get("W")), [{"start": r["window_start"], "diameter": r.get("diameter"), "peak_89": r.get("peak_89_size")} for r in inst["instances"] if r.get("W")]) chk("R1 whenever the old band certifies (delta > 0), the one-step margin stays " "positive at all 47 phases, on every prescribed family tested", all(r["min_margin"] > 0 for r in inst["instances"] if r.get("certifies")) and all(x["min_margin"] > 0 for x in inst["concentration_sweep"] if x["certifies"]), {"instances": [{"start": r["window_start"], "delta": r.get("delta"), "min_margin": r.get("min_margin")} for r in inst["instances"]], "sweep": [{"lambda": x["lambda"], "delta": x["delta"], "min_margin": x["min_margin"]} for x in inst["concentration_sweep"]]}) chk("G2d with unit weights the entering cost never exceeds the largest 89-class-pair " "occupancy of the support (the entering owner has no other way to reach two " "slots at one phase)", all(r["max_entering_cost"] <= r["peak_89_size"] for r in inst["instances"] if r.get("W")) and all(v["max_entering_cost"] <= v["peak_89"]["size"] for v in cases.values()), {"instances": [{"start": r["window_start"], "entering": r["max_entering_cost"], "peak_89": r["peak_89_size"]} for r in inst["instances"] if r.get("W")]}) out = { "job": 1289, "route": 21, "instance": {"p": 43, "r": 47, "N": 20, "L": 913, "window_starts": [1003, 50021, 100003, 1000003], "note": "prescribed new cases of the withheld case's shape; the " "withheld p43/N20/a10007 dual witness is neither read nor " "regenerated nor fitted"}, "structural": { "admissible_residue_mod_6": 5, "entering_owner": 89, "R": cases[str(base_start)]["R"] if base is not None else [89], "Q_43": cases[str(base_start)]["Q"] if base is not None else [], "co_kill_distances_mod_89_in_913": co_kill_distances(89, 913), "co_kill_distances_mod_47_in_913": co_kill_distances(47, 913), }, "measured": inst, "case_detail": {k: {kk: vv for kk, vv in v.items() if kk not in ("D", "w", "rows")} for k, v in cases.items()}, "checks": CHECKS, "passed": sum(1 for c in CHECKS if c["pass"]), "total": len(CHECKS), "exit_ok": all(c["pass"] for c in CHECKS), } with open(a.out, "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh, indent=1, sort_keys=True) fh.write("\n") print(json.dumps({k: out[k] for k in ("instance", "structural", "measured", "passed", "total", "exit_ok")}, indent=1)) for c in CHECKS: print(("PASS " if c["pass"] else "FAIL ") + c["check"]) return 0 if out["exit_ok"] else 1 if __name__ == "__main__": sys.exit(main())