#!/usr/bin/env python3 """job 2775 / route 117 pursuit: hard rows of P=210, |R| in {4,5}, M <= 3e8. Refuses to report unless the pre-registration hash and gates G1-G3 pass in the same run. python3 sweep2775.py --threads 3 --out sweep2775.json Needs: ./kstar (cc -O2 -o kstar kstar.c -lpthread), prereg-2775.md, and #1384's hard-210-4.json and stream-cells.json (saved as src_hard-210-4.json, src_stream-cells.json). stdout is the artifact (deterministic); progress goes to stderr. """ import argparse, hashlib, itertools, json, os, subprocess, sys HERE = os.path.dirname(os.path.abspath(__file__)) PREREG_SHA = "0cb758b43f7eb3ea8810223e9efba11abe65d5ff7c20c1bd25e03ddb2e6288e9" P_MAX = 97 GATES = [((30, 1, (7, 11, 19)), 9), ((30, 13, (7, 11, 19)), 8), ((30, 1, (7, 11, 13, 19)), 10), ((30, 1, (7, 13, 19, 23)), 12), ((30, 11, (7, 13, 19, 23)), 10)] def primes_upto(n): return [q for q in range(2, n + 1) if all(q % d for d in range(2, int(q ** 0.5) + 1))] def prod(xs): r = 1 for x in xs: r *= x return r def kstar_batch(rows, threads): """rows: list of (P, p, R). Returns list of K in the same order.""" if not rows: return [] inp = "".join("%d %d %d %s\n" % (P, p, len(R), " ".join(map(str, R))) for P, p, R in rows) out = subprocess.run([os.path.join(HERE, "kstar"), str(threads)], input=inp, capture_output=True, text=True, check=True).stdout.split("\n") res = [] for (P, p, R), line in zip(rows, out): f = list(map(int, line.split())) assert f[:2] == [P, p] and f[2:-1] == list(R), (line, P, p, R) res.append(f[-1]) assert len(res) == len(rows) return res def coprime_R(P, R): """A q in R dividing P kills no alive slot, so K*(P,R) = K*(P, R minus q).""" return tuple(q for q in R if P % q) def main(): ap = argparse.ArgumentParser() ap.add_argument("--threads", type=int, default=1) ap.add_argument("--cap-M", type=float, default=3e8) ap.add_argument("--P", type=int, default=210) ap.add_argument("--nR", default="4,5") ap.add_argument("--out", default=None) a = ap.parse_args() log = lambda *x: print(*x, file=sys.stderr, flush=True) h = hashlib.sha256(open(os.path.join(HERE, "prereg-2775.md"), "rb").read()).hexdigest() if h != PREREG_SHA: sys.exit("pre-registration hash mismatch") # G1 + G2 g = kstar_batch([r for r, _ in GATES], a.threads) g1 = [{"P": r[0], "p": r[1], "R": list(r[2]), "expected": w, "observed": o} for (r, w), o in zip(GATES, g)] if any(x["expected"] != x["observed"] for x in g1): sys.exit("G1 fail %s" % g1) g2 = {"A": g[3], "B": g[4], "rise": g[4] - g[3]} if (g2["A"], g2["B"]) != (12, 10): sys.exit("G2 fail") log("G1/G2 pass") # G3: #1384 served rows h4 = json.load(open(os.path.join(HERE, "src_hard-210-4.json"))) sc = json.load(open(os.path.join(HERE, "src_stream-cells.json"))) q3 = [(210, 1, tuple(r["R"])) for r in h4["rows"]] q3 += [(210, m["p"], tuple(m["R"])) for m in h4["hard_rows_measured"]] notes = [] for c in sc["rows"]: R = coprime_R(c["P"], c["R"]) if len(R) != len(c["R"]): notes.append("stream-cells row P=%d R=%s: %s divide P; evaluated as R=%s" % (c["P"], c["R"], [q for q in c["R"] if c["P"] % q == 0], list(R))) q3 += [(c["P"], 1, R), (c["P"], c["p"], R)] want = [r["A"] for r in h4["rows"]] + [m["B"] for m in h4["hard_rows_measured"]] for c in sc["rows"]: want += [c["A"], c["B"]] got = kstar_batch(q3, a.threads) mism = [(q, w, o) for q, w, o in zip(q3, want, got) if w != o] if mism: sys.exit("G3 fail %s" % mism) g3 = {"values_checked": len(q3), "mismatches": 0, "notes": notes} log("G3 pass", len(q3)) # enumeration allp = primes_upto(P_MAX) base = [q for q in allp if a.P % q] cells = [] for nR in map(int, a.nR.split(",")): sets = sorted(R for R in itertools.combinations(base, nR) if a.P * prod(R) <= a.cap_M) A = kstar_batch([(a.P, 1, R) for R in sets], a.threads) hard = [] for R, Av in zip(sets, A): for p in base: if p not in R and p <= 2 * Av: hard.append((R, Av, p)) log("|R|=%d sets=%d hard rows=%d" % (nR, len(sets), len(hard))) B = kstar_batch([(a.P, p, R) for R, _, p in hard], a.threads) rows = [{"R": list(R), "A": Av, "p": p, "B": Bv, "rise": Bv - Av, "Mp": a.P * prod(R) * p} for (R, Av, p), Bv in zip(hard, B)] by = {} for r in rows: k = "A=%d,p=%d" % (r["A"], r["p"]) by[k] = min(by.get(k, 99), r["rise"]) hist = {} for r in rows: hist[str(r["rise"])] = hist.get(str(r["rise"]), 0) + 1 cells.append({ "P": a.P, "nR": nR, "cap_M": a.cap_M, "R_sets": len(sets), "A_hist": {str(v): A.count(v) for v in sorted(set(A))}, "sets_with_no_hard_row": sum(1 for R, Av in zip(sets, A) if not any(p <= 2 * Av for p in base if p not in R)), "hard_rows": len(rows), "rise_hist": dict(sorted(hist.items(), key=lambda t: int(t[0]))), "min_rise": min((r["rise"] for r in rows), default=None), "min_rise_per_A_p": dict(sorted(by.items())), "drops": [r for r in rows if r["rise"] < 0], "rows": rows}) out = {"tool": "job2775/sweep2775.py", "instrument": "job2775/kstar.c", "prereg_sha256": PREREG_SHA, "gates": {"G1": g1, "G2_1267_witness": g2, "G3_1384_rows": g3}, "hard_rule": "p prime <= 97, p not dividing P, p not in R, p <= 2A", "cells": cells, "H1_floor_B_ge_A_minus_1": all(r["rise"] >= -1 for c in cells for r in c["rows"]), "H2_no_rise_le_minus_3": all(r["rise"] > -3 for c in cells for r in c["rows"]), "H3_no_drop": all(r["rise"] >= 0 for c in cells for r in c["rows"])} s = json.dumps(out, indent=1, sort_keys=True) if a.out: with open(a.out, "w", encoding="utf-8") as fh: fh.write(s + "\n") print(s) if __name__ == "__main__": main()