#!/usr/bin/env python3 """Reference statistic (job #2041, design run): the signed below-level Type II piece T_II^low. Implements fixed-endpoint-discrepancy.md eq. (2.7): T_II^low = - sum_{eU, b>V, ab in I_e, (ab,e)=1} mu(a) * gamma_V(b) * log(ab) * Lambda(eab-2), gamma_V(b) = sum_{k|b, k>V} mu(k), I_e = (x/(2e), x/e], e0 = x^(1/2-eps'), U = V = x^(eps'/3). Plus a seeded random-sign control: mu(a) -> +/-1 (same support), which measures the magnitude generic signs would give, so |T_II^low| / |control| tests for Mobius-specific (beyond square-root) cancellation in the sign structure. Also reports the total D_y cross-check is NOT computed here (D_y is #165's measured object); this isolates the piece the retained census does not separate. Exact integer weights (mu, gamma_V); logs only scale terms and enter no comparison. """ import sys, math, random from math import log def mobius(n): mu = [1] * (n + 1) isp = [True] * (n + 1) isp[0] = isp[1] = False for p in range(2, n + 1): if isp[p]: for j in range(p, n + 1, p): mu[j] = -mu[j] if j != p: isp[j] = False pp = p * p for j in range(pp, n + 1, pp): mu[j] = 0 return mu def primes_and_lambda(n): isc = [True] * (n + 1) isc[0] = isc[1] = False for p in range(2, int(n ** 0.5) + 1): if isc[p]: for j in range(p * p, n + 1, p): isc[j] = False # Lambda(m) = log(p) if m = p^k, else 0 lam = [0.0] * (n + 1) for p in range(2, n + 1): if isc[p]: pk = p lp = log(p) while pk <= n: lam[pk] = lp pk *= p return isc, lam def gamma_V(mu, V, n): # gamma_V(b) = sum_{k|b, k>V} mu(k) = [b=1] - sum_{k|b, k<=V} mu(k); sieve the latter. g = [0] * (n + 1) for k in range(1, V + 1): mk = mu[k] if mk == 0: continue for b in range(k, n + 1, k): g[b] -= mk g[1] = 0 # sum_{k|1, k>V} mu(k) = 0 for V >= 1 return g def tII_convolve(mu, gV, U, n): # tII[m] = sum_{a|m, a>U} mu[a] * gV[m//a] t = [0] * (n + 1) for a in range(U + 1, n + 1): ma = mu[a] if ma == 0: continue for m in range(a, n + 1, a): t[m] += ma * gV[m // a] return t def TII(x, eps_prime, mu, lam, gV, tII, rng=None): e0 = int(x ** (0.5 - eps_prime)) U = int(x ** (eps_prime / 3.0)) + 1 total = 0.0 terms = 0 for e in range(1, e0): if e % 2 == 0 or mu[e] == 0: continue # e odd, squarefree # m = ab in (x/(2e), x/e], (m, e)=1, n = e*m in (x/2, x] lo = x // (2 * e) + 1 hi = x // e for m in range(lo, hi + 1): if math.gcd(m, e) != 1: continue t = tII[m] if t == 0: continue n = e * m ln = lam[n - 2] if ln == 0.0: continue # sign shuffle on mu(a): tII already sums mu[a]; for control, rebuild per term is slow, # so the control is handled by recomputing tII with shuffled mu (see main). total += -t * log(m) * ln terms += 1 return total, terms, U, e0 def main(): x = int(sys.argv[1]) if len(sys.argv) > 1 else 2**18 eps_prime = 0.1 mu = mobius(x) isc, lam = primes_and_lambda(x) V = int(x ** (eps_prime / 3.0)) + 1 U = V gV = gamma_V(mu, V, x) tII = tII_convolve(mu, gV, U, x) real, terms, U_used, e0 = TII(x, eps_prime, mu, lam, gV, tII) # sign-shuffle control: mu[a] -> random +/-1 on squarefree a (seeded), 4 seeds ctrls = [] for seed in range(4): rng = random.Random(0x5EED + seed) mus = mu[:] # copy for a in range(2, x + 1): if mu[a] != 0: mus[a] = 1 if rng.random() < 0.5 else -1 gVs = gamma_V(mus, V, x) tIIs = tII_convolve(mus, gVs, U, x) c, _, _, _ = TII(x, eps_prime, mus, lam, gVs, tIIs) ctrls.append(c) rms = (sum(c * c for c in ctrls) / len(ctrls)) ** 0.5 print(f"x=2^{int(math.log2(x))}={x} eps'={eps_prime} U=V={U_used} e0={e0} terms={terms}") print(f" T_II^low/x = {real/x:+.6f}") print(f" control |T_II^low|/x (4 seeds) = {[f'{c/x:+.6f}' for c in ctrls]}") print(f" |real|/control_rms = {abs(real)/rms:.4f} (rms|x| = {rms/x:.6f})") print(f" |real|/x = {abs(real)/x:.6f}") if __name__ == "__main__": main()