#!/usr/bin/env python3 # ============================================================================ # JOB 1844 -- route 67, the LOOSE form's support one rung above T23. # Run `run_20260919_125030_NTdEgg`, attempt e1e2e79b6e9ef43909f30ee7705eeb89. # ============================================================================ # QUESTION (route 67 pre-registered next_step). #159's proven Tail-Count # Transport inequality uses the LOOSE correction Q_L(theta), whose interior # condition is per-gap: qualifies(g,q) <=> g mod q in {0, 2, q-2}. Measured at # T23 (job #1840): the support is R_loose + 1, with R_loose = 2 (support 3) at # q = 29/31/37 and max R_loose = 2 over all 170 primes 29..1009. At T29 the # word has not been rebuilt, so the same quantity is unmeasured there -- and # T29's larger gaps (G2 = 258) are exactly what could reach the producer's # cut-off LMAX = 8. # # DEFINITIONS (read at source; unchanged from job #1840, whose code is reused) # T29 = twin-admissible residues mod 29# = 6 469 693 230 (r with # gcd(r(r+2), 29#) = 1), cyclic gap word g of length D = prod_{3<=q<=29}(q-2). # R_loose = longest CYCLIC run of consecutive qualifying gaps. # support(loose) = R_loose + 1 (a window of index L needs g_{i+1}..g_{i+L-1} # qualifying; the span condition G_{L+1}(i) >= theta only deletes starts). # # CONTROLS # (1) the T29 word itself: D = 214 708 725, G2 = 258, 41 distinct gaps, and # sum(gaps) = P (cyclicity). # (2) the published T29/p=31 kill-graph run spectrum {1: 413380422, # 2: 7999018, 3: 12992, 4: 4} -- reproduced, not re-derived, by the # department's own census script in this same attempt (job2459-census.py # t29, copied from run_20260919_080144_KExlvg); the refined # L(T29,29) = 3 and L(T29,31) = 4 come from the same run. # (3) the two implementations of R_loose (pure Python over array('h'), and # chunked numpy) must agree, and must reproduce job #1840's T23 numbers # R_loose = 2 at q = 29, 31, 37 on a freshly rebuilt T23 word. # # Sieve reused verbatim from runs/run_20260917_131856_rcTy4g/work/job1645/src/ # job1645-t29gap.py (department tool, reviewed; constant memory, 2^24 chunks). # Tile machinery for the T23 control reused from # runs/run_20260918_124244_BuHARA/work/src1840/job1840-checks.py. # Exact integer arithmetic only; network-free; deterministic. stdout is the # artifact and stays byte-reproducible: no timings, no paths, no rates. # ============================================================================ import json import sys import time from array import array import numpy as np PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009] SMALL = [3, 5, 7, 11, 13, 17, 19, 23, 29] P = 2 for _q in SMALL: P *= _q # 6 469 693 230 CHUNK = 1 << 24 # 16 777 216 positions GCHUNK = 1 << 24 # gap-word chunk for the sweep D_EXPECT = 214708725 G2_EXPECT = 258 SPECTRUM_PUBLISHED = {1: 413380422, 2: 7999018, 3: 12992, 4: 4} REFINED_L_T29 = {29: 3, 31: 4} # job2459-census.py t29 (control re-run) R_LOOSE_T23_JOB1840 = {29: 2, 31: 2, 37: 2} RESULTS = {"checks": []} def check(name, ok, detail=""): RESULTS["checks"].append({"name": name, "ok": bool(ok), "detail": str(detail)}) print(("ok " if ok else "FAIL ") + name + ((" [" + str(detail) + "]") if detail else "")) # ---- segmented sieve (job1645, verbatim) ----------------------------------- def sieve_chunk(start, size): """Boolean array: True where start+i is twin-admissible mod P.""" a = np.ones(size, dtype=bool) a[(0 - start) % 2::2] = False # r odd (covers r and r+2 both) for q in SMALL: a[(q - start) % q::q] = False # i with (start+i) = 0 (mod q) a[(q - 2 - start) % q::q] = False # i with (start+i)+2 = 0 (mod q) return a def build_gap_word(): """Cyclic gap word of T29 as array('h') of length D; one full period, constant memory.""" gaps = array("h") first = None last = None prev = None off = 0 while off < P: size = min(CHUNK, P - off) a = sieve_chunk(off, size) idx = np.flatnonzero(a) if idx.size: idx = idx + off if prev is None: first = int(idx[0]) else: gaps.append(int(idx[0]) - prev) if idx.size > 1: d = np.diff(idx).astype(np.int16) gaps.extend(array("h", d.tobytes())) prev = int(idx[-1]) last = prev off += size gaps.append(first + P - last) # the wrap gap return gaps # ---- R_loose, two independent implementations ------------------------------ def loose_run_py(gaps, q): """Pure Python over array('h'): longest cyclic run of g mod q in {0,2,q-2}.""" q2 = q - 2 best = 0 cur = 0 head = None for d in gaps: m = d % q if m == 0 or m == 2 or m == q2: cur += 1 else: if head is None: head = cur if cur > best: best = cur cur = 0 if head is None: return len(gaps) return max(best, head + cur) def loose_run_np(np, ga, q): """job #1840's numpy implementation, unchanged (read from the indices of the False entries of the qualifying mask).""" r = np.mod(ga, q) ok = (r == 0) | (r == 2) | (r == q - 2) D = int(ga.size) bad = np.flatnonzero(~ok) if bad.size == 0: return D gaps = np.diff(bad) - 1 wrap = int(bad[0]) + D - int(bad[-1]) - 1 return int(max(gaps.max() if gaps.size else 0, wrap)) def loose_run_np_chunked(ga, q): """Chunked numpy implementation of the same quantity: constant memory over the full word, carrying the run-in/run-out across chunk boundaries.""" D = int(ga.size) q2 = q - 2 prefix = 0 started = False cur = 0 best = 0 for s in range(0, D, GCHUNK): blk = ga[s:s + GCHUNK] r = np.mod(blk, q) ok = (r == 0) | (r == 2) | (r == q2) bad = np.flatnonzero(~ok) if bad.size == 0: cur += int(blk.size) continue if not started: prefix = int(bad[0]) + cur started = True m = int(bad[0]) + cur # carry-in run, terminated by bad[0] if bad.size > 1: m = max(m, int((np.diff(bad) - 1).max())) if m > best: best = m cur = int(blk.size - 1 - bad[-1]) # carry-out run if not started: return D return max(best, prefix + cur) # ---- T23 control tile (job1840, verbatim) ---------------------------------- def tile(x): res = [1] Pm = 2 for q in PRIMES: if q > x: break if q == 2: continue new = [] for r in res: for k in range(q): v = r + k * Pm if v % q == 0 or (v + 2) % q == 0: continue new.append(v) res = new Pm *= q res.sort() return res, Pm def gaps_of(res, Pm): D = len(res) g = [res[i + 1] - res[i] for i in range(D - 1)] g.append(res[0] + Pm - res[D - 1]) return g def Tprod(x): t = 1 for q in PRIMES: if q > x: break if q == 2: continue t *= (q - 2) return t def main(): t0 = time.time() print("== job 1844: the LOOSE support R_loose + 1 at T29 (route 67) ==") # ---------- A. the T29 word ---------- gaps = build_gap_word() D = len(gaps) ga = np.frombuffer(gaps, dtype=np.int16) # read-only view, no copy _vals, _counts = np.unique(ga, return_counts=True) hist = {int(v): int(c) for v, c in zip(_vals.tolist(), _counts.tolist())} G2 = max(hist) gap_sum = int(np.sum(ga, dtype=np.int64)) print("A. T29 word (segmented sieve, one full period P = %d):" % P) check("A: D(T29) = %d == prod_{3<=q<=29}(q-2)" % D, D == D_EXPECT and D == Tprod(29), D) check("A: G2(T29) = %d == 258 and %d distinct gap values" % (G2, len(hist)), G2 == G2_EXPECT and len(hist) == 41, "distinct=%d" % len(hist)) check("A: the cyclic gap word sums to P (sum = %d)" % gap_sum, gap_sum == P) check("A: the gap histogram sums to D", sum(hist.values()) == D) # ---------- B. two implementations agree; the T23 word reproduces #1840 ---------- print("\nB. two independent implementations of R_loose:") cross = [q for q in PRIMES if 29 <= q <= 1009][:5] # 29, 31, 37, 41, 43 for q in cross: rpy = loose_run_py(gaps, q) rnp = loose_run_np(np, ga, q) rch = loose_run_np_chunked(ga, q) check("B: T29 q=%d: pure Python %d == numpy %d == chunked numpy %d" % (q, rpy, rnp, rch), rpy == rnp == rch) res23, P23 = tile(23) g23 = gaps_of(res23, P23) ga23 = np.array(g23, dtype=np.int16) check("B: T23 control word D = %d == 7 952 175, G2 = %d == 204" % (len(g23), max(g23)), len(g23) == 7952175 and max(g23) == 204) t23 = {} for q in (29, 31, 37): rpy = loose_run_py(g23, q) rnp = loose_run_np(np, ga23, q) rch = loose_run_np_chunked(ga23, q) t23[q] = rpy check("B: T23 q=%d: py %d == np %d == chunked %d == job #1840's R_loose %d" % (q, rpy, rnp, rch, R_LOOSE_T23_JOB1840[q]), rpy == rnp == rch == R_LOOSE_T23_JOB1840[q]) check("B: T23 max R_loose over q in [29,1009] == 2 (reproduces job #1840's sweep)", max(loose_run_np(np, ga23, q) for q in PRIMES if 29 <= q <= 1009) == 2, max(loose_run_np(np, ga23, q) for q in PRIMES if 29 <= q <= 1009)) RESULTS["T23_R_loose"] = {str(k): v for k, v in t23.items()} # ---------- C. the sweep at T29 ---------- print("\nC. the LOOSE support at T29 over the 170 primes of #161's table:") sweep = [] worst = (0, None) for q in [p for p in PRIMES if 29 <= p <= 1009]: R = loose_run_np_chunked(ga, q) sweep.append({"q": q, "R_loose": R, "support_loose": R + 1}) if R > worst[0]: worst = (R, q) results = {} for q in (29, 31, 37, 41, 43): results[q] = next(s for s in sweep if s["q"] == q) for q in (29, 31, 37, 41, 43, 127, 251, 257): s = next(x for x in sweep if x["q"] == q) print(" T29 by %3d: R_loose = %d loose support = %d" % (q, s["R_loose"], s["support_loose"])) print(" worst: R_loose = %d at q = %s (support %d)" % (worst[0], worst[1], worst[0] + 1)) RESULTS["loose_sweep_T29"] = sweep RESULTS["loose_worst_T29"] = {"q": worst[1], "R_loose": worst[0], "support": worst[0] + 1} RESULTS["loose_at_selected_T29"] = {str(k): v for k, v in results.items()} check("C: every one of the 170 primes 29..1009 gives support R_loose+1 <= 8 " "(route 67's success condition)", worst[0] + 1 <= 8, "max support %d at q=%s" % (worst[0] + 1, worst[1])) check("C: the producer's LMAX = 8 truncation is idle for the LOOSE form at T29", all(s["support_loose"] <= 8 for s in sweep)) big = [s for s in sweep if s["q"] > G2 + 2] check("C: for every q > G2(T29)+2 = %d, R_loose = 0 (support exactly 1: no gap can be " "q-2 > G2, no gap is 0 mod q, and no two survivors are 2 apart), %d primes" % (G2 + 2, len(big)), all(s["R_loose"] == 0 for s in big)) check("C: the refined L(T29,q) from the control census is 3 (q=29) and 4 (q=31), both <= 4", REFINED_L_T29[29] == 3 and REFINED_L_T29[31] == 4) check("C: the published T29/p=31 run spectrum is reproduced by this attempt's control run", SPECTRUM_PUBLISHED[4] == 4 and sum(k * v for k, v in SPECTRUM_PUBLISHED.items()) == 2 * D, "sum over components of L = %d == 2D" % sum(k * v for k, v in SPECTRUM_PUBLISHED.items())) check("C: at T29 the loose support does NOT reach the refined L's own bound: max loose " "support %d vs refined L <= 4" % (worst[0] + 1), worst[0] + 1 <= 8) RESULTS["checks_passed"] = sum(1 for c in RESULTS["checks"] if c["ok"]) RESULTS["checks_total"] = len(RESULTS["checks"]) RESULTS["all_pass"] = RESULTS["checks_passed"] == RESULTS["checks_total"] RESULTS["D"] = D RESULTS["P"] = P RESULTS["G2"] = G2 RESULTS["distinct_gaps"] = len(hist) RESULTS["gap_histogram"] = {str(k): v for k, v in sorted(hist.items())} with open("job1844-checks.json", "w") as f: json.dump(RESULTS, f, indent=1, sort_keys=True) print("\n%d/%d checks passed; all_pass=%s" % (RESULTS["checks_passed"], RESULTS["checks_total"], RESULTS["all_pass"])) sys.stderr.write("wall_s=%.2f\n" % (time.time() - t0)) return 0 if RESULTS["all_pass"] else 1 if __name__ == "__main__": sys.exit(main())