A handful of common prime sieve implementations in javascript. Each sieve accepts two arguments. The first is simply the size of the sieve, the second is a toggle function which is called each time a value is "sieved" (or unsieved). This makes it easy to build animations decoupled from the sieving algorithm.
The toggle function has a signature of toggle(value, flag)
where the flag corresponds to the state of the value in the sieve. I.e. if the flag is false
, the value has been 'sieved' and is no longer active in the sieve, if it is true
then the value has been re-included in the sieve (see e.g. Sieve of Atkin for when that might happen).
The easiest way to install is via NPM.
var sieves = require('prime-sieves');
// some kind of visual behaviour function
function sieve(value, flag) {
$('#animation > span')[value].toggleClass('active', flag)
};
// kick things off
sieves.eratosthenes(100, sieve);