Branchwell

Watch Encyclopedia / website · watchencyclopedia.org / exp / sieve.js

Raw 3.5 KB on 2d94b28e8ffdaff52ef49f9af4d2eb2c3a6adb21
1WE.mount('sieve', function (S) {
2 var C = S.C, ctx = S.ctx, N, cols, st, p, m, done, run = false, acc = 0, found, hover = 0, G = {};
3 var PAL = [C.red, C.blue, C.green, C.violet, C.teal, C.pink, C.yellow, '#e09a5a'];
4 var size = S.select('Numbers', [['100', '1 to 100'], ['400', '1 to 400'], ['900', '1 to 900']], '100', reset), spd = S.slider('Speed', 1, 100, 20, 1);
5 var play = S.toggle('Run', true); S.button('One step', function () { play.set(false); step(); }); S.button('Start over', reset);
6 function reset() { N = +size.v; cols = Math.round(Math.sqrt(N)); st = new Int16Array(N + 1); p = 2; m = 0; done = false; found = 0; st[1] = -2; }
7 function step() { if (done) return; if (m === 0) { st[p] = -1; found++; if (p * p > N) { for (var i = p + 1; i <= N; i++) if (st[i] === 0) { st[i] = -1; found++; } done = true; return; } m = p * p; return; } if (st[m] === 0) st[m] = p; m += p; if (m > N) { m = 0; do { p++; } while (p <= N && st[p] !== 0); } }
8 reset();
9 S.pointer({ scroll: true, move: function (x, y) { var c = Math.floor((x - G.x0) / G.cs), r = Math.floor((y - G.y0) / G.cs); hover = c >= 0 && c < cols && r >= 0 && r * cols + c < N ? r * cols + c + 1 : 0; }, down: function (x, y) { var c = Math.floor((x - G.x0) / G.cs), r = Math.floor((y - G.y0) / G.cs); hover = c >= 0 && c < cols && r >= 0 && r * cols + c < N ? r * cols + c + 1 : 0; } });
10 function factors(v) { var o = [], d = 2; while (v > 1 && d * d <= v) { while (v % d === 0) { o.push(d); v /= d; } d++; } if (v > 1) o.push(v); return o; }
11 S.loop(function (dt) {
12 if (play.v && !done) { acc += dt * spd.v * (N / 100) * 1.2; while (acc >= 1) { acc--; step(); } }
13 var w = S.W, h = S.H, sm = S.small(), rws = Math.ceil(N / cols), top = 34, cs = Math.min((w - 20) / cols, (h - top - 8) / rws); G.cs = cs; G.x0 = (w - cs * cols) / 2; G.y0 = top;
14 S.clear(); var pi = {}, cnt = 0;
15 for (var i = 1; i <= N; i++) { var c = (i - 1) % cols, r = Math.floor((i - 1) / cols), x = G.x0 + c * cs, y = G.y0 + r * cs, s = st[i], fill = s === -1 ? C.acc : s === -2 ? '#15181e' : s > 0 ? null : '#1e232c';
16 if (s > 0) { if (pi[s] === undefined) pi[s] = cnt++ ; }
17 if (fill) S.rr(x + 1, y + 1, cs - 2, cs - 2, Math.min(5, cs * .2), fill); else { ctx.globalAlpha = .28; S.rr(x + 1, y + 1, cs - 2, cs - 2, Math.min(5, cs * .2), PAL[[2, 3, 5, 7, 11, 13, 17, 19, 23, 29].indexOf(s) % 8]); ctx.globalAlpha = 1; }
18 if (i === m && !done) S.rr(x + 1, y + 1, cs - 2, cs - 2, Math.min(5, cs * .2), null, C.ink);
19 if (cs >= 17) S.txt(i, x + cs / 2, y + cs / 2 + .5, s === -1 ? '#1a1206' : s > 0 || s === -2 ? C.dim : C.ink, Math.min(13, cs * .38), 'center', s === -1 ? 700 : 500);
20 }
21 S.txt(found + ' primes found', 14, 17, C.acc, 14, 'left', 700); if (!done) S.txt(m ? 'Crossing out every multiple of ' + p : 'Next prime: ' + p, w - 54, 17, C.mut, 12.5, 'right');
22 var tip = ''; if (hover) { var f = factors(hover); tip = hover === 1 ? ' <b>1</b> is not counted as a prime.' : f.length === 1 ? ' <b>' + hover + '</b> is prime. Nothing divides it but 1 and itself.' : ' <b>' + hover + '</b> = ' + f.join(' × ') + '.'; }
23 S.say((done ? 'Done. Every gold number is a <b>prime</b>: ' + found + ' of them up to ' + N + '. We only had to cross out multiples of the primes up to ' + Math.floor(Math.sqrt(N)) + '.' : 'Start at 2. Keep it, and cross out every multiple of it. Move to the next number still standing, and repeat. Whatever survives is <b>prime</b>.') + (tip || (sm ? '' : ' Point at any number to see what it is made of.')));
24 });
25});