#!/usr/bin/env python3 """job 2753 / triage of route 117 -- K* in a stream, so Mp stops being a memory wall. The swept instrument (job2732/rise-bound.py) materialises the whole period as a numpy array, which is where route 117's "Mp <= 3e8" comes from: it is a MEMORY cap, not an arithmetic one. Both predicates here are unions of residue classes, alive (slots of B) : r != 0, -2 (mod q) for every prime q | B killed : x != 0, -2 (mod q) for every q in R so a block of the period can be evaluated with strided writes and O(block) memory, at the same speed or better than the array version. Streaming TWO periods and taking the largest linear run inside it returns the cyclic maximum exactly: every cyclic run of length l <= (slots per period) appears whole inside the two copies, and every run of the stream is a cyclic run. That is the whole claim of this file, and it is checked three ways before any new number is reported: G1 the five published custody values of returns #1246/#1250/#1267, verbatim; G2 exact agreement with the swept instrument on rows inside its range; G3 the P=30, |R|=5 class served with return #1365, all 12 rows. python stream-run.py --mode gates python stream-run.py --mode xcheck --rows ../job2732/rows-r5.json python stream-run.py --mode cells --cells cells-triage117.json --out stream-cells.json """ import argparse import json import os import sys import time import numpy as np VERSION = "1.0.0" HERE = os.path.dirname(os.path.abspath(__file__)) RISE_BOUND = os.path.join(os.path.dirname(HERE), "job2732", "rise-bound.py") # The five published custody values (returns #1246, #1250, #1267), as in rise-bound.py. GATES = [ (30, (7, 11, 19), 9), (390, (7, 11, 19), 8), (30, (7, 11, 13, 19), 10), (30, (7, 13, 19, 23), 12), (330, (7, 13, 19, 23), 10), ] def prime_factors(n): out, d = [], 2 while d * d <= n: if n % d == 0: out.append(d) while n % d == 0: n //= d d += 1 if n > 1: out.append(n) return out def kstar_stream(B, S, block=1 << 22, max_slots=None): """Maximal cyclic run of consecutive B-slots that are killed by S. O(block) memory; streams two copies of the period and returns the largest linear run inside them (== the cyclic maximum). """ L = B for q in S: L *= q span = 2 * L bq = prime_factors(B) best = 0 cur = 0 slots_total = 0 for start in range(0, span, block): w = min(block, span - start) alive = np.ones(w, dtype=bool) for q in bq: alive[(-start) % q::q] = False alive[((-start - 2) % q)::q] = False killed = np.zeros(w, dtype=bool) for q in S: killed[(-start) % q::q] = True killed[((-start - 2) % q)::q] = True flags = killed[alive] n = int(flags.size) slots_total += n if n == 0: continue idx = np.flatnonzero(flags) if idx.size == 0: cur = 0 continue d = np.diff(idx) brk = np.flatnonzero(d != 1) starts = np.concatenate(([0], brk + 1)) ends = np.concatenate((brk, [idx.size - 1])) lens = (idx[ends] - idx[starts] + 1).astype(np.int64) first = int(lens[0]) last = int(lens[-1]) if int(idx[0]) == 0: cur += first else: cur = first best = max(best, cur, int(lens.max())) if int(idx[-1]) != n - 1: cur = 0 elif idx.size != n: cur = last return best, slots_total, span def swept_kstar(B, S): import importlib.util spec = importlib.util.spec_from_file_location("risebound", RISE_BOUND) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod.kstar(B, S) def mode_gates(): rows, ok = [], True for B, S, want in GATES: t = time.time() got, slots, span = kstar_stream(B, S) good = got == want ok = ok and good rows.append({"base": B, "R": list(S), "expected": want, "observed": got, "slots_per_period": slots, "stream_span": span, "pass": good, "sec": round(time.time() - t, 3)}) print("K*(%-4d,%-20s) expected %2d observed %2d %s" % (B, "{" + ",".join(map(str, S)) + "}", want, got, "PASS" if good else "FAIL")) print(json.dumps({"mode": "gates", "all_pass": ok, "rows": rows}, indent=1)) return 0 if ok else 1 def mode_xcheck(files): rows = [] for f in files: with open(f, encoding="utf-8") as fh: data = json.load(fh) for r in data["rows"]: rows.append(r) bad, checked = 0, 0 for r in rows: B, S = r["P"], tuple(r["R"]) got, _, _ = kstar_stream(B, S) want = r["A"] checked += 1 if got != want: bad += 1 print("MISMATCH P=%d R=%s stream=%d row(A)=%d" % (B, r["R"], got, want)) print(json.dumps({"mode": "xcheck", "rows": checked, "mismatches": bad, "files": files, "claim": "stream A == served A on every row"}, indent=1)) return 0 if bad == 0 else 1 def mode_cells(cells_file, out): with open(cells_file, encoding="utf-8") as fh: cells = json.load(fh) out_rows = [] for c in cells: P, R, p = c["P"], tuple(c["R"]), c["p"] t = time.time() A, slots_M, span_M = kstar_stream(P, R) B, slots_Mp, span_Mp = kstar_stream(P * p, R) out_rows.append({"P": P, "R": list(R), "p": p, "A": A, "B": B, "rise": B - A, "M": P * _prod(R), "Mp": P * _prod(R) * p, "old_instrument_reaches": (P * _prod(R) * p) <= 3e8, "slots_per_period_M": slots_M, "slots_per_period_Mp": slots_Mp, "sec": round(time.time() - t, 3)}) print("P=%-6d R=%-22s p=%-3d A=%-3d B=%-3d rise=%-3d Mp=%.4g %s (%.2fs)" % (P, "{" + ",".join(map(str, R)) + "}", p, A, B, B - A, P * _prod(R) * p, "new" if P * _prod(R) * p > 3e8 else "in-range", time.time() - t)) res = {"tool": "job2753/stream-run.py", "version": VERSION, "mode": "cells", "cells_file": cells_file, "rows": out_rows, "observation": "sec is machine- and load-dependent and is not part of any claim"} if out: with open(out, "w", encoding="utf-8") as fh: json.dump(res, fh, indent=1, sort_keys=True) return 0 def _prod(R): n = 1 for q in R: n *= q return n def main(): ap = argparse.ArgumentParser() ap.add_argument("--mode", choices=["gates", "xcheck", "cells"], default="gates") ap.add_argument("--rows", nargs="*", default=None) ap.add_argument("--cells", default=None) ap.add_argument("--out", default=None) a = ap.parse_args() if a.mode == "gates": return mode_gates() if a.mode == "xcheck": files = a.rows or [os.path.join(os.path.dirname(HERE), "job2732", "rows-r5.json")] return mode_xcheck(files) return mode_cells(a.cells, a.out) if __name__ == "__main__": sys.exit(main())