Skip to content

Instantly share code, notes, and snippets.

@missett
Last active September 14, 2025 02:04
Show Gist options
  • Select an option

  • Save missett/8022615 to your computer and use it in GitHub Desktop.

Select an option

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