-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.html
More file actions
272 lines (245 loc) · 8.04 KB
/
primes.html
File metadata and controls
272 lines (245 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Watch the Sieve of Eratosthenes find primes interactively." />
<title>Prime Number Sieve – Fun With Math</title>
<link rel="stylesheet" href="../../css/style.css" />
<style>
#grid {
display: grid;
gap: 3px;
padding: 1rem;
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius);
overflow: auto;
}
.cell {
width: 100%;
aspect-ratio: 1;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
font-size: clamp(.5rem, 1.4vw, .75rem);
font-weight: 600;
font-family: 'Fira Code', Consolas, monospace;
background: rgba(255,255,255,.05);
color: var(--text-muted);
transition: background .25s, color .25s, transform .15s;
cursor: default;
}
.cell.eliminated {
background: rgba(255,255,255,.03);
color: rgba(255,255,255,.12);
text-decoration: line-through;
}
.cell.prime {
background: rgba(88,166,255,.2);
color: var(--accent);
transform: scale(1.08);
}
.cell.current-factor {
background: rgba(188,140,255,.35);
color: var(--accent2);
transform: scale(1.15);
}
#statsBar {
display: flex;
gap: 1.5rem;
flex-wrap: wrap;
font-size: .85rem;
color: var(--text-muted);
padding: .5rem 0;
}
#statsBar strong { color: var(--text); }
#primeList {
font-family: 'Fira Code', Consolas, monospace;
font-size: .8rem;
color: var(--accent);
word-break: break-all;
line-height: 1.9;
max-height: 160px;
overflow-y: auto;
padding: .5rem;
background: var(--bg);
border: 1px solid var(--border);
border-radius: 6px;
}
</style>
</head>
<body>
<nav>
<a class="brand" href="../../index.html">∑ Fun With Math</a>
<a href="../../index.html">← All Experiments</a>
</nav>
<div class="page-header">
<h1>Prime Number Sieve</h1>
<p>Watch the Sieve of Eratosthenes eliminate composites in real time.</p>
</div>
<div class="experiment-layout">
<div>
<div id="statsBar">
<span>Primes found: <strong id="primeCount">—</strong></span>
<span>Current factor: <strong id="currentFactor">—</strong></span>
<span>Step: <strong id="stepDisplay">—</strong></span>
</div>
<div id="grid"></div>
</div>
<aside class="controls">
<h3>Controls</h3>
<label>
Limit (n): <span id="limitLabel">200</span>
<input type="range" id="limitRange" min="20" max="400" step="10" value="200" />
</label>
<label>
Animation Speed
<select id="speed">
<option value="600">Slow</option>
<option value="250" selected>Medium</option>
<option value="80">Fast</option>
<option value="20">Very Fast</option>
</select>
</label>
<div style="display:flex;gap:.5rem;flex-wrap:wrap">
<button class="btn btn-primary" id="startBtn">▶ Start</button>
<button class="btn btn-secondary" id="resetBtn">↺ Reset</button>
</div>
<div class="info-box">
<h4>Primes</h4>
<div id="primeList">—</div>
</div>
<div class="info-box">
<h4>Algorithm</h4>
<ol style="padding-left:1.2rem;line-height:2">
<li>List integers 2 … n</li>
<li>Mark 2 as prime; cross out multiples</li>
<li>Find next unmarked number; repeat</li>
<li>All remaining numbers are prime</li>
</ol>
</div>
</aside>
</div>
<footer>
<p><a href="../../index.html">← Back to all experiments</a></p>
</footer>
<script>
(function () {
const grid = document.getElementById('grid');
const limitRange = document.getElementById('limitRange');
const limitLabel = document.getElementById('limitLabel');
const speedSelect = document.getElementById('speed');
const startBtn = document.getElementById('startBtn');
const resetBtn = document.getElementById('resetBtn');
const primeCountEl = document.getElementById('primeCount');
const currentFactEl = document.getElementById('currentFactor');
const stepEl = document.getElementById('stepDisplay');
const primeListEl = document.getElementById('primeList');
let n = 200;
let sieve = []; // false = still candidate, true = eliminated
let cells = [];
let animTimer = null;
let running = false;
// ── Build grid ───────────────────────────────────────────
function buildGrid() {
n = parseInt(limitRange.value);
sieve = new Array(n + 1).fill(false);
sieve[0] = sieve[1] = true; // not prime
// Determine columns for a roughly-square grid
const cols = Math.ceil(Math.sqrt(n));
grid.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
grid.innerHTML = '';
cells = [null, null]; // index 0 and 1 unused
for (let i = 2; i <= n; i++) {
const div = document.createElement('div');
div.className = 'cell';
div.textContent = i;
div.title = `${i}`;
grid.appendChild(div);
cells.push(div);
}
updateStats(null, 0);
primeListEl.textContent = '—';
}
function updateStats(factor, step) {
const primes = [];
for (let i = 2; i <= n; i++) {
if (!sieve[i]) primes.push(i);
}
primeCountEl.textContent = primes.length;
currentFactEl.textContent = factor !== null ? factor : '—';
stepEl.textContent = step > 0 ? step : '—';
if (primes.length > 0) {
primeListEl.textContent = primes.join(' ');
}
return primes;
}
// ── Animation generator ──────────────────────────────────
function* sieveGenerator() {
let step = 0;
for (let p = 2; p * p <= n; p++) {
if (sieve[p]) continue;
// Highlight current prime
cells[p].classList.add('current-factor');
yield { factor: p, step: ++step, phase: 'factor' };
// Eliminate multiples
for (let m = p * p; m <= n; m += p) {
if (!sieve[m]) {
sieve[m] = true;
cells[m].classList.add('eliminated');
cells[m].classList.remove('prime');
yield { factor: p, step: ++step, phase: 'eliminate', target: m };
}
}
// Mark p as confirmed prime
cells[p].classList.remove('current-factor');
cells[p].classList.add('prime');
}
// Mark all remaining candidates as prime
for (let i = 2; i <= n; i++) {
if (!sieve[i]) cells[i].classList.add('prime');
}
yield { factor: null, step: ++step, phase: 'done' };
}
function startAnimation() {
if (running) return;
running = true;
startBtn.disabled = true;
resetBtn.disabled = true;
const gen = sieveGenerator();
function tick() {
const { value, done } = gen.next();
if (done || (value && value.phase === 'done')) {
updateStats(null, value ? value.step : 0);
running = false;
startBtn.disabled = false;
resetBtn.disabled = false;
return;
}
updateStats(value.factor, value.step);
animTimer = setTimeout(tick, parseInt(speedSelect.value));
}
tick();
}
function resetAll() {
if (animTimer) clearTimeout(animTimer);
running = false;
startBtn.disabled = false;
resetBtn.disabled = false;
buildGrid();
}
// ── Event listeners ──────────────────────────────────────
limitRange.addEventListener('input', () => {
limitLabel.textContent = limitRange.value;
if (!running) buildGrid();
});
startBtn.addEventListener('click', startAnimation);
resetBtn.addEventListener('click', resetAll);
// ── Init ─────────────────────────────────────────────────
buildGrid();
})();
</script>
</body>
</html>