'use strict'; // seam.js -- does brutePairs's cyclic-seam branch `if (prevDel) { pairs++; ... }` ever overcount? // It adds a pair whenever the LAST slot of the new tile is dead, without checking the FIRST. // Search small (W, S, p) with gcd(W,p)=1 for a case where last is dead and first is alive. function gcd(a,b){ while(b){[a,b]=[b,a%b];} return a; } // verbatim transcription of brutePairs from research/a3-08-adjacent-pairs.js function brutePairs(S, W, p) { const D = S.length; let pairs = 0, prevDel = 0, kills = 0, headRun = -1, run = 0; const runs = new Map(); const bump = (L) => runs.set(L, (runs.get(L) || 0) + 1); for (let k = 0; k < p; k++) { const off = k * W; for (let i = 0; i < D; i++) { const m = (S[i] + off) % p; const del = (m === 0 || m === p - 2) ? 1 : 0; 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); } else if (headRun) bump(headRun); return { pairs, kills, runs }; } // the same walk with the cyclic seam done correctly function truePairs(S, W, p) { const D = S.length, del = []; for (let k = 0; k < p; k++) for (let i = 0; i < D; i++) { const m = (S[i]+k*W)%p; del.push(m===0||m===p-2); } let pairs = 0; for (let j = 0; j < del.length; j++) if (del[j] && del[(j+1)%del.length]) pairs++; return pairs; } let found = 0; outer: for (let W = 6; W <= 40; W++) { for (const p of [5,7,11,13]) { if (gcd(W,p) !== 1) continue; for (let mask = 3; mask < (1<= 4) break outer; } } } } if (!found) console.log(' no case found in the search range'); console.log('\n Why it never fires on the project tiles:'); console.log(' W-1 satisfies (W-1) mod q = q-1 and (W+1) mod q = 1 for every q | W, so W-1 is in T_x for every x.'); console.log(' Hence the last slot of the pre-deletion new tile is (W-1)+(p-1)W = pW-1 = -1 (mod p),'); console.log(' and -1 is in {0,-2} (mod p) only if p | 1: prevDel is 0 at the seam for every fold of every T_x.');