#!/usr/bin/env python3 """job #1965 (explore / discovery, lane formalize): the next term of the closed route's slack ladder for the Comb Discrepancy Lemma (TODO 8c, Q-comb-discrepancy). Object (served definition, `history/staging/comb-discrepancy-tight.md` section 1): C_x = { r : r = 11 or 17 (mod 30); r != 0 and r != p-2 (mod p) for every mid 7 <= p <= x } W = x# , N = 2 * prod_{7<=p<=x}(p-2) , rho = N/W , k = #mids = pi(x) - 3 G(u) = #(C_x cap [0,u)) - rho*u (u integer, W-periodic) Theorem A (PROVEN): sup_{s,l} |#(C_x cap [s,s+l)) - rho*l| = max G - min G =: D_x published slack = 2*3^k / D_x reads 2.68 / 4.29 / 6.61 / 12.36 / 16.41 / 28.99 / 53.64 at @7 .. @29. Walk conventions. For r_1 < ... < r_N the period's points, A_i = (i-1) - rho*r_i (i 1-indexed) is G just after the i-th point; between consecutive points G decreases with slope -rho, so the sup and inf over all integers u in [0,W] are taken in {A_i} U {0}. Hence D_x = max(A, 0) - min(A, 0). This script scans the whole period once, chunk by chunk, keeping only the running max/min, so memory stays constant (CH positions). The analytic count N is a self-check on the scan. Output: one JSON object per level, appended and flushed to the log file given on the command line (never to stdout - `sah.py exec` truncates captured stdout at 4000 bytes). Levels run in increasing cost: the @7..@29 ladder reproduces published slacks (validation gate); @31 is the new term. A level that does not finish is simply absent from the log. """ import json import sys import time import numpy as np PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] MIDS = [7, 11, 13, 17, 19, 23, 29, 31] CH = 1 << 24 def period(x): W = 1 for p in PRIMES: if p > x: break W *= p ms = [p for p in MIDS if p <= x] N = 2 for p in ms: N *= p - 2 return W, ms, N def sharp_constant(x, log): W, ms, N = period(x) rho = N / W t0 = time.time() best_max, best_min = 0.0, 0.0 count = 0 for base in range(0, W, CH): size = min(CH, W - base) a = np.ones(size, dtype=bool) for p in ms: a[(-base) % p::p] = False a[(p - 2 - base) % p::p] = False mask = np.zeros(size, dtype=bool) for c in (11, 17): mask[(-base + c) % 30::30] = True a &= mask found = np.flatnonzero(a) if found.size: vals = (count + np.arange(found.size)) - rho * (base + found) best_max = max(best_max, float(vals.max())) best_min = min(best_min, float(vals.min())) count += int(found.size) ok = count == N D = best_max - best_min rec = {"level": x, "W": W, "k": len(ms), "N": N, "N_scanned": count, "count_ok": ok, "D": D, "trivial": 2 * 3 ** len(ms), "slack": (2 * 3 ** len(ms)) / D, "seconds": round(time.time() - t0, 2)} with open(log, "a", encoding="utf-8") as fh: fh.write(json.dumps(rec, sort_keys=True) + "\n") fh.flush() return rec def main(): log = sys.argv[1] for x in [7, 11, 13, 17, 19, 23, 29, 31]: sharp_constant(x, log) if __name__ == "__main__": main()