--- a3-08-adjacent-pairs.js 2026-09-10 06:51:07 +++ patched.js 2026-09-10 07:00:55 @@ -36,6 +36,28 @@ // folded with weight 2 on the p-divisible sizes: // // PAIRS(T, p) = 2 * sum_{d = 0 (mod p)} count(d) + sum_{d = +-2 (mod p)} count(d) +// +// THE TWO HYPOTHESES, which the paragraphs above use silently and which are +// load-bearing (both fail loudly, not marginally, when dropped): +// (H1) gcd(W, p) = 1, so that w = W mod p is invertible and r_i + kw does +// run over all of Z/p. Fold T_x by a q dividing W and w = 0: every copy +// carries the same residue, T_x has no slot at 0 or -2 mod q by +// construction, so the true count is 0 while the formula still returns +// the histogram sum. T11 by q = 3, 5, 7, 11: true 0, 0, 0, 0 against +// formula 270, 126, 86, 10. The kill law 2D goes with it (kills = 0). +// (H2) p does not divide 4, so that 0 and -2 are distinct (else the deleted +// set is a 1-set, not a 2-set) and +2 and -2 are distinct (else the two +// off-diagonal transfers coincide). At p = 2 the formula returns twice +// the truth, at p = 4 half of it. Neither is reachable from this tile +// family, since 2 and 3 divide every W; p = 3 is fine, {0,-2} = {0,1}. +// Under (H1) and (H2) nothing else is used: the identity holds for ANY cyclic +// set S in Z/W, p need not be prime and need not be the next prime. Checked on +// 271 random (W, p, S), on composite p = 49, 77, 91, 119, 121, 143, 169 against +// T5, and by a criterion-free position walk over all 31 * 214,708,725 slots at +// fold 31 (8025014, agreeing with PREDICT, E2 and the kill graph). +// Every call site in this file satisfies both: pairTable pairs each tile with +// its next prime, the [8]/[8b] sweeps carry `if (x >= p) continue`, and +// streamAnalyse is only ever called with increasing primes. // // THE QUALIFYING SET IN CLOSED FORM. Grain gaps are multiples of 6, so d must // solve a pair of congruences mod 6p. Three progressions, no more: @@ -104,8 +126,13 @@ } // --------------------------------------------------- the closed-form set ---- -// aP: the unique residue mod 6p that is 2 mod p and 0 mod 6. -function aPlus(p) { let a = 2; while (a % 6 !== 0) a += p; return a; } +// aP: the unique residue mod 6p that is 2 mod p and 0 mod 6. It exists only +// when gcd(p, 6) = 1; for p = 3 the loop below never terminates (a runs +// 2, 5, 8, 11, ... and is never 0 mod 6), so refuse rather than hang. +function aPlus(p) { + if (p % 2 === 0 || p % 3 === 0) throw new Error(`aPlus: needs gcd(p,6)=1, got p=${p}`); + let a = 2; while (a % 6 !== 0) a += p; return a; +} function qualifyingSet(p, G2) { // [{d, weight}] with d <= G2 const out = [], six = 6 * p, aP = aPlus(p), aM = six - aP; @@ -131,7 +158,7 @@ // count consecutive deleted pairs, cyclically. Uses no gap criterion at all. function brutePairs(t, p) { const S = t.S, D = S.length, W = t.W; - let pairs = 0, prevDel = 0, kills = 0, headRun = -1, run = 0; + let pairs = 0, prevDel = 0, kills = 0, headRun = -1, run = 0, firstDel = 0; const runs = new Map(); const bump = (L) => runs.set(L, (runs.get(L) || 0) + 1); for (let k = 0; k < p; k++) { @@ -139,13 +166,20 @@ for (let i = 0; i < D; i++) { const m = (S[i] + off) % p; const del = (m === 0 || m === p - 2) ? 1 : 0; + if (k === 0 && i === 0) firstDel = del; if (del) { kills++; run++; if (prevDel) pairs++; } else { if (headRun < 0) headRun = run; else if (run) bump(run); run = 0; } prevDel = del; } } if (headRun < 0) { runs.set(run, 1); return { pairs: pairs + 1, kills, runs }; } - if (prevDel) { pairs++; bump(run + headRun); } // cyclic seam of the NEW tile + // Cyclic seam of the NEW tile. The pair (last slot, first slot) is a double + // kill only if BOTH are dead; the run merge only needs the last one dead. + // On T_x the branch is unreachable, because W-1 is in T_x for every x (it is + // -1 mod every q | W and W+1 is 1 mod every q), so the last slot of the new + // tile is pW-1 = -1 (mod p) and never dies. Off this tile family it fires: + // W = 6, p = 5, S = [1,4] gave 3 for the true 2 before the firstDel guard. + if (prevDel) { if (firstDel) pairs++; bump(run + headRun); } else if (headRun) bump(headRun); return { pairs, kills, runs }; }