"""Job #2065, supplementary instrument: the INDEPENDENT SAMPLED CENSUS at 23#->43#. Why it exists: the exact cell is running under a 2400 s job-object cap and the producer writes its JSON only after the whole curve, so a kill would leave no curve at all. This instrument costs about a minute, uses a DIFFERENT counting algorithm (a direct residue test on uniform (slot start, copy) pairs -- no inclusion-exclusion, no subset transform), and writes its JSON after every k, so a kill still leaves the levels already measured. NOT independent of the engine's SPECIFICATION: it reads the same pinned slot word and the same kill classes. What it does not share is the counting method, which is exactly the step route 91's method (d) declares unaffordable. As a SAMPLE it is affordable; its coverage is stated, not implied. Seed 2065, registered in this header before the run. Its control is R1 itself: the anchor N_1 = D*(NCOPY - prod(q-2)) is exact and convention-free, so the sampled N_1-hat must agree with it to within its own standard error. """ import argparse import hashlib import importlib.util import json import os import sys import time import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) ENGINE = os.path.join(HERE, "kstar-engine-u64.py") SEED = 2065 def main(): ap = argparse.ArgumentParser() ap.add_argument("--out", required=True) ap.add_argument("--target", default="23,43") ap.add_argument("--kmax", type=int, default=9) ap.add_argument("--u", type=int, default=4000000) a = ap.parse_args() spec = importlib.util.spec_from_file_location("eng", ENGINE) eng = importlib.util.module_from_spec(spec) spec.loader.exec_module(eng) esha = hashlib.sha256(open(ENGINE, "rb").read()).hexdigest() P, Pp = [int(x) for x in a.target.split(",")] t0 = time.time() word, Psharp = eng.slot_word(P) # slot_word returns a PYTHON LIST of residues; indexing it with the sampled index array needs # a real array. (check-2065.py's own R4 omits this conversion and therefore raises # TypeError: only integer scalar arrays can be converted to a scalar index -- measured here, # see the report's defect note. The exact curve it prints is unaffected.) slots = np.asarray(word, dtype=np.int64) D = len(slots) Q = [q for q in eng.PRIMES if P < q <= Pp] ncopy, prod2 = 1, 1 for q in Q: ncopy *= q prod2 *= (q - 2) anchor = D * (ncopy - prod2) res = {"tool": "sample-2065.py", "job": 2065, "route": 91, "engine_sha256": esha, "seed": SEED, "step": "%d#->%d#" % (P, Pp), "D": D, "Q": Q, "NCOPY": ncopy, "prod_q_minus_2": prod2, "anchor_N1": anchor, "U": a.u, "windows_per_period": D * ncopy, "coverage_fraction": a.u / (D * ncopy), "rows": [], "status": "running"} res["setup_seconds"] = round(time.time() - t0, 1) rng = np.random.default_rng(SEED) i = rng.integers(0, D, size=a.u, dtype=np.int64) c = rng.integers(0, ncopy, size=a.u, dtype=np.int64) alive = np.ones(a.u, dtype=bool) for j in range(a.kmax): t1 = time.time() t = i + j idx = t % D carry = c + t // D val = slots[idx] + Psharp * carry killed = np.zeros(a.u, dtype=bool) for q in Q: r = val % q killed |= (r == 0) | (r == q - 2) alive &= killed p = float(alive.mean()) nhat = p * D * ncopy se = (p * (1.0 - p) / a.u) ** 0.5 * D * ncopy rel = (nhat - anchor) / anchor if j == 0 else None z = (nhat - anchor) / se if (j == 0 and se > 0) else None res["rows"].append({"k": j + 1, "sample_N_hat": round(nhat, 1), "sample_se": round(se, 1), "hits": int(alive.sum()), "p_hat": p, "seconds": round(time.time() - t1, 1), "rel_error_vs_anchor": None if rel is None else round(rel, 5), "z_vs_anchor": None if z is None else round(z, 3)}) res["status"] = "k=%d done" % (j + 1) with open(a.out, "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, indent=1, sort_keys=True) print("k=%-2d N_hat=%-22.0f +/- %-14.0f (%d hits, %.1f s)" % (j + 1, nhat, se, int(alive.sum()), res["rows"][-1]["seconds"])) sys.stdout.flush() res["status"] = "complete" res["total_seconds"] = round(time.time() - t0, 1) with open(a.out, "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, indent=1, sort_keys=True) print(json.dumps({"status": res["status"], "rows": len(res["rows"]), "anchor": anchor, "z_N1": res["rows"][0]["z_vs_anchor"], "seconds": res["total_seconds"]})) return 0 if __name__ == "__main__": sys.exit(main())