Skip to content

Instantly share code, notes, and snippets.

@carl-parrish
Created December 22, 2017 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carl-parrish/485347a53d597bca22080f909844e697 to your computer and use it in GitHub Desktop.
Save carl-parrish/485347a53d597bca22080f909844e697 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
Array.range = (start, stop, step=1) => {
  let A = [start];
  while(start+step <= stop){
    A = [...A, start+=step];
  }
  return A;
}


function sieveOfEratosthenes(num) {
  let primes = Array.range(3,num,2).filter((val) =>{
    for(i=3; i <= Math.sqrt(val); i+=2) {
      if(val % i === 0 ){return false;}
    }
    return val > 1;
    
  });
  return [2,...primes];
}

console.log(sieveOfEratosthenes(200));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment