#!/usr/bin/env node 'use strict'; // ============================================================================ // nq_tile.js -- for one twin-slot tile T_x, count the QUALIFYING GAPS per fold // and the smallest qualifying gap value at each fold. // // qualifies(g, q) <=> g % q in {0, 2, q-2} (the served producer's // research/attack-foldL-03-transport.js) // nq(x, q) = #{ i : qualifies(g_i, q) } // minQ(x, q) = min { g_i : qualifies(g_i, q) } (null if nq = 0) // // Why: L(T_x, q) = 1 exactly when no two adjacent slots are both deleted, which // is the same as nq(x, q) = 0, and then Q_L(theta) = 0 for every L >= 2, so the // transport inequality's loose and alternation-refined forms are the SAME // function of theta. The record's closed form (research/kappa-not-L.md) says // the smallest qualifying value is 2q-2 (q = 1 mod 6) or 2q+2 (q = 5 mod 6); // hence 2q - 2 > G2(T_x) should force nq = 0. // // The tile is built by the SERVED engine (research/scanstat-t37-01-engine.js), // so D and G2 must reproduce the record's ladder -- that is the self-test. // // Usage: node nq_tile.js [outFile] // Progress/timing: stderr. stdout: one deterministic JSON object. // ============================================================================ const fs = require('fs'); const path = require('path'); const ENG = process.env.T37_ENGINE || path.resolve(process.cwd(), 'research', 'scanstat-t37-01-engine.js'); const E = require(ENG); const x = parseInt(process.argv[2], 10); const QMAX = parseInt(process.argv[3], 10) || 200; const outFile = process.argv[4]; // primes > x up to QMAX (gcd(W,q)=1 is exactly q > x for a primorial W) function primesUpTo(n) { const s = new Uint8Array(n + 1); const out = []; for (let i = 2; i <= n; i++) { if (s[i]) continue; out.push(i); for (let j = i * i; j <= n; j += i) s[j] = 1; } return out; } const QS = primesUpTo(QMAX).filter(q => q > x); const nq = new Float64Array(QS.length); const minQ = new Float64Array(QS.length).fill(Infinity); const { Dn, Wn } = E.tileDW(x); const WHEEL = E.WHEEL; const OFF = E.wheelOffsets(), NW = OFF.length; const nBlocksTotal = Wn / WHEEL; const tabs = E.sieveTables(x, OFF); // The served engine drives blocks of 30030 = 2*3*5*7*11*13, so it serves x >= 13. // For the small tiles (x = 5, 7, 11) the same object is built by direct // enumeration of the twin residues mod x#, which is exact and cheap at W <= 2310. let nGap = 0, G2 = 0, G2pos = -1; const smallX = !Number.isInteger(nBlocksTotal); if (smallX) { const ps = primesUpTo(x).filter(p => p > 2); const slots = []; for (let n = 1; n < Wn; n += 2) { let ok = true; for (const p of ps) { if (n % p === 0 || (n + 2) % p === 0) { ok = false; break; } } if (ok) slots.push(n); } if (slots.length !== Dn) throw new Error(`small-x D mismatch ${slots.length} != ${Dn}`); const gapList = slots.map((v, i) => (i + 1 < slots.length ? slots[i + 1] : slots[0] + Wn) - v); for (const g of gapList) { if (g > G2) G2 = g; for (let qi = 0; qi < QS.length; qi++) { const q = QS[qi], r = g % q; if (r === 0 || r === 2 || r === q - 2) { nq[qi]++; if (g < minQ[qi]) minQ[qi] = g; } } } const outSmall = { x, qmax: QMAX, D: gapList.length, G2, G2pos_mod_W: -1, path: 'direct-enumeration', primes: QS, nq: Array.from(nq), minQ: Array.from(minQ, v => (v === Infinity ? null : v)), gate: { D_ok: gapList.length === Dn, D_want: String(Dn) } }; const text = JSON.stringify(outSmall); if (outFile) fs.writeFileSync(outFile, text); else process.stdout.write(text + '\n'); process.stderr.write(` x=${x}: DONE (small-x path) D=${gapList.length} G2=${G2}\n`); process.exit(0); } const cand = new Uint8Array(NW); const cls = new Int32Array(tabs.length); for (let ti = 0; ti < tabs.length; ti++) cls[ti] = (0 % tabs[ti].p) * tabs[ti].step % tabs[ti].p; let p1 = NaN, p2 = NaN; function feed(s) { if (!Number.isNaN(p2) && nGap < Dn) { // each cyclic gap is counted exactly once const g = s - p2; // the gap that ENDS at p2 nGap++; if (g > G2) { G2 = g; G2pos = p1; } for (let qi = 0; qi < QS.length; qi++) { const q = QS[qi], r = g % q; if (r === 0 || r === 2 || r === q - 2) { nq[qi]++; if (g < minQ[qi]) minQ[qi] = g; } } } p1 = p2; p2 = s; } const t0 = Date.now(); for (let bb = 0; bb <= nBlocksTotal; bb++) { let b = bb, off = 0; if (b >= nBlocksTotal) { b -= nBlocksTotal; off = Wn; } cand.fill(0); for (let ti = 0; ti < tabs.length; ti++) { const tb = tabs[ti], c = cls[ti], st = tb.start, ls = tb.list; for (let q = st[c]; q < st[c + 1]; q++) cand[ls[q]] = 1; let nc = c + tb.step; if (nc >= tb.p) nc -= tb.p; cls[ti] = nc; } const bp = b * WHEEL + off; for (let w = 0; w < NW; w++) if (cand[w] === 0) feed(bp + OFF[w]); if ((bb & 8191) === 0) process.stderr.write(` x=${x}: ${((bb / (nBlocksTotal + 1)) * 100).toFixed(1)}% [${((Date.now() - t0) / 1000).toFixed(0)}s]\n`); } const out = { x, qmax: QMAX, D: nGap, G2, G2pos_mod_W: G2pos < 0 ? -1 : G2pos % Wn, primes: QS, nq: Array.from(nq, v => v), minQ: Array.from(minQ, v => (v === Infinity ? null : v)), gate: { D_ok: nGap === Dn, D_want: String(Dn) } }; const s = JSON.stringify(out); if (outFile) { fs.writeFileSync(outFile, s); process.stderr.write(` x=${x}: wrote ${outFile}\n`); } else process.stdout.write(s + '\n'); process.stderr.write(` x=${x}: DONE D=${nGap} G2=${G2} in ${((Date.now() - t0) / 1000).toFixed(0)}s\n`);