#!/usr/bin/env python3 """The |Q| = 1 covering capacity K*({p}) of route 27, measured with this run's instrument. Route 27's identity: L(T_x, p) = K*({p}) at level x for every prime p > x -- #161's adjacent-kill run IS route 26's covering capacity with a single killer. The derivation is a re-indexing, and the route gives it: r = a or a+2 (mod p) <=> p | u or p | u+2 for u = r - (a+2); the wraparound p-2 is -2 mod p. So both sides are: the longest run of consecutive level-x slots v with v = a or a-2 (mod p) for ONE class a, maximised over the translate a. This file measures that quantity from the tile, by residue arithmetic and a run scan -- no filter, no DP, no CRT -- and compares it with every recorded value this run holds: #161's diagonal as quoted in route 27, and this run's own published fold controls. Method. The period is doubled (the second copy is the first shifted by P#), so a window crossing the period seam is covered with the SAME class a, which is what the object requires. For a prime p the slots of a residue class are gathered once (stable argsort), and for each a the killed indices are the merge of the two classes {a, a-2}: the longest run of consecutive killed SLOTS is then the longest maximal block of consecutive integers in that set, taken over blocks that start inside the first copy (blocks starting in the second copy are translates of such blocks). Cost is O(D) per prime after one O(D log D) sort, not O(p*D). No period of level 29 is built here: the 29# tile is 6.5 GB and the job's memory hint is 2 GB. """ import importlib.util import json import pathlib import sys import time import numpy as np HERE = pathlib.Path(__file__).parent DET = HERE.parent / "readings" _spec = importlib.util.spec_from_file_location("det", str(DET / "determine.py")) det = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(det) T0 = time.time() # #161's diagonal as quoted in route 27 rev 8: L(T_{p-}, p) at folds 7..31 DIAGONAL = [(5, 7, 2), (7, 11, 1), (11, 13, 2), (13, 17, 2), (17, 19, 2), (19, 23, 3), (23, 29, 2), (29, 31, 4)] # the extra single-prime entries this sprint adds, at the two tiles whose K*(Q) this run measured EXTRA = {19: [29, 31, 37, 41, 43], 23: [31, 37, 41, 43, 47, 53]} def log(m): sys.stderr.write("[%8.2fs] %s\n" % (time.time() - T0, m)) sys.stderr.flush() def capacity_single_prime(level, p): """K*({p}) at this level: the longest run of consecutive level-s slots killed by one class of p.""" pos, P = det.tile(level) D = pos.size pd = np.concatenate([pos, pos + P]) # doubled frame: carries the period seam r = (pd % p) idx = np.argsort(r, kind="stable") # stable: indices increase inside a class rs = r[idx] lists = {} for c in np.unique(rs): s = int(np.searchsorted(rs, c, "left")) e = int(np.searchsorted(rs, c, "right")) lists[int(c)] = idx[s:e] best, best_a = 0, None for a in range(p): parts = [lists[c] for c in (a, (a - 2) % p) if c in lists] if not parts: continue k = np.sort(np.concatenate(parts)) if k.size == 0: continue d = np.diff(k) brk = np.flatnonzero(d != 1) starts = np.concatenate([[0], brk + 1]) ends = np.concatenate([brk, [k.size - 1]]) lens = ends - starts + 1 keep = k[starts] < D # a run must start inside the first copy if not keep.any(): continue m = int(lens[keep].max()) if m > best: best, best_a = m, a return {"level": level, "p": p, "D": int(D), "P": int(P), "K_star_single": best, "class_a": best_a} def main(): out = {"tool": "single-prime/1", "diagonal": [], "extra": [], "checks": []} for level, p, recorded in DIAGONAL: if level == 29: out["diagonal"].append({"level": level, "p": p, "recorded": recorded, "K_star_single": None, "note": "T_29 is 6.5 GB of sieve; value taken from #161/route 27"}) continue got = capacity_single_prime(level, p) got["recorded"] = recorded got["agrees"] = bool(got["K_star_single"] == recorded) out["diagonal"].append(got) out["checks"].append({"check": "diagonal fold T_%d with p=%d: measured %d vs recorded %d" % (level, p, got["K_star_single"], recorded), "pass": got["agrees"], "observed": got["K_star_single"]}) log("diagonal T_%-3d p=%-3d measured %d recorded %d %s" % (level, p, got["K_star_single"], recorded, "AGREES" if got["agrees"] else "*** DISAGREES ***")) for level, ps in EXTRA.items(): for p in ps: got = capacity_single_prime(level, p) out["extra"].append(got) log("new entry T_%-3d p=%-3d K*({p}) = %d (class %s)" % (level, p, got["K_star_single"], got["class_a"])) out["all_checks_pass"] = all(c["pass"] for c in out["checks"]) (HERE / "single-prime.json").write_text(json.dumps(out, indent=1, sort_keys=True), encoding="utf-8", newline="\n") log("checks: %d, all pass: %s, %.1fs" % (len(out["checks"]), out["all_checks_pass"], time.time() - T0)) return 0 if out["all_checks_pass"] else 2 if __name__ == "__main__": sys.exit(main())