#!/usr/bin/env python3 """Independent check of the A/B side-split numbers in ab-split-4480.js. Deliberately a SECOND implementation: pure Python, stdlib only, no numpy, no shared code with the JavaScript producer, its own sieve and its own root-finder. It re-derives, from the raw counts, only claims that need no main term at all (the split needs none): G1 the B8 anchor count is 491 and sum T over B8 is 280128 (the served producer's own column); G2 (withdrawn: X <= T is NOT an invariant, the served file only asserts X[0] <= T - 1 for the handful of anchors on its K = 0 list); G3 at the crossing depth u*, the coupling factor Xind/X at B8 (the number the report headlines); G4 the per-anchor absolute and signed side asymmetries at B8, at u*. The conventions are the served producer's: openers a = 11, 17, 29 (mod 30); iA / iB the 1-based index of the smallest active prime (>= 7) dividing a / a+2, 0 when none; K the largest active index with ACT[K-1] <= (Q^2)^(1/u). Coverage is B8 (the top band, 491 anchors) rather than all 1,227, so the script stays under a minute; every headline number of the return lives in B8. python3 check4480.py exit 0 iff every check passes """ import sys sys.stdout.reconfigure(newline="\n", encoding="utf-8", errors="replace") PLIM = 31700 QMAX = 10007 USTAR = 3.565847 # the served producer's crossing depth; cited, not re-derived OPENERS = (11, 17, 29) composite = bytearray(PLIM + 1) composite[0] = composite[1] = 1 p = 2 while p * p <= PLIM: if not composite[p]: composite[p * p::p] = b"\x01" * len(range(p * p, PLIM + 1, p)) p += 1 PRIMES = [n for n in range(2, PLIM + 1) if not composite[n]] ACT = [q for q in PRIMES if q >= 7] ANCHORS = [q for q in ACT if q <= QMAX] IDX = {q: i for i, q in enumerate(ANCHORS)} B8 = [q for q in ANCHORS if 5624 <= q <= 10007] NEXT = {} for q in B8: i = IDX[q] NEXT[q] = ANCHORS[i + 1] if i + 1 < len(ANCHORS) else q + 2 def kat_u(q, u): y = (q * q) ** (1.0 / u) k = 0 while k < len(ACT) and ACT[k] <= y: k += 1 return k def anchor_counts(q, qp): """Returns (C, T, X, NA, NB) at depth u*, singly sieved with the window's own least-prime map.""" lo, hi = q * q, qp * qp width = hi - lo nR = IDX[q] + 1 lpf = [0] * (width + 6) for ri in range(nR): r = ACT[ri] for v in range(((lo + r - 1) // r) * r, hi + 5, r): if lpf[v - lo] == 0: lpf[v - lo] = ri + 1 K = kat_u(q, USTAR) C = T = X = NA = NB = 0 # a + 2 < hi, exactly as the served producer's loop condition: a = hi - 2 reads a + 2 = hi, # which the sieve never marks (Qp > Q is not among the marking primes), so including it # invented one extra a per anchor. That was this checker's own bug: it inflated sum T over B8 # by 92 against both the served producer and the JavaScript producer. for a in range(lo, hi - 2): if a % 30 not in OPENERS: continue C += 1 iA = lpf[a - lo] iB = lpf[a + 2 - lo] # the census's own event: iA > K requires iA > 0, so a coprime to ACT is in neither # marginal. (Including iA == 0 here was the bug this checker was written to find.) roughA = (iA > K) roughB = (iB > K) if iA == 0 and iB == 0: T += 1 if roughA: NA += 1 if roughB: NB += 1 if roughA and roughB: X += 1 return C, T, X, NA, NB def main() -> int: fails = [] n = len(B8) if n != 491: fails.append("G1 B8 anchor count is %d, expected 491" % n) sT = sX = 0.0 sXind = 0.0 sNA = 0 sAbs = 0 sSig = 0 for i, q in enumerate(B8): C, T, X, NA, NB = anchor_counts(q, NEXT[q]) sT += T sX += X sNA += NA sXind += NA * NB / C sAbs += abs(NA - NB) sSig += NA - NB if i % 50 == 0: print(" ... %d/%d anchors" % (i, n), flush=True) if sT != 280128: fails.append("G1 sum T over B8 is %d, the served producer's column says 280128" % sT) coupling = sXind / sX absAsym = sAbs / sNA sigAsym = sSig / sNA print(" B8, u* = %.6f: anchors %d sumT %d sumX %d" % (USTAR, n, sT, sX)) print(" Xind/X (the coupling factor) = %.4f [JS producer: 1.0653 = 1/0.9387]" % coupling) print(" <|NA-NB|>/ = %.6f [JS producer: 0.0113]" % absAsym) print(" / = %+.6f [JS producer: +0.00056]" % sigAsym) if not (1.055 <= coupling <= 1.075): fails.append("G3 coupling %.4f outside [1.055, 1.075]" % coupling) if not (0.010 <= absAsym <= 0.013): fails.append("G4 absolute asymmetry %.6f outside [0.010, 0.013]" % absAsym) if abs(sigAsym) > 0.0008: fails.append("G4 signed asymmetry %+.6f larger than 0.0008 in magnitude" % sigAsym) for f in fails: print("FAIL: " + f) print("CHECKS PASSED" if not fails else "CHECKS FAILED: %d" % len(fails)) return 0 if not fails else 1 if __name__ == "__main__": raise SystemExit(main())