Skip to content

Instantly share code, notes, and snippets.

@missett
Last active April 6, 2021 20:36
Show Gist options
  • Save missett/8022615 to your computer and use it in GitHub Desktop.
Save missett/8022615 to your computer and use it in GitHub Desktop.
Recursive implementation of the Sieve of Eratosthenes algorithm for filtering a set of numbers for prime numbers.
var sieve = function(xs) {
var head = xs[0],
tail = xs.slice(1);
if(head === undefined) return [];
if(head < 2) return sieve(tail);
tail = tail.filter(function(x) {
return x % head > 0 ? true : false;
});
return [head].concat( sieve(tail) );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment