# gaphist1919.py -- route 79 triage (job #1919): the tile gap histogram of T_x = {r mod x# : gcd(r(r+2), x#) = 1} # by a segmented sieve (numpy), then the kill-class functional A(T_x, p) = sum of hist[g] over g = 6k with # k in {0, +-2*6^{-1}} (mod p), g <= G2(T_x), for the folds the route names. Exact integers; no author code. # usage: python gaphist1919.py [

...] e.g. python gaphist1919.py 23 29 31 | python gaphist1919.py 29 31 37 import sys, math, json, time import numpy as np PR = [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] x = int(sys.argv[1]); folds = [int(p) for p in sys.argv[2:]] ps = [q for q in PR if q <= x]; P = math.prod(ps) SEG = 1 << 24 hist = {}; count = 0; last = None; first = None; t0 = time.time() for lo in range(0, P, SEG): hi = min(P, lo + SEG); m = hi - lo keep = np.ones(m, dtype=bool) for q in ps: # kill r = 0 mod q and r = q-2 mod q (r and r+2 divisible by q) s0 = (-lo) % q; keep[s0::q] = False s2 = (q - 2 - lo) % q; keep[s2::q] = False pos = np.flatnonzero(keep).astype(np.int64) + lo if len(pos): if first is None: first = int(pos[0]) if last is not None: g = int(pos[0]) - last; hist[g] = hist.get(g, 0) + 1 d = np.diff(pos) if len(d): v, c = np.unique(d, return_counts=True) for a, b in zip(v.tolist(), c.tolist()): hist[a] = hist.get(a, 0) + b last = int(pos[-1]); count += len(pos) # wrap-around gap (cyclic): from last slot to first slot + P g = first + P - last; hist[g] = hist.get(g, 0) + 1 D_expected = math.prod(q - 2 for q in ps if q >= 3) G2 = max(hist) out = {'x': x, 'P': P, 'D': count, 'D_expected_prod(q-2)': D_expected, 'sum_gaps': sum(k * v for k, v in hist.items()), 'gap_count': sum(hist.values()), 'min_gap': min(hist), 'G2': G2, 'all_gaps_mult_6': all(k % 6 == 0 for k in hist), 'n_distinct_gaps': len(hist), 'hist': {str(k): v for k, v in sorted(hist.items())}, 'folds': {}} out['wall_s'] = round(time.time() - t0, 1) print(f"x={x} P={P} D={count} (prod(q-2)={D_expected}) gaps={sum(hist.values())} sum={out['sum_gaps']} min={min(hist)} G2={G2} all%6={out['all_gaps_mult_6']} distinct={len(hist)}", flush=True) print(f"wall {out['wall_s']}s", file=sys.stderr) # timing to stderr: stdout must reproduce byte for byte print('hist:', ' '.join(f'{k}:{v}' for k, v in sorted(hist.items()))) for p in folds: inv6 = pow(6, -1, p); ks = {0, (2 * inv6) % p, (-2 * inv6) % p} vals = [6 * k for k in range(1, G2 // 6 + 1) if k % p in ks] A = sum(hist.get(g, 0) for g in vals) out['folds'][str(p)] = {'kill_class_values_le_G2': vals, 'A': A, 'A_over_D': A / count, 'naive_3_over_p': 3 / p} print(f"fold p={p}: kill-class gap values <= G2: {vals} A = {A} A/D = {A/count:.6f} (naive 3/p = {3/p:.4f})", flush=True) out.pop('wall_s', None) json.dump(out, open(f'gaphist1919-x{x}.json', 'w'), indent=0)