Last active
December 19, 2015 00:49
-
-
Save OpenGrid/5871665 to your computer and use it in GitHub Desktop.
Simple Prime Sieve
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPrimes(howMany) { | |
var primes = [], isPrime, primesCount = 0, candidate, primeIndex; | |
for (candidate = 2; primesCount < howMany; candidate++) { | |
for(primeIndex = 0, isPrime = true; | |
primeIndex < primesCount && primes[primeIndex] <= Math.sqrt(candidate) && isPrime;) { | |
isPrime = (candidate % primes[primeIndex++] === 0) ? false : true; | |
} | |
if(isPrime === false) { | |
continue; | |
} | |
primes[primesCount++] = candidate; | |
} | |
return primes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment