#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Reduced-scale fold test for route 138 / job 2858 (run-2026-09-23-l). Direct implementation of research/moving-cutoff-parity.md (9), the same object the served research/centered-discrepancy-measurement.js implements as its naive control (iii): f(n) = Lambda(n-2) * mu(n) (standard Lambda, prime powers kept) M = sum_{x/2 < n <= x} f(n) (y-free) D_y = sum_{e <= Q, e odd, mu(e)!=0} mu(e) * sum_{n > max(x/2, e*y)} (log(e/n)) * ( 1_{e|n} f(n) - f(n)/phi(e) ) with y = ceil(x^theta), Q = floor(x/y). Deterministic; no randomness. usage: python3 fold_test.py [—nothing] out: work/fold_test.json (+ stdout summary) """ import json import math import os import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) J_LIST = [16, 18, 20] THETA_LIST = [0.48, 0.49, 0.50] SERVED_THETA = 12 / 25 def sieve(N): """Lambda (standard, prime powers kept), mu and phi on [0, N].""" mu = np.ones(N + 1, dtype=np.int64) lam = np.zeros(N + 1, dtype=np.float64) spf = np.zeros(N + 1, dtype=bool) for p in range(2, N + 1): if spf[p]: continue spf[p::p] = True mu[p::p] *= -1 pk = p while pk <= N: lam[pk] = math.log(p) pk *= p # mu must also be zero on non-squarefree; recompute by square marking sq = np.zeros(N + 1, dtype=bool) p = 2 while p * p <= N: sq[p * p::p * p] = True p += 1 mu[sq] = 0 return lam, mu def phi_table(Q): phi = np.arange(Q + 1, dtype=np.int64) for p in range(2, Q + 1): if phi[p] == p: # p prime phi[p::p] -= phi[p::p] // p return phi def measure(x, theta, lam, mu, cache): """D_y and M for one (x, theta) straight from (9).""" y = math.ceil(x ** theta) Q = x // y if Q not in cache: cache[Q] = phi_table(Q) phi = cache[Q] n = np.arange(x // 2 + 1, x + 1) f = lam[n - 2] * mu[n] M = float(f.sum()) logn = np.log(n.astype(np.float64)) D = 0.0 n_e = 0 for e in range(1, Q + 1, 2): if mu[e] == 0: continue n_e += 1 ae = max(x // 2, e * y) mask = (n % e == 0) jump = np.where(mask, f, 0.0) - f / phi[e] sel = n > ae D += float(mu[e]) * float(np.sum((math.log(e) - logn[sel]) * jump[sel])) return {"x": x, "j": round(math.log2(x)), "theta": theta, "y": y, "Q": Q, "n_odd_sqfree_e": n_e, "support": int(np.count_nonzero(f)), "M": M, "D": D, "D_over_x": D / x, "M_over_x": M / x} def main(): out = {"schema": 1, "j_list": J_LIST, "theta_list": THETA_LIST, "served_theta": SERVED_THETA, "rows": [], "pairs": []} by_j = {} for j in J_LIST: x = 2 ** j lam, mu = sieve(x) cache = {} for theta in THETA_LIST: r = measure(x, theta, lam, mu, cache) out["rows"].append(r) by_j.setdefault(j, {})[theta] = r print(f"j={j} theta={theta} y={r['y']} Q={r['Q']} M={r['M']:.4f} " f"D={r['D']:.4f} D/x={r['D_over_x']:.6e} support={r['support']}", flush=True) # pairs at this fixed x ths = THETA_LIST for i in range(len(ths)): for k in range(i + 1, len(ths)): a, b = by_j[j][ths[i]], by_j[j][ths[k]] dd = a["D"] - b["D"] lx = math.log(x) out["pairs"].append({ "j": j, "theta_a": ths[i], "theta_b": ths[k], "dD": dd, "dD_over_x": dd / x, "dD_over_x_abs": abs(dd) / x, "D_a_over_x": a["D_over_x"], "D_b_over_x": b["D_over_x"], "inv_log2_x": 1.0 / (lx * lx), "threshold_32_over_log2x": 32.0 / (lx * lx), "M_diff": a["M"] - b["M"], }) print(f" pair j={j} theta {ths[i]} vs {ths[k]}: dD={dd:.4f} " f"dD/x={dd / x:.6e} |dD|/x={abs(dd) / x:.6e} " f"thr={32.0 / (lx * lx):.6e}", flush=True) # served-row validation at j=16, theta=0.48 got = [r for r in out["rows"] if r["j"] == 16 and abs(r["theta"] - 0.48) < 1e-12][0] out["validation"] = {"served_naive_D": -1090.9610, "served_naive_M": 948.41, "got_D": got["D"], "got_M": got["M"], "rel_D": abs(got["D"] - (-1090.9610)) / 1090.9610, "rel_M": abs(got["M"] - 948.41) / 948.41} # verdicts f0 = not (out["validation"]["rel_D"] < 5e-5 and out["validation"]["rel_M"] < 5e-5) f2 = all(p["dD_over_x_abs"] == 0.0 for p in out["pairs"]) f1 = any(p["dD_over_x_abs"] > p["threshold_32_over_log2x"] for p in out["pairs"]) out["verdict"] = {"F0_instrument_mismatch": f0, "F2_fold_difference_vacuous": f2, "F1_fold_difference_exceeds_32_over_log2x": f1} with open(os.path.join(HERE, "fold_test.json"), "w", encoding="utf-8") as fh: json.dump(out, fh, indent=2) print("VERDICT", json.dumps(out["verdict"]), flush=True) print("VALIDATION", json.dumps(out["validation"]), flush=True) if __name__ == "__main__": main()