Added Implementation of Sieve Of Eratosthenes

This commit is contained in:
Mohit Sharma
2017-09-20 04:37:43 +05:30
committed by GitHub
parent b29f95310c
commit a3dcc2d294

View File

@ -0,0 +1,31 @@
function sieveOfEratosthenes (n) {
/*
* Calculates prime numbers till a number n
* :param n: Number upto which to calculate primes
* :return: A boolean list contaning only primes
*/
let primes = new Array(n + 1);
primes.fill(true); // set all as true initially
primes[0] = primes[1] = false; // Handling case for 0 and 1
let sqrtn = Math.ceil(Math.sqrt(n));
for (let i = 2; i <= sqrtn; i++) {
if (primes[i]) {
for (let j = 2 * i; j <= n; j += i) {
primes[j] = false;
}
}
}
return primes;
}
function main () {
let n = 69; // number till where we wish to find primes
let primes = sieveOfEratosthenes(n);
for (let i = 2; i <= n; i++) {
if (primes[i]) {
console.log(i);
}
}
}
main();