#!/usr/bin/env python3 """Fixed-endpoint discrepancy: an independent Python exercise of the SPLIT'S PIECES. Object (research/fixed-endpoint-discrepancy.md, served sha 19b6b12c...): x = 2^j, J = (x/2, x], f(n) = Lambda(n-2) mu(n), Lambda standard (prime powers in), M = sum_{n in J} f(n), F1 = sum_{n in J} f(n) log n, e_1 = floor(x^(1/2+eps)), e_0 = floor(x^(1/2-eps')), eps = eps' = 1/60 for the served note, D^(e_1) = sum_{eV} mu(k); TII(m) = sum_{a|m, a>U} mu(a) gamma_V(m/a). mu(m) = TII(m) - TI(m) + (m<=U)mu(m) + (m<=V)mu(m), and every m in I_e exceeds U, V, so P_low = tI + tII exactly with tI = sum_{e N: break comp[ip] = 1 spf[ip] = p if i % p == 0: mu[ip] = 0 break mu[ip] = -mu[i] # Lambda is supported exactly on the prime powers. The first version of this file filled # every multiple of p (then of p^2, ...), which leaves Lambda(6) = log 2; M came out 2.55x # too small and the whole decomposition was wrong. Caught by comparing M against the # served validator's own M and #165's published M/x = 0.014472 at j = 16. lam = [0.0] * (N + 1) for p in primes: q = p lp = math.log(p) while q <= N: lam[q] = lp q *= p return mu, lam, spf def phi_of(n, spf, memo): if n in memo: return memo[n] r, m = n, n while m > 1: p = spf[m] r = r // p * (p - 1) while m % p == 0: m //= p memo[n] = r return r def gcd(a, b): while b: a, b = b, a % b return a def measure(x, U, V, do_raw_D=False): """All the pieces at one x for one (U,V). Returns a dict of finite numbers.""" N = x mu, lam, spf = sieve(N) phimemo = {} lo, hi = x // 2 + 1, x M = 0.0 F1 = 0.0 for n in range(lo, hi + 1): fn = lam[n - 2] * mu[n] if fn: M += fn F1 += fn * math.log(n) e0 = int(math.floor(x ** (0.5 - EPS))) e1 = int(math.floor(x ** (0.5 + EPS))) assert 3 <= e0 < e1, (x, e0, e1) # S0, S1 over odd e < u (used for Q, and as the density projection) def means(u): s0 = s1 = 0.0 for e in range(1, u, 2): if mu[e]: pe = phi_of(e, spf, phimemo) s0 += mu[e] / pe s1 += mu[e] * math.log(e) / pe return s0, s1 def P(lo_e, hi_e): """P_R by the flipped arrangement (2.1): free of any (U,V).""" s = 0.0 for e in range(lo_e, hi_e): if e % 2 == 0 or mu[e] == 0: continue m_lo = x // (2 * e) m_hi = x // e for m in range(m_lo + 1, m_hi + 1): if mu[m] == 0 or lam[e * m - 2] == 0.0: continue if gcd(m, e) != 1: continue s -= mu[m] * math.log(m) * lam[e * m - 2] return s def Q(lo_e, hi_e): a0, a1 = means(lo_e) b0, b1 = means(hi_e) return M * (b1 - a1) - F1 * (b0 - a0) def D_raw(lo_e, hi_e): """D_R straight from the definition, by e-range and both bracket parts.""" s = 0.0 for e in range(lo_e, hi_e): if e % 2 == 0 or mu[e] == 0: continue pe = phi_of(e, spf, phimemo) for n in range(lo, hi + 1): fn = lam[n - 2] * mu[n] if fn: s += mu[e] * ((1 if n % e == 0 else 0) - 1.0 / pe) * fn * math.log(e / n) return s Plow = P(1, e0) Pband = P(e0, e1) Qlow = Q(1, e0) Qband = Q(e0, e1) Dlow = Plow - Qlow Dband = Pband - Qband Dall = P(1, e1) - Q(1, e1) checks = {} checks["split_P"] = abs((Plow + Pband) - P(1, e1)) <= 1e-9 * max(1.0, abs(P(1, e1))) checks["split_D"] = abs((Dlow + Dband) - Dall) <= 1e-9 * max(1.0, abs(Dall)) checks["P_eq_D_plus_Q_low"] = abs(Dlow - (Plow - Qlow)) == 0.0 if do_raw_D: raw = D_raw(1, e1) checks["raw_D_identity_%d" % x] = abs(raw - Dall) <= 1e-9 * max(1.0, abs(Dall)) # ---------------- Vaughan pieces of P_low ---------------- # m runs over I_e = (x/(2e), x/e], so at e = 1 it reaches x: the tables are over [1, x]. Mx = x c = [0.0] * (U * V + 1) for d in range(1, U + 1): if mu[d] == 0: continue for k in range(1, V + 1): if mu[k] == 0: continue c[d * k] += mu[d] * mu[k] TI = [0.0] * (Mx + 1) for r in range(1, U * V + 1): if c[r] == 0.0: continue for m in range(r, Mx + 1, r): TI[m] += c[r] gamma = [0.0] * (Mx + 1) for k in range(V + 1, Mx + 1): if mu[k]: for b in range(k, Mx + 1, k): gamma[b] += mu[k] TII = [0.0] * (Mx + 1) for a in range(U + 1, Mx + 1): if mu[a] == 0: continue ma = mu[a] for b in range(1, Mx // a + 1): g = gamma[b] if g: TII[a * b] += ma * g thr = max(U, V) tI = 0.0 tII = 0.0 for e in range(1, e0): if e % 2 == 0 or mu[e] == 0: continue m_lo = x // (2 * e) m_hi = x // e for m in range(m_lo + 1, m_hi + 1): L = lam[e * m - 2] if L == 0.0 or m <= thr: continue if gcd(m, e) != 1: continue w = math.log(m) * L tI += w * TI[m] tII -= w * TII[m] pieces_sum_ok = abs((tI + tII) - Plow) <= 1e-6 * max(1.0, abs(Plow)) def ratio(a, b): return (abs(a) / abs(b)) if b else float("inf") return { "x": x, "j": int(round(math.log2(x))), "e0": e0, "e1": e1, "U": U, "V": V, "note_cutoff_x_eps_over_3": int(math.floor(x ** (EPS / 3.0))), "M_over_x": M / x, "Dall_over_x": Dall / x, "Plow_over_x": Plow / x, "Pband_over_x": Pband / x, "tI_over_x": tI / x, "tII_over_x": tII / x, "Dlow_over_x": Dlow / x, "Dband_over_x": Dband / x, "TI_minus_TII_cancel_over_x": (tI + tII) / x, "abs_TI_over_Pband": ratio(tI, Pband), "abs_TI_over_Dall": ratio(tI, Dall), "abs_TI_plus_TII_over_TI": ratio(tI + tII, tI), "TI_times_TII_sign": (1 if tI * tII > 0 else (-1 if tI * tII < 0 else 0)), "checks": checks, "vaughan_sum_matches_Plow": pieces_sum_ok, } def main(): out = {"rows": [], "checks_failed": []} ladder = [j for j in range(12, JMAX + 1)] for j in ladder: x = 2 ** j for (U, V) in UV_SET: if x >= 2 ** 18 and (U, V) not in ((3, 3), (6, 6)): continue # keep the sweep affordable; 2^18+ at two cutoffs only if x >= 2 ** 19 and (U, V) != (3, 3): continue row = measure(x, U, V, do_raw_D=(x <= 2 ** 14 and (U, V) == (3, 3))) out["rows"].append(row) for k, v in row["checks"].items(): if not v: out["checks_failed"].append({"x": x, "U": U, "V": V, "check": k}) if not row["vaughan_sum_matches_Plow"]: out["checks_failed"].append({"x": x, "U": U, "V": V, "check": "vaughan_sum_matches_Plow"}) # the smallest x at which the NOTE's own cutoff U=V=floor(x^(eps/3)) reaches 2 print("x=2^%d done" % j, file=sys.stderr) out["note_cutoff_degeneracy"] = { "eps_prime": EPS, "cutoff_rule": "U = V = floor(x^(eps'/3)) per the served note", "U_is_1_through_j": max([j for j in range(8, 60) if int(math.floor((2 ** j) ** (EPS / 3.0))) < 2]), "first_j_with_U_at_least_2": min([j for j in range(8, 400) if int(math.floor((2 ** j) ** (EPS / 3.0))) >= 2]), } with open(sys.argv[1] if len(sys.argv) > 1 else "split-census.json", "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh, indent=1, sort_keys=True) print(json.dumps(out["note_cutoff_degeneracy"], indent=1)) print("rows:", len(out["rows"]), "failed checks:", len(out["checks_failed"])) return 0 if not out["checks_failed"] else 1 if __name__ == "__main__": sys.exit(main())