// tiles.mjs — twin-slot tiles of the primorial levels, as gap/6 byte words. // T_x is the set of n < x# with gcd(n, x#) = gcd(n+2, x#) = 1, in increasing order, // read cyclically. Lifted out of capladder.mjs so the timing bench uses the same code. export const PRIMES_UPTO = { 5: [5], 7: [5, 7], 11: [5, 7, 11], 13: [5, 7, 11, 13], 17: [5, 7, 11, 13, 17], 19: [5, 7, 11, 13, 17, 19], 23: [5, 7, 11, 13, 17, 19, 23], 29: [5, 7, 11, 13, 17, 19, 23, 29], 31: [5, 7, 11, 13, 17, 19, 23, 29, 31], }; export function buildTile(topPrime) { let slots = [5], W = 6; const stats = {}; for (const p of PRIMES_UPTO[topPrime]) { const keep = []; for (let k = 0; k < p; k++) { const off = k * W; for (let i = 0; i < slots.length; i++) { const v = slots[i] + off; if (v % p === 0 || (v + 2) % p === 0) continue; keep.push(v); } } slots = keep; W *= p; stats[p] = { D: slots.length, W }; } return { slots: Float64Array.from(slots), W, stats }; } export function gap6Of(slots, W) { const D = slots.length, g6 = new Uint8Array(D); let gmax = 0, sum = 0; for (let i = 0; i < D; i++) { const g = (i + 1 < D ? slots[i + 1] : slots[0] + W) - slots[i]; if (g % 6 !== 0) throw new Error('gap not a multiple of 6'); if (g / 6 > 255) throw new Error('gap overflows a byte'); g6[i] = g / 6; if (g > gmax) gmax = g; sum += g; } return { g6, gmax, sum }; } // Fold T_x by p: p copies at offsets k*W, delete every slot whose value is 0 or // (p-2) mod p, emit the merged gap word. The emission follows #159's // direct_fold_hist: a kept slot emits the gap accumulated since the previous kept // slot, and the scan ends by emitting the carry plus the leading accumulation as // the wrap gap -- one entry per kept slot, in order. export function foldGap6(g6, W, p) { const D = g6.length, qm = new Uint8Array(256); for (let b = 0; b < 256; b++) qm[b] = (6 * b) % p; const out = new Uint8Array(D * (p - 2)); const wq = W % p; let n = 0, carry = 0, pre6 = 0, started = false; for (let k = 0; k < p; k++) { let r = (5 + ((k * wq) % p)) % p; for (let i = 0; i < D; i++) { const rr = r; r += qm[g6[i]]; if (r >= p) r -= p; if (rr !== 0 && rr !== p - 2) { if (started) out[n++] = carry; else { pre6 = carry; started = true; } carry = 0; } carry += g6[i]; } } if (n + 1 !== D * (p - 2)) throw new Error('fold count mismatch: ' + (n + 1) + ' vs ' + D * (p - 2)); out[n++] = carry + pre6; return out; }