"""Job #2656 (route 35 rev 13, pursue): the second moment of twin counts in intervals. Pre-registered here before the run: Data: twin pairs (n, n+2) by smaller member n from the chunked segmented sieve of #1029/#1297 over [10^6, 10^9). Intervals: for H in {2310, 30030, 510510} (= 11#, 13#, 17#) and each decade [10^d, 10^(d+1)), d = 6, 7, 8, the consecutive intervals [a, a+H) with a = multiples of H (aligned to the primorial period, the census control) and, separately, with a = H/2 + multiples of H (offset by half a period); only intervals fully inside the decade are used. Detrending: each interval's expected count is its own Hardy-Littlewood mean m_i = 2 C_2 int_a^(a+H) dt/ln^2 t, so the 1/ln^2 drift across a decade does not enter the variance; the measured dispersion is R = sum (N_i - m_i)^2 / sum m_i (variance-to-mean, Poisson = 1), with a bootstrap band (resampling intervals, 400 draws, seed 2656). Prediction (Hardy-Littlewood k-tuple conjecture for two twin pairs): E[N_i^2] - E[N_i] = sum over even h, 0 < |h| < H, of (H - |h|) S_4(h) / ln^4 X, where S_4(h) is the singular series of the 4-tuple {0, 2, h, h+2}: S_4(h) = 8 prod_{p > 2} (1 - nu_p(h)/p) / (1 - 1/p)^4 with nu_p = 4 generically, 2 if p | h, 3 if p | h - 2 or p | h + 2 (h = +-2 is the 3-tuple {0, 2, 4}, nu_p = 3 generically, 2 if p | 2? no: p > 2 never divides 2, so nu_p = 3 for p not dividing... handled explicitly: for h = +-2 the tuple is {0, 2, 4} and nu_p = min(3, p) with p = 3 giving nu_3 = 3, so S_4(+-2) = 0). Then the predicted ratio is R_HL = 1 + [sum_h (H - |h|) S_4(h) - H^2 (2 C_2)^2] / (ln^4 X) / (H 2 C_2 / ln^2 X), evaluated at X = the decade's geometric midpoint, together with its value at the decade ends (the prediction's own spread). The sum over h is computed once per H with exact prime factorisation of h (h - 2)(h + 2) by trial division and the Euler products truncated at p <= 10^6 with the analytic tail (relative error < 10^-6). Falsifier (fixed here): agreement, measured R inside the bootstrap band around R_HL, at >= 2 of 3 lengths in every decade, for the aligned intervals; the offset intervals are the control: their R must agree with the aligned R within the bands (no census effect on the variance). Controls: G0 pi_2(10^6) = 8169; the total twin count in [10^6, 10^9) against pi_2(10^9) - pi_2(10^6) = 3424506 - 8169. Stdout: ledger + JSON summary (deterministic); stderr: progress. RUN: python var2656.py --out var2656.json """ import argparse, json, math, sys, time import numpy as np from importlib.machinery import SourceFileLoader bg = SourceFileLoader("bg", "../job1936/job1936-blockgrain.py").load_module() C2 = 0.660161815846869573927812110014 HS = [2310, 30030, 510510]; DECADES = [6, 7, 8]; DRAWS, SEED = 400, 2656; PMAX_EULER = 10 ** 6 def f(t): return 1.0 / (math.log(t) ** 2) def hl_mean(a, b, n=64): h = (b - a) / n; s = f(a) + f(b) for i in range(1, n): s += f(a + i * h) * (4 if i % 2 else 2) return 2 * C2 * s * h / 3 def hl_means_vec(starts, H): # Simpson with 8 panels per interval, vectorised (intervals are short relative to their position) n = 8; xs = starts[:, None] + (H / n) * np.arange(n + 1)[None, :] w = np.array([1] + [4, 2] * (n // 2 - 1) + [4, 1], dtype=float) return 2 * C2 * (H / n) / 3 * (w[None, :] / np.log(xs) ** 2).sum(axis=1) def singular_sum(H, primes): """sum over even h, 0 < |h| < H, of (H - |h|) * S_4(h), with S_4 relative to (2 C_2)^2 computed exactly by the generic constant C4 = prod_{p>2} (1 - 4/p)/(1 - 2/p)^2 (p <= PMAX_EULER, tail analytic) times local corrections.""" # generic ratio S_4(h) / (2 C_2)^2 for h with no small-prime coincidences: factor 2 from p = 2 (8/4), times C4 # p = 2 and p = 3 are handled explicitly (nu_p = min(4, p) breaks the generic factor there); the generic product runs over p >= 5 logC4 = 0.0 for p in primes: if p <= 3: continue logC4 += math.log((1 - 4.0 / p) / (1 - 2.0 / p) ** 2) # tail: log((1-4/p)/(1-2/p)^2) ~ -4/p^2 ... sum_{p > P} -4/p^2 ~ -4 / (P ln P) logC4 += -4.0 / (PMAX_EULER * math.log(PMAX_EULER)) C4 = math.exp(logC4) # local corrections: p | h: (1 - 2/p)/(1 - 4/p); p | h +- 2: (1 - 3/p)/(1 - 4/p); h = +-2: S_4 = 0 (3 | one of 0, 2, 4) total = 0.0 hs = np.arange(2, H, 2) ratio = np.full(len(hs), 2.0 * C4) # even h: factor 2 from p = 2 (S_4 has 8, (2 C_2)^2 has 4) # p = 3: residues {0, 2, h, h+2} mod 3 cover all three classes unless h = 0 (mod 3); S_4's factor (1 - 2/3)/(1 - 1/3)^4 # relative to (2 C_2)^2's [(1 - 2/3)/(1 - 1/3)^2]^2 is 3 for h = 0 (mod 3) and 0 otherwise ratio *= np.where(hs % 3 == 0, 3.0, 0.0) # sieve-style factor application over primes p >= 5 up to H + 2 for p in primes: if p <= 3: continue if p > H + 2: break c_h = (1 - 2.0 / p) / (1 - 4.0 / p); c_pm = (1 - 3.0 / p) / (1 - 4.0 / p) # indices where p | h (h = 2k, k = 1..): h ≡ 0 mod p idx = np.arange(((-2) % (2 * p)) // 2, len(hs), p) # h = 2 + 2i ≡ 0 mod p -> i ≡ -1 mod p ratio[idx] *= c_h idx2 = np.arange((0) % p, len(hs), p) # h ≡ 2 mod p -> h - 2 ≡ 0: 2 + 2i ≡ 2 -> i ≡ 0 mod p ratio[idx2] *= c_pm idx3 = np.arange(((-4) % (2 * p)) // 2, len(hs), p) # h ≡ -2 mod p -> 2 + 2i ≡ -2 -> i ≡ -2 mod p ratio[idx3] *= c_pm # h = 2 (the 3-tuple {0, 2, 4}) is already 0 by the p = 3 rule weights = (H - hs).astype(float) total = 2.0 * float((weights * ratio).sum()) * (2 * C2) ** 2 # both signs of h; ratio is relative to (2 C_2)^2, so restore the absolute scale return total, C4 def main(): ap = argparse.ArgumentParser(); ap.add_argument("--out", required=True); ap.add_argument("--draws", type=int, default=DRAWS) a = ap.parse_args(); log = sys.stderr; t0 = time.time() ledger = [] def check(tag, ok, detail): ledger.append(f"{'PASS' if ok else 'FAIL'} {tag} :: {detail}") sp0 = bg.primes_to(int(math.isqrt(10 ** 6)) + 1); tw0 = bg.twins_in(sp0, 3, 10 ** 6 - 1) check("G0_pi2_1e6", len(tw0) == 8169, f"observed {len(tw0)} published 8169") lo, hi = 10 ** 6, 10 ** 9 sp = bg.primes_to(int(math.isqrt(hi + 2)) + 1) tpts = bg.twins_in(sp, lo, hi, log=log) check("G0b_pi2_1e9_minus_1e6", len(tpts) == 3424506 - 8169, f"observed {len(tpts)} published {3424506 - 8169} (twins with smaller member in [1e6, 1e9))") print(f"twins {len(tpts)} {time.time() - t0:.1f}s", file=log, flush=True) primes = [int(p) for p in bg.primes_to(PMAX_EULER)] out = {"instrument": "var2656.py", "job": 2656, "route": 35, "C2": C2, "preregistered": "see header", "lengths": {}, "ledger": ledger} rng = np.random.default_rng(SEED) for H in HS: ssum, C4 = singular_sum(H, primes) res = {"H": H, "C4_generic_ratio_constant": C4, "singular_sum_relative": ssum / (H * H * (2 * C2) ** 2), "decades": {}} print(f"H={H}: singular sum / (H^2 (2C2)^2) = {ssum / (H * H * (2 * C2) ** 2):.6f} {time.time() - t0:.1f}s", file=log, flush=True) for d in DECADES: D0, D1 = 10 ** d, 10 ** (d + 1) cell = {} for name, off in (("aligned", 0), ("offset", H // 2)): first = ((D0 - off + H - 1) // H) * H + off starts = np.arange(first, D1 - H + 1, H, dtype=np.int64) if len(starts) < 3: continue idx0 = np.searchsorted(tpts, starts); idx1 = np.searchsorted(tpts, starts + H) N = (idx1 - idx0).astype(float); m = hl_means_vec(starts.astype(float), H) R = float(((N - m) ** 2).sum() / m.sum()) boots = [] for _ in range(a.draws): j = rng.integers(0, len(N), len(N)); boots.append(float(((N[j] - m[j]) ** 2).sum() / m[j].sum())) boots = np.array(boots) cell[name] = {"intervals": int(len(N)), "mean_count": float(N.mean()), "mean_HL": float(m.mean()), "count_ratio": float(N.sum() / m.sum()), "R_var_over_mean": R, "R_boot_q05": float(np.quantile(boots, 0.05)), "R_boot_q95": float(np.quantile(boots, 0.95)), "R_boot_sd": float(boots.std())} # prediction at the decade midpoint and ends pred = {} for tag, X in (("mid", math.sqrt(D0 * D1)), ("lo", D0), ("hi", D1)): L2 = math.log(X) ** 2; mean = H * 2 * C2 / L2 var = mean + (ssum - H * H * (2 * C2) ** 2) / (L2 * L2) pred[tag] = var / mean cell["R_HL"] = pred # added, labelled: the prediction integrated over the aligned intervals themselves (each interval's own # 1/ln^4 and 1/ln^2 factors), instead of the decade-midpoint value al = cell.get("aligned") if al is not None: first = ((D0 + H - 1) // H) * H; st = np.arange(first, D1 - H + 1, H, dtype=np.int64).astype(float) Xi = st + H / 2.0; corr = (ssum - H * H * (2 * C2) ** 2) / np.log(Xi) ** 4 cell["R_HL_integrated_added"] = float(1 + corr.sum() / hl_means_vec(st, H).sum()) of = cell.get("offset") agree = al is not None and (al["R_boot_q05"] <= pred["mid"] <= al["R_boot_q95"]) ctrl = al is not None and of is not None and abs(al["R_var_over_mean"] - of["R_var_over_mean"]) <= 2 * math.hypot(al["R_boot_sd"], of["R_boot_sd"]) cell["agrees_with_HL"] = bool(agree); cell["aligned_offset_agree"] = bool(ctrl) res["decades"][str(d)] = cell if al: print(f" decade 1e{d}: n={al['intervals']} R_aligned={al['R_var_over_mean']:.4f} [{al['R_boot_q05']:.4f},{al['R_boot_q95']:.4f}] R_offset={of['R_var_over_mean'] if of else float('nan'):.4f} R_HL(mid)={pred['mid']:.4f} (lo {pred['lo']:.4f}, hi {pred['hi']:.4f}) R_HL(int)={cell['R_HL_integrated_added']:.4f} agree={agree} ctrl={ctrl}", file=log, flush=True) out["lengths"][str(H)] = res verdict = {} for d in DECADES: n_agree = sum(1 for H in HS if str(d) in out["lengths"][str(H)]["decades"] and out["lengths"][str(H)]["decades"][str(d)]["agrees_with_HL"]) n_cells = sum(1 for H in HS if str(d) in out["lengths"][str(H)]["decades"]) verdict[f"decade_1e{d}"] = {"cells": n_cells, "agree": n_agree, "falsifier_met": n_agree >= min(2, n_cells)} verdict["all_decades_met"] = all(v["falsifier_met"] for v in verdict.values() if isinstance(v, dict)) verdict["controls_aligned_offset_all_agree"] = all(c["aligned_offset_agree"] for H in HS for c in out["lengths"][str(H)]["decades"].values() if "offset" in c) out["verdict"] = verdict json.dump(out, open(a.out, "w"), indent=1) print("\n".join(ledger)); print("VERDICT " + json.dumps(verdict, sort_keys=True)) return 0 if __name__ == "__main__": sys.exit(main())