#!/usr/bin/env node // ============================================================================ // JOB #630 — the zone window as ONE translate of the tile word, against the // EXACT translate distribution (mean, sd, percentile) of the same window. // // WHY. Row 53's Lemma A (Q-zonegap-reduction, zonegap-02-reduction.md §1) makes // the zone's candidate openers exactly the tile slots: // { a : p < a, a+2 < p'^2, gcd(a(a+2), p#) = 1 } = { a : p < a, a+2 < p'^2, a,a+2 prime }, // and the zone's candidate positions are n = p'^2 - p - 3 CONSECUTIVE integers. // So the zone's occupancy is the number of slots of the cyclic tile word // (period W = p#) in a window of length n starting at offset p+1. The // translate ensemble -- the same window slid to every offset mod W -- has the // EXACT mean mu = n*A/W, A = prod_{q=3..p}(q-2), and the EXACT variance from // the pair correlation // rho_2(d) = #{(r mod W) : r,r+2 slots and r+d,r+d+2 slots} // = prod_{q<=p} c_q(d), c_q(d) = #{r mod q : r(r+2)(r+d)(r+d+2) != 0} // Var = sum_{|d| { let q = p + 1; while (!isP[q]) q++; return q; }; const primorial = (p) => { let W = 1n; for (const q of PR) { if (q > p) break; W *= BigInt(q); } return W; }; const Aof = (p) => { let A = 1n; for (const q of PR) { if (q > p) break; if (q >= 3) A *= BigInt(q - 2); } return A; }; // ---- PART A ----------------------------------------------------------------- function partA() { const out = []; for (const p of PR) { if (p > A_LIM || p < 3) continue; const pp = nextP(p), Wb = primorial(p); if (Wb > 400000000n) continue; // memory guard: W bytes for the slot array const w = Number(Wb), pp2 = pp * pp, n = pp2 - p - 3; if (n + n > w) { out.push({ p, skipped: `n=${n} > W/2` }); continue; } // slot indicator by masking multiples of each q <= p, on r and on r+2 const slot = new Uint8Array(w).fill(1); for (const q of PR) { if (q > p) break; for (let j = 0; j < w; j += q) slot[j] = 0; for (let j = (q - 2) % q; j < w; j += q) slot[j] = 0; } const zoneOffset = (p + 1) % w; // sliding window over the doubled word let cur = 0; for (let i = 0; i < n; i++) cur += slot[i]; let sum = 0, sum2 = 0, min = Infinity, max = -1, zoneCount = null; const hist = new Map(); for (let t = 0; t < w; t++) { if (t > 0) { cur += slot[(t + n - 1) % w] - slot[t - 1]; } sum += cur; sum2 += cur * cur; if (cur < min) min = cur; if (cur > max) max = cur; hist.set(cur, (hist.get(cur) || 0) + 1); if (t === zoneOffset) zoneCount = cur; } const mean = sum / w, varr = sum2 / w - mean * mean; let below = 0; for (const [c, k] of hist) if (c < zoneCount) below += k; const atOrBelow = below + (hist.get(zoneCount) || 0); out.push({ p, pp, W: w, n, zoneCount, mean, sd: Math.sqrt(varr), min, max, percentile: 100 * atOrBelow / w, strict: 100 * below / w, A: Number(Aof(p)), closedMu: n * Number(Aof(p)) / w, hist: [...hist.entries()].sort((a, b) => a[0] - b[0]), }); process.stderr.write(`PART A p=${p} W=${w} n=${n} zone=${zoneCount} mean=${mean.toFixed(4)} sd=${Math.sqrt(varr).toFixed(4)} min=${min} max=${max} closedMu=${(n * Number(Aof(p)) / w).toFixed(4)}\n`); } return out; } // ---- PART B: closed-form mu and Var from c_q tables ------------------------- function cqTables(p) { const T = []; for (const q of PR) { if (q > p) break; const t = new Uint16Array(q); // c_q(d) can be as large as q-2 > 255: Uint8 overflows for (let d = 0; d < q; d++) { let c = 0; for (let r = 0; r < q; r++) { const a = r % q, b = (r + 2) % q, cc = (r + d) % q, e = (r + d + 2) % q; if (a && b && cc && e) c++; } t[d] = c; } T.push([q, t]); } return T; } // Stable form. With density = A/W and G(d) = prod_{q<=p} c_q(d)*q/a_q^2 // (a_q = q-2 for odd q, a_2 = 1), one has rho_2(d)/W = density^2 * G(d), so // Var = density^2 * sum_{|d| 0 ? Math.sqrt(varr) : 0; return { p, pp, n, mu, sd, var: varr }; } // ---- exact zone count by direct sieve -------------------------------------- function zoneCount(p) { const pp = nextP(p), lim = pp * pp; const comp = new Uint8Array(lim + 1); comp[0] = comp[1] = 1; for (let i = 2; i * i <= lim; i++) if (!comp[i]) for (let j = i * i; j <= lim; j += i) comp[j] = 1; let c = 0; for (let a = p + 1; a + 2 < lim; a++) if (!comp[a] && !comp[a + 2]) c++; return c; } // ---- run -------------------------------------------------------------------- const A = partA(); // validate the closed form against the exact enumeration where both exist for (const r of A) { if (r.skipped) continue; const m = muSig(r.p); process.stderr.write(`VALIDATE p=${r.p}: enum mean ${r.mean.toFixed(4)} vs closed ${m.mu.toFixed(4)}; ` + `enum sd ${r.sd.toFixed(4)} vs closed ${m.sd.toFixed(4)}\n`); } const B = []; for (const p of PR) { if (p > PLIM || p < 3) continue; const m = muSig(p), C = zoneCount(p); B.push({ ...m, C, z: m.sd > 0 ? (C - m.mu) / m.sd : 0 }); } process.stderr.write('PART B done\n'); console.log('JOB #630 — the zone window versus the exact translate distribution of the tile'); console.log(''); console.log('PART A — every translate of the window enumerated exactly (p <= 19)'); console.log(' p W n zone mean sd min max zone pct'); for (const r of A) { if (r.skipped) { console.log(` ${String(r.p).padStart(2)} skipped (${r.skipped})`); continue; } console.log(` ${String(r.p).padStart(2)} ${String(r.W).padStart(8)} ${String(r.n).padStart(5)} ` + `${String(r.zoneCount).padStart(5)} ${r.mean.toFixed(4).padStart(8)} ${r.sd.toFixed(4).padStart(7)} ` + `${String(r.min).padStart(4)} ${String(r.max).padStart(4)} ${r.percentile.toFixed(2).padStart(8)}%`); } console.log(''); console.log(' exact distribution over translates, count:multiplicity'); for (const r of A) { if (r.skipped) continue; console.log(` p=${String(r.p).padStart(2)} ` + r.hist.map(([c, k]) => `${c}:${k}`).join(' ')); } console.log(''); console.log('PART B — closed-form mu/sd versus the exact zone count, every prime p <= ' + PLIM); console.log(' p n C(p) mu sd z'); for (const r of B) { console.log(` ${String(r.p).padStart(3)} ${String(r.n).padStart(6)} ${String(r.C).padStart(6)} ` + `${r.mu.toFixed(4).padStart(8)} ${r.sd.toFixed(4).padStart(8)} ${r.z.toFixed(3).padStart(8)}`); } const zs = B.map((r) => Math.abs(r.z)); const ratios = B.map((r) => r.C / r.mu); const vare = B.map((r) => r.var / r.mu); // Var/E : row 17's sub-Poisson ratio const sdOverSqrtMu = B.map((r) => r.sd / Math.sqrt(r.mu)); const npos = B.filter((r) => r.z >= 0).length, N = B.length; // one-sided binomial tail P(X >= npos) with q = 1/2 (exact, small N) let binom = 0; for (let k = npos; k <= N; k++) { let c = 1; for (let i = 1; i <= k; i++) c = c * (N - i + 1) / i; binom += c; } binom = binom / Math.pow(2, N); const zmin = B.reduce((a, r) => (r.z < a.z ? r : a), B[0]); const zmax = B.reduce((a, r) => (r.z > a.z ? r : a), B[0]); console.log(''); console.log('SUMMARY'); console.log(` levels ${N}; |z| max ${Math.max(...zs).toFixed(3)}, mean ${(zs.reduce((a, b) => a + b, 0) / N).toFixed(3)}`); console.log(` z >= 0 at ${npos}/${N} (${(100 * npos / N).toFixed(1)}%), one-sided binomial tail ${binom.toExponential(3)}`); console.log(` z extremes: min ${zmin.z.toFixed(3)} at p=${zmin.p}, max ${zmax.z.toFixed(3)} at p=${zmax.p}`); console.log(` C(p) min ${Math.min(...B.map((r) => r.C))}; C/mu in [${Math.min(...ratios).toFixed(4)}, ${Math.max(...ratios).toFixed(4)}]`); console.log(` sd/sqrt(mu) in [${Math.min(...sdOverSqrtMu).toFixed(3)}, ${Math.max(...sdOverSqrtMu).toFixed(3)}] (1 = Poisson)`); console.log(` Var/E in [${Math.min(...vare).toFixed(3)}, ${Math.max(...vare).toFixed(3)}] (row 17's diagonal family: 0.152..0.396)`); console.log(` mu(p) in [${Math.min(...B.map((r) => r.mu)).toFixed(2)}, ${Math.max(...B.map((r) => r.mu)).toFixed(2)}]`);