From a3dcc2d294dcc8ab60affa8d32c4f8bf15855f1e Mon Sep 17 00:00:00 2001 From: Mohit Sharma Date: Wed, 20 Sep 2017 04:37:43 +0530 Subject: [PATCH 1/2] Added Implementation of Sieve Of Eratosthenes --- Algorithms/sieveOfEratosthenes | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Algorithms/sieveOfEratosthenes diff --git a/Algorithms/sieveOfEratosthenes b/Algorithms/sieveOfEratosthenes new file mode 100644 index 0000000000..0efabbca83 --- /dev/null +++ b/Algorithms/sieveOfEratosthenes @@ -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(); From ee3f3dbf6f4d94d23867ef157fb6bdc08d8a1f22 Mon Sep 17 00:00:00 2001 From: Mohit Sharma Date: Wed, 20 Sep 2017 04:40:27 +0530 Subject: [PATCH 2/2] Added Implementation of Sieve Of Eratosthenes --- Algorithms/{sieveOfEratosthenes => sieveOfEratosthenes.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Algorithms/{sieveOfEratosthenes => sieveOfEratosthenes.js} (100%) diff --git a/Algorithms/sieveOfEratosthenes b/Algorithms/sieveOfEratosthenes.js similarity index 100% rename from Algorithms/sieveOfEratosthenes rename to Algorithms/sieveOfEratosthenes.js