Task 2 of 5
The Mandelbrot set asks one question at every point c: start
z = 0 and repeat z → z² + c — does z stay near the
origin forever, or fly off to infinity? Points that stay bounded are in the set;
for the rest, the interesting number is how many iterations they survived.
Two facts make this computable. Once |z| > 2, escape is guaranteed — so
we can stop watching. And we cap the loop at 100 passes: anything still bounded by then we
declare "inside". With z = zr + zi·i, one step is
zr² − zi² + cr for the new real part and 2·zr·zi + ci for the new
imaginary part.
z → z² + c up to 100 times, but only
while zr² + zi² < 4, and return how many iterations actually ran.zr = 0, zi = 0, count = 0 (already wired up)zr² + zi² < 4zr is read by both formulascount: 100 means "never escaped", small means "escaped fast"gpu.js's WebGL backend needs a fixed loop bound, so instead of breaking out we guard the body:
for (let i = 0; i < 100; i++) {
if (zr * zr + zi * zi < 4) {
// …step and count…
}
}
After escape the guard fails on every remaining pass, so z freezes and count stops.
Both formulas read the old zr, so stash the new real part
first:
const zrNext = zr * zr - zi * zi + cr;
zi = 2 * zr * zi + ci;
zr = zrNext;
count = count + 1;This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.