An AI skill that turns books into animated, narrated, interactive web experiences
JavaScript ai-agentsai-educationanimationclaudeclaude-codeclaude-skillsedtecheducationinteractive-textbookscormtext-to-video
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How Much Work Is Elimination?</title>
<link rel="stylesheet" href="../lib/engine.css">
<link rel="expect" href="#bar" blocking="render"><!-- hold the first frame until the player exists, so the page transition can run -->
</head>
<body>
<script src="../lib/engine.js"></script>
<script src="../lib/math.js"></script>
<script src="../lib/cost.js"></script>
<script src="../lib/play.js"></script>
<script src="audio/en/timings.js"></script>
<script>
'use strict';
const CHAPTER = { number: 33, title: 'How Much Work Is Elimination?', minutes: 9, language: 'en', next: '../ch34/' };
/* ---------- chapter helpers ---------- */
const A2 = [[2, 1], [8, 7]]; // chapter 30's matrix (OCW 18.06, lecture 4)
const A3 = [[2, 1, 1], [4, 3, 3], [8, 7, 9]]; // pivots 2, 1, 2
const CHART = { x: 1090, y: 770, w: 440, h: 470 };
const say = (p, parts, x, y, t0, { size = 40, fill = COL.chalk, anchor = 'start' } = {}) => { const e = M(p, [].concat(parts), { x, y, size, fill, anchor, o: 0 }); show(e, t0); return e; };
const text = (p, str, x, y, t0, { size = 30, fill = COL.chalk, anchor = 'start', weight = 500 } = {}) => { const e = T(p, str, { x, y, size, fill, anchor, weight, o: 0 }); show(e, t0); return e; };
const title = (p, str) => text(p, str, 60, 110, .2, { size: 40, weight: 600 });
// hide the readout lines a beat does not use yet
const quietRead = (ce, keep = ['n', 'ops']) => ['n', 'ops', 'third', 'ratio'].forEach(k => { if (!keep.includes(k)) put(ce.parts.read[k], { o: 0 }); });
// operations c0 + 1 … c1 one at a time, spread evenly from t0 to t1
const steps = (ce, c0, c1, t0, t1) => { for (let c = c0 + 1; c <= c1; c++) ce.to({ c }, t0 + (t1 - t0) * (c - c0 - 1) / (c1 - c0), .25, lin); };
// a glow behind the dashed curve of the chart
// a soft box over cells [i, j] of a cost grid, for a moment (the cells the voice is naming)
const cellGlow = (p, { x, y, w }, n, cells, at, len = 2) => {
const cs = w / n, g = G(p, { o: 0 });
cells.forEach(([i, j]) => mk('rect', { x: x + j * cs + 3, y: y + i * cs + 3, width: cs - 6, height: cs - 6, rx: 8, fill: 'none', stroke: COL.chalk, 'stroke-width': 4 }, g));
show(g, at, .25); hide(g, at + len, .4);
return g;
};
// [U | b] as cells (j = n is b); on(cells, at) lights cells like '13' (row 1, column 3) for a moment; count([[at, v]]) sets the counter
const ubGrid = (p, U, x0, y0, cs, t0, { blank = false, cy = 590 } = {}) => {
const n = U.length, cell = (i, j) => [x0 + j * cs + (j === n ? 24 : 0), y0 + i * cs];
const g = G(p, { o: 0 }); show(g, t0, .5);
const lit = {};
for (let i = 0; i < n; i++) for (let j = 0; j <= n; j++) {
const [x, y] = cell(i, j);
mk('rect', { x, y, width: cs, height: cs, fill: COL.chalk, 'fill-opacity': j === n ? .14 : (i > j ? .03 : .08), stroke: COL.faint, 'stroke-width': 1.5 }, g);
const hl = mk('rect', { x: x + 4, y: y + 4, width: cs - 8, height: cs - 8, rx: 8, fill: COL.chalk, 'fill-opacity': .5 }, g);
put(hl, { o: 0 }); lit[`${i}${j}`] = hl;
const lab = j === n ? [`b${'₁₂₃₄'[i]}`] : (blank && i <= j ? null : [num(U[i][j])]);
if (lab) M(g, lab, { x: x + cs / 2, y: y + cs / 2 + 13, size: 36, fill: j === n ? COL.task : (i > j ? COL.faint : COL.chalk), anchor: 'middle' });
}
path(g, `M${x0 + n * cs + 12} ${y0 - 6}V${y0 + n * cs + 6}`, { stroke: COL.dim, 'stroke-width': 2.5, 'stroke-dasharray': '6 6' });
const on = (cells, at, len = 2.2) => cells.forEach(k => { tw(lit[k], { o: 1 }, at, .25); tw(lit[k], { o: .35 }, at + len, .5); });
const cnt = G(p, { o: 0 }); show(cnt, t0, .4);
T(cnt, 'operations on b:', { x: x0, y: cy, size: 30, weight: 600 });
const cv = G(cnt, {});
let shown = -1;
const count = list => [[0, 0], ...list].forEach(([t, v]) => prog(q => { if (q > 0 && shown !== v) { shown = v; cv.replaceChildren(); T(cv, String(v), { x: x0 + 260, y: cy, size: 34, fill: COL.task, weight: 700 }); } }, t, 0, lin));
return { on, count };
};
const curveFlash = (ce, at) => { tw(ce.parts.curve, { 'a_stroke-width': 7 }, at, .3); tw(ce.parts.curve, { 'a_stroke-width': 3 }, at + 1.4, .5); };
/* ---------- practice data (every count done twice: step by step, and by (n³ − n)/3) ---------- */
const STAGEQ = [
{ parts: ['5 × 5, step 1: ', { box: '4' }, ' rows × ', { box: '5' }, ' numbers = ', { box: '20' }], why: 'Four rows below the first pivot, five numbers in each.', hint: 'Every row but the pivot row changes; every number in it changes.' },
{ parts: ['6 × 6, step 1: ', { box: '30' }, ' operations'], why: '5 rows × 6 numbers = 30.', hint: 'Rows below the pivot, times numbers in a row.' },
{ parts: ['6 × 6, step 3: the corner is ', { box: '4' }, ' × 4, so ', { box: '12' }, ' operations'], why: 'After two steps a 4 × 4 corner is left: 3 rows × 4 numbers = 12.', hint: 'Each step leaves a corner one smaller.' },
{ parts: ['7 × 7, the last step: ', { box: '2' }, ' operations'], why: 'The last corner is 2 × 2: 1 row × 2 numbers.', hint: 'The last step works on a 2 × 2 corner.' },
{ parts: ['10 × 10, step 1: ', { box: '90' }, ' operations'], why: '9 rows × 10 numbers.', hint: '(k − 1) × k with k = 10.' },
];
const TOTALQ = [
{ parts: ['3 × 3: ', { box: '8' }], why: '6 + 2 = 8; (27 − 3)/3 = 8.', hint: 'Steps of 3 × 2 and 2 × 1.' },
{ parts: ['5 × 5: ', { box: '40' }], why: '20 + 12 + 6 + 2 = 40; (125 − 5)/3 = 40.', hint: 'Add the steps, or use (n³ − n)/3.' },
{ parts: ['6 × 6: ', { box: '70' }], why: '30 + 20 + 12 + 6 + 2 = 70; (216 − 6)/3 = 70.', hint: '6³ = 216.' },
{ parts: ['7 × 7: ', { box: '112' }], why: '(343 − 7)/3 = 336/3 = 112.', hint: '7³ = 343.' },
{ parts: ['10 × 10: ', { box: '330' }], why: '(1000 − 10)/3 = 330.', hint: '10³ = 1000.' },
];
const GROWQ = [
{ parts: ['n = 30: about n³/3 = ', { box: '9000' }, ' operations'], why: '30³ = 27,000; a third is 9,000 (exactly 8,990).', hint: '30³ = 27,000.' },
{ parts: ['n doubles: the work is multiplied by about ', { box: '8' }], why: '(2n)³ = 8n³.', hint: 'What is 2³?' },
{ parts: ['n triples: the work is multiplied by about ', { box: '27' }], why: '(3n)³ = 27n³.', hint: 'What is 3³?' },
{ parts: ['n = 4: b costs ', { box: '16' }, ' operations'], why: 'b costs n² = 16 (6 forward, 10 back).', hint: 'b costs n squared.' },
{ parts: ['n = 100: b costs ', { box: '10000' }, ', A about ', { box: '333333' }], why: 'n² = 10,000; n³/3 ≈ 333,333 (exactly 333,300).', hint: '100² and 100³/3.' },
];
// near the exact count is fine for "about": within 1 %
const HANDS = [
{ n0: 4, test: v => v.n === 7, ans: 7, prompt: 'Find the n whose elimination takes exactly 112 operations.', why: 'n = 7: (343 − 7)/3 = 112.',
hint: v => `n = ${v.n} takes ${coFmt(coTotal(v.n))}.` },
{ n0: 4, test: v => v.n === 25, ans: 25, prompt: 'Find the smallest n that needs more than 5,000 operations.', why: 'n = 25: 5,200. n = 24 needs only 4,600.',
hint: v => v.n < 25 ? `n = ${v.n}: ${coFmt(coTotal(v.n))}, not more than 5,000 yet.` : `n = ${v.n}: ${coFmt(coTotal(v.n))}. A smaller n is enough.` },
{ n0: 4, test: v => v.n === 32, ans: 32, prompt: 'Find the smallest n that needs more than 10,000 operations.', why: 'n = 32: 10,912. n = 31 needs 9,920.',
hint: v => v.n < 32 ? `n = ${v.n}: ${coFmt(coTotal(v.n))}, not more than 10,000 yet.` : `n = ${v.n}: ${coFmt(coTotal(v.n))}. A smaller n is enough.` },
];
/* ---------- beats ---------- */
const BEATS = [
['intro', 'How much work?', (m, D) => {
const g = G(scene, { o: 0, y: 24 });
T(g, 'Unit 4, Chapter 33', { x: 800, y: 230, size: 28, fill: COL.dim, anchor: 'middle' });
T(g, 'How Much Work Is Elimination?', { x: 800, y: 330, size: 76, weight: 600, anchor: 'middle' });
tw(g, { o: 1, y: 0 }, .2, 1.1, out);
const r = text(scene, 'count every operation: about n³/3 for n × n', 800, 420, m('sub'), { size: 34, fill: COL.dim, anchor: 'middle' });
const lg = G(scene, { o: 0 }); show(lg, m('sub') + 1, .5);
const ce = cost(lg, { n: 2, chart: { x: 560, y: 820, w: 480, h: 260, N: 10 }, t0: 0 });
ce.to({ n: 10 }, m('move') + .3, 3.4, lin);
tw(g, { o: 0, y: -24 }, D - .9, .8); hide(r, D - .9, .8); hide(lg, D - .9, .8);
}],
['one', 'One operation', (m) => {
const p = panel(0);
title(p, 'What is one operation?');
const ce = cost(p, { n: 2, A: A2, grid: { x: 140, y: 220, w: 260, work: [140, 580] }, chart: { ...CHART, N: 4 }, read: { x: 500, y: 290 }, t0: .3 });
quietRead(ce); ce.set({ c: 0 });
put(ce.parts.chart, { o: 0 }); show(ce.parts.chart, m('chart'), .5);
const GR1 = { x: 140, y: 220, w: 260 };
cellGlow(p, GR1, 2, [[1, 0], [1, 1]], m('A') + 2, 3.5);
cellGlow(p, GR1, 2, [[1, 0]], m('l') + .3, 2.2); cellGlow(p, GR1, 2, [[0, 0]], m('l') + 2.6, 1.8);
cellGlow(p, GR1, 2, [[1, 1]], m('seven') + .2, 2.6); cellGlow(p, GR1, 2, [[0, 1]], m('seven') + 1.4, 1.4);
cellGlow(p, GR1, 2, [[1, 0], [1, 1]], m('total') + .4, 3);
cellGlow(p, GR1, 2, [[1, 0], [1, 1]], m('rule') + 2, 3);
ce.to({ c: 1 }, m('l') + 4, .3, lin);
ce.to({ c: 2 }, m('seven') + 1.6, .3, lin);
text(p, 'a multiply and a subtract: one operation', 140, 680, m('seven') + 5, { size: 28, fill: COL.dim, weight: 600 });
text(p, 'every number below a pivot that changes: 1 operation', 140, 760, m('rule'), { size: 30, weight: 600 });
}],
['three', 'Three by three', (m) => {
const p = panel(0);
title(p, 'Counting a 3 × 3');
const ce = cost(p, { n: 3, A: A3, grid: { x: 130, y: 210, w: 330, work: [130, 610] }, chart: { ...CHART, N: 4 }, read: { x: 530, y: 260 }, t0: .3 });
quietRead(ce); ce.set({ c: 0 });
const GR3 = { x: 130, y: 210, w: 330 };
cellGlow(p, GR3, 3, [[0, 0]], m('s1'), 4.5); cellGlow(p, GR3, 3, [[1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]], m('s1') + 2, 3);
cellGlow(p, GR3, 3, [[1, 1]], m('s2'), 4.5); cellGlow(p, GR3, 3, [[2, 1], [2, 2]], m('s2') + 2.4, 3);
cellGlow(p, GR3, 3, [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]], m('corner') + .6, 2.4);
cellGlow(p, GR3, 3, [[1, 1], [1, 2], [2, 1], [2, 2]], m('corner') + 3.4, 2.6);
steps(ce, 0, 3, m('go1') + .8, m('row3') - 1.5);
steps(ce, 3, 6, m('row3') + .8, m('six') - 1.5);
text(p, 'step 1: 2 rows × 3 numbers = 6', 530, 420, m('six'), { size: 30, weight: 600 });
steps(ce, 6, 8, m('go2') + .6, m('two') - 1.2);
text(p, 'step 2: 1 row × 2 numbers = 2', 530, 470, m('two'), { size: 30, weight: 600 });
text(p, 'total: 6 + 2 = 8', 530, 530, m('tot'), { size: 30, weight: 600 });
text(p, 'each step: a smaller corner', 130, 720, m('corner'), { size: 28, fill: COL.dim, weight: 600 });
}],
['four', 'Four by four', (m) => {
const p = panel(0);
title(p, 'Counting a 4 × 4');
const ce = cost(p, { n: 4, grid: { x: 130, y: 210, w: 320 }, chart: { ...CHART, N: 5 }, read: { x: 530, y: 260 }, t0: .3 });
quietRead(ce); ce.set({ c: 0 });
ce.count(1, m('s1') + .3, 3, lin);
text(p, 'step 1: 3 rows × 4 numbers = 12', 530, 420, m('s1') + .5, { size: 30, weight: 600 });
ce.count(2, m('s2') + .2, 2, lin);
text(p, 'step 2: 2 rows × 3 numbers = 6', 530, 470, m('s2') + .4, { size: 30, weight: 600 });
ce.count(3, m('s3') + .2, 1, lin);
text(p, 'step 3: 1 row × 2 numbers = 2', 530, 520, m('s3') + .4, { size: 30, weight: 600 });
text(p, 'total: 12 + 6 + 2 = 20', 530, 580, m('tot'), { size: 30, weight: 600 });
say(p, ['a k × k corner: (k − 1) · k ≈ k²'], 130, 660, m('pat'), { size: 32 });
const GR4 = { x: 130, y: 210, w: 320 }, blk = (r0, c0) => { const o = []; for (let i = r0; i < 4; i++) for (let j = c0; j < 4; j++) o.push([i, j]); return o; };
cellGlow(p, GR4, 4, blk(0, 0), m('pat') + 1.5, 1.6); cellGlow(p, GR4, 4, blk(1, 1), m('pat') + 3.3, 1.6); cellGlow(p, GR4, 4, blk(2, 2), m('pat') + 5.1, 1.6);
curveFlash(ce, m('sum') + 2);
say(p, ['big n: n(n − 1) + … + 2·1 ≈ n² + (n − 1)² + … + 1²'], 130, 730, m('sum'), { size: 30, fill: COL.dim });
}],
['q1', 'Quick check', () => {
const p = panel(0);
title(p, 'Quick check');
const ce = cost(p, { n: 5, grid: { x: 130, y: 180, w: 240 }, chart: { ...CHART, N: 5 }, read: { x: 530, y: 260 } });
quietRead(ce); ce.set({ c: 0 });
S.ce = ce;
}, { ask: done => quiz({ x: 40, y: 450, w: 800, cls: 'side' }, [{
id: 'c-five', prompt: ['Count the operations for 5 × 5, one step at a time.'],
build: blanks([
{ parts: ['step 1: ', { box: '4' }, ' rows × ', { box: '5' }, ' numbers = ', { box: '20' }], why: 'Four rows below the pivot, five numbers in each.', hint: 'Every row below the pivot row, every number in it.' },
{ parts: ['steps 2, 3, 4: ', { box: '12' }, ' + ', { box: '6' }, ' + ', { box: '2' }, ', total ', { box: '40' }], why: 'Corners 4 × 4, 3 × 3, 2 × 2: 12 + 6 + 2; with step 1, 20 + 12 + 6 + 2 = 40.', hint: 'Each corner is one smaller: (k − 1) · k. Then add all four steps.',
fx: () => fxp(q => S.ce.set({ c: 40 * q }), 0, 2.4, lin) },
]),
}], done) }],
['chart', 'The chart', (m) => {
const p = panel(0);
title(p, 'The counts on a chart');
const ce = cost(p, { n: 2, grid: { x: 90, y: 470, w: 240 }, chart: { x: 760, y: 780, w: 760, h: 560, N: 5 }, read: { x: 90, y: 230 }, t0: .3 });
quietRead(ce, ['n', 'ops', 'third']);
put(ce.parts.read.third, { o: 0 }); show(ce.parts.read.third, m('curve'), .4);
ce.to({ n: 3 }, m('n3'), .1); ce.to({ n: 4 }, m('n4'), .1); ce.to({ n: 5 }, m('n5'), .1);
ce.to({ n: 10 }, m('grow') + .3, 3.4, lin);
curveFlash(ce, m('curve') + .3);
show(ce.parts.read.ratio, m('ratio'), .4);
ce.to({ n: 3 }, m('ratio') + 5, .2); ce.to({ n: 10 }, m('ratio') + 5.6, 3.6, lin);
say(p, ['= 990 ÷ 3'], 400, 845, m('exact') + 6, { size: 30, fill: COL.dim });
say(p, ['(n³ − n)/3: (1000 − 10)/3 = 330'], 90, 790, m('exact') + 1, { size: 32 });
}],
['pyr', 'Why a third', (m) => {
const p = panel(0);
title(p, 'Why a third?');
const ce = cost(p, { n: 6, pyr: { x: 760, y: 790, size: 460 }, read: { x: 90, y: 260 }, t0: .3 });
quietRead(ce); ce.set({ c: 0 });
put(ce.parts.box, { o: 0 }); show(ce.parts.box, m('box'), .6);
ce.count(1, m('l1') + .3, 3.5, lin);
ce.count(2, m('l2') + .2, 2.4, lin);
ce.to({ c: coTotal(6) }, m('all') + .2, 2.6, lin);
text(p, 'layer 1: 5 × 6 = 30', 90, 420, m('l1') + 1, { size: 28, weight: 600 });
text(p, 'layer 2: 4 × 5 = 20', 90, 465, m('l2') + .5, { size: 28, weight: 600 });
text(p, '… 12, 6, 2: 70 in all', 90, 510, m('all') + 2, { size: 28, weight: 600 });
text(p, 'box: 6 × 6 × 6 = 216', 90, 600, m('box') + .4, { size: 28, fill: COL.dim, weight: 600 });
tw(ce.parts.cone, { o: 1 }, m('cone') + .3, .8);
say(p, ['pyramid: ', F(1, 3), ' · base · height'], 90, 660, m('third'), { size: 30 });
say(p, ['= ', F(1, 3), ' · 6² · 6 = 72'], 110, 720, m('third') + 2.6, { size: 30 });
say(p, ['steps: 70 = 72 − 6/3'], 90, 800, m('steps') + .6, { size: 30, fill: COL.dim });
tw(ce.parts.cone, { 'a_fill-opacity': .16 }, m('third') + 4, .6); tw(ce.parts.cone, { 'a_fill-opacity': 0 }, m('third') + 7, .8);
tw(ce.parts.box, { 'a_stroke-width': 4 }, m('steps') + 2.4, .4); tw(ce.parts.box, { 'a_stroke-width': 2 }, m('steps') + 4, .6);
}],
['q2', 'Your turn', () => {
const p = panel(0);
title(p, 'Your turn');
const ce = cost(p, { n: 4, chart: { x: 760, y: 780, w: 760, h: 560, N: 10 }, read: { x: 90, y: 260 } });
quietRead(ce, ['n', 'ops', 'third']);
S.ce = ce;
}, { ask: done => quiz({ x: 40, y: 520, w: 640, cls: 'side' }, [{
id: 'c-thousand', prompt: ['Change n until elimination needs more than 1,000 operations, with n as small as it can be.'],
build: costTask(S.ce, v => v.n === 15, 'n = 15: (3375 − 15)/3 = 1,120. n = 14 needs only 910.',
v => v.n < 15 ? `n = ${v.n} needs ${coFmt(coTotal(v.n))}: not more than 1,000 yet.` : `n = ${v.n} needs ${coFmt(coTotal(v.n))}: more than 1,000, but a smaller n does it too.`, () => S.ce.set({ n: 15 })),
}], done) }],
['double', 'Twice the size', (m) => {
const p = panel(0);
title(p, 'Twice the size, and three times');
const ce = cost(p, { n: 10, chart: { x: 760, y: 780, w: 760, h: 560, N: 10 }, read: { x: 90, y: 260 }, t0: .3 });
quietRead(ce, ['n', 'ops', 'third']);
ce.to({ n: 20 }, m('twenty') + .2, 2.6, lin);
const r1 = say(p, ['2660 ÷ 330 ≈ 8 = 2³'], 90, 450, m('eight'), { size: 34 });
const r2 = say(p, ['twice n: × 8'], 90, 505, m('eight') + 4, { size: 34 });
ce.to({ n: 5 }, m('five') + 1.6, .4); ce.to({ n: 10 }, m('back') + .1, 1.4, lin);
const r3 = say(p, ['330 ÷ 40 ≈ 8'], 90, 570, m('back') + 2, { size: 34 });
ce.to({ n: 30 }, m('thirty') + .1, 2.8, lin);
const r4 = say(p, ['8990 ÷ 330 ≈ 27 = 3³'], 90, 650, m('thirty') + 3.2, { size: 34 });
const r5 = say(p, ['three times n: × 27'], 90, 705, m('thirty') + 5, { size: 34 });
[r1, r2, r3, r4, r5].forEach(e => hide(e, m('big') - .2, .5));
curveFlash(ce, m('ten') + .5);
text(p, '1000 × 1000: about 333 million operations', 90, 470, m('big') + .5, { size: 28, weight: 600 });
text(p, 'well under a second on a computer', 90, 515, m('big') + 5, { size: 26, fill: COL.dim, weight: 600 });
text(p, 'a million × a million: days', 90, 600, m('huge') + .5, { size: 28, weight: 600 });
}],
['right', 'And b?', (m) => {
const p = panel(0);
title(p, 'And the right side b?');
const { on, count } = ubGrid(p, [[2, 1, 1], [0, 1, 1], [0, 0, 2]], 140, 220, 96, .4);
count([[m('f1') + .6, 2], [m('f2') + .5, 3], [m('r3') + .6, 4], [m('r2') + .8, 6], [m('r1') + .8, 9]]);
on(['13', '23'], m('f1') + .3);
on(['23'], m('f2') + .3);
text(p, 'forward: 2 + 1 = 3', 600, 300, m('f2') + 1.2, { size: 28, weight: 600 });
on(['22', '23'], m('r3') + .3);
on(['11', '12', '13'], m('r2') + .3);
on(['00', '01', '02', '03'], m('r1') + .3);
text(p, 'back: 1 + 2 + 3 = 6', 600, 350, m('r1') + 1.5, { size: 28, weight: 600 });
say(p, ['3 + 6 = 9 = 3²'], 600, 420, m('nine') + .5, { size: 32 });
on(['03', '13', '23'], m('nine') + 1.6, 2.6);
}],
['b4', 'b for 4 × 4', (m) => {
const p = panel(0);
title(p, 'The right side of a 4 × 4');
const { on, count } = ubGrid(p, [[1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1]], 140, 200, 84, .3, { blank: true, cy: 600 });
count([[m('f1') + .6, 3], [m('f2') + .4, 5], [m('f3') + .4, 6], [m('r4') + .2, 7], [m('r3') + .2, 9], [m('r2') + .2, 12], [m('r1') + .2, 16]]);
on(['14', '24', '34'], m('f1') + .3, 1.6);
on(['24', '34'], m('f2') + .2, 1.2);
on(['34'], m('f3') + .2, 1.2);
text(p, 'forward: 3 + 2 + 1 = 6', 640, 280, m('f3') + 1, { size: 28, weight: 600 });
on(['33', '34'], m('r4'), 1.4);
on(['22', '23', '24'], m('r3'), 1.4);
on(['11', '12', '13', '14'], m('r2'), 1.4);
on(['00', '01', '02', '03', '04'], m('r1'), 1.6);
text(p, 'back: 1 + 2 + 3 + 4 = 10', 640, 330, m('r1') + .8, { size: 28, weight: 600 });
say(p, ['6 + 10 = 16 = 4²'], 640, 400, m('sum') + .3, { size: 32 });
say(p, ['n × n: b costs n²'], 640, 470, m('any') + .3, { size: 34 });
on(['04', '14', '24', '34'], m('any') + .6, 2.4);
}],
['keep', 'Keep L and U', (m) => {
const p = panel(0);
title(p, 'Factor once, keep L and U');
const tb = G(p, { o: 0 }); show(tb, m('cheap') + .3, .5);
const tbh = mk('rect', { x: 128, y: 284, width: 900, height: 46, rx: 10, fill: COL.chalk, 'fill-opacity': .13 }, tb); put(tbh, { o: 0 });
tw(tbh, { o: 1 }, m('ex') + .3, .3);
// the voice compares the columns: b's n² first, then A's
[[688, m('cheap') + 1.2], [318, m('cheap') + 3.6]].forEach(([x, at]) => { const c = mk('rect', { x, y: 186, width: 300, height: 196, rx: 12, fill: 'none', stroke: COL.chalk, 'stroke-width': 3 }, p); put(c, { o: 0 }); show(c, at, .3); hide(c, at + 2.2, .4); });
[['n', 'A: (n³ − n)/3', 'b: n²'], ['10', '330', '100'], ['100', '333,300', '10,000'], ['1000', '333,333,000', '1,000,000']].forEach((row, i) => row.forEach((str, j) =>
T(tb, str, { x: 140 + [0, 190, 560][j], y: 220 + i * 50, size: 28, fill: i ? COL.chalk : COL.dim, weight: 600 })));
// two bars to scale: 1 × A + 10 × b, against 10 × (A + b)
const X = 140, W = 1320, k = W / 3433000, wA = 333300 * k, wb = 10000 * k, H = 56;
const blk = (g, x, w, fill, at, op = .55) => { const r = mk('rect', { x, y: 0, width: w, height: H, fill, 'fill-opacity': op, stroke: COL.board, 'stroke-width': 1 }, g); put(r, { o: 0 }); show(r, at, .2); return r; };
const b1 = G(p, { y: 500 }), b2 = G(p, { y: 670 });
const l1 = G(p, { o: 0 }); show(l1, m('once'), .4);
T(l1, 'factor A once, then 10 right sides:', { x: X, y: 482, size: 28, weight: 600 }); T(l1, '333,300', { x: X + 600, y: 482, size: 28, fill: COL.task, weight: 700 });
text(p, '+ 10 × 10,000 = 433,300', X + 722, 482, m('tenb') + .4, { size: 28, fill: COL.task, weight: 700 });
blk(b1, X, wA, COL.chalk, m('once') + .4);
for (let i = 0; i < 10; i++) blk(b1, X + wA + i * wb, wb, COL.task, m('tenb') + .4 + i * .25, .9);
const l2 = G(p, { o: 0 }); show(l2, m('redo'), .4);
T(l2, 'start over for every b:', { x: X, y: 652, size: 28, weight: 600 }); T(l2, '10 × 343,300 = 3,433,000', { x: X + 600, y: 652, size: 28, fill: COL.task, weight: 700 });
for (let i = 0; i < 10; i++) { const x = X + i * (wA + wb); blk(b2, x, wA, COL.chalk, m('redo') + .5 + i * .45); blk(b2, x + wA, wb, COL.task, m('redo') + .5 + i * .45, .9); }
text(p, 'factor A = LU once; each new b: n² only', 140, 820, m('lu') + 1, { size: 30, weight: 600 });
}],
['q3', 'Quick check', () => {
const p = panel(0);
title(p, 'Quick check');
const ce = cost(p, { n: 6, grid: { x: 130, y: 180, w: 240 }, chart: { ...CHART, N: 6 }, read: { x: 530, y: 260 } });
quietRead(ce); ce.set({ c: 0 });
S.ce = ce;
}, { ask: done => quiz({ x: 40, y: 450, w: 800, cls: 'side' }, [{
id: 'c-six', prompt: ['Count for a 6 × 6 matrix.'],
build: blanks([
{ parts: ['elimination on A: (6³ − 6)/3 = ', { box: '70' }], why: '(216 − 6)/3 = 70: 30 + 20 + 12 + 6 + 2.', hint: '6³ = 216.', fx: () => fxp(q => S.ce.set({ c: 70 * q }), 0, 2.4, lin) },
{ parts: ['b, forward and back: ', { box: '36' }, ', n³/3 = ', { box: '72' }], why: 'b costs n² = 36 (15 forward, 21 back); 216 ÷ 3 = 72, just above the exact 70.', hint: 'b costs n squared; n³/3 is 6³ ÷ 3.' },
]),
}], done) }],
['wrap', 'Summary', (m) => {
const p = panel(0);
[['One operation: one number below a pivot changes.', 'w1'], ['A k × k corner costs (k − 1) · k; n × n costs (n³ − n)/3.', 'w2'],
['About n³/3: a third of the box. Twice n, eight times the work.', 'w3'], ['b costs only n²: factor A once, keep L and U.', 'w4']].forEach(([str, mark], i) => {
const g = G(p, { x: -16, y: 230 + i * 130, o: 0 });
mk('circle', { cx: 120, cy: -12, r: 26, fill: COL.chalk }, g);
T(g, String(i + 1), { x: 120, y: -1, size: 30, fill: COL.board, weight: 700, anchor: 'middle' });
T(g, str, { x: 170, size: 34, weight: 600 });
tw(g, { o: 1, x: 0 }, m(mark), .6);
});
}],
['final1', 'Chapter practice', () => { panel(0); }, { ask: done => quiz(SCREEN, [{
id: 'p-stage', prompt: ['One step: the rows below the pivot, times the numbers in each row.'],
build: blanks(STAGEQ),
}], done, 'Chapter practice · set 1 of 4') }],
['final2', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
id: 'p-total', prompt: ['The whole elimination of A: add the steps, or use (n³ − n)/3.'],
build: blanks(TOTALQ),
}], done, 'Chapter practice · set 2 of 4') }],
['final3', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
id: 'p-grow', prompt: ['Estimate with n³/3 for A and n² for b.'],
build: blanks(GROWQ),
}], done, 'Chapter practice · set 3 of 4') }],
['final4', 'Chapter practice', () => {
const p = panel(0);
title(p, 'Find n');
const ce = cost(p, { n: 4, chart: { x: 760, y: 780, w: 760, h: 560, N: 10 }, read: { x: 90, y: 260 } });
quietRead(ce, ['n', 'ops', 'third']);
S.ce = ce;
}, { ask: done => quiz({ x: 40, y: 520, w: 640, cls: 'side' }, HANDS.map((h, k) => ({
id: `p-hand${k + 1}`, prompt: [h.prompt],
build: (body, api) => { S.ce.set({ n: h.n0 }); return costTask(S.ce, h.test, h.why, h.hint, () => S.ce.set({ n: h.ans }))(body, api); },
})), done, 'Chapter practice · set 4 of 4') }],
['play', 'Playground', (m) => {
const p = panel(0);
title(p, 'Playground');
const ce = cost(p, { n: 5, grid: { x: 420, y: 170, w: 170 }, pyr: { x: 725, y: 780, size: 280 }, chart: { x: 1120, y: 780, w: 420, h: 480, N: 10 }, read: { x: 70, y: 200, gap: 46, size: 30 } });
S.ce = ce;
const hint = text(p, 'try: n = 2; n = 20; n = 60', 300, 110, m('go'), { size: 24, fill: COL.dim });
hide(hint, m('go') + 7, .6);
}, { ask: playAsk(() => S.ce, { pos: { x: 40, y: 450, w: 540, cls: 'side' }, tip: 'Change n and watch every part follow. Draw on the board if you like. Nothing is marked.' }) }],
['finish', 'Finished', () => { panel(0); }, { ask: finishCard }],
].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));
boot();
</script>
</body>
</html>