From 91d4714d197a6e0d3889e6dbadcf9b7e74b4f0c1 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Sun, 3 Jun 2018 09:34:48 +0300 Subject: [PATCH] Code styling fixes for Sieve of Eratosthenes. --- README.md | 1 + .../math/sieve-of-eratosthenes/README.md | 13 ++++--- .../__test__/sieveOfEratosthenes.test.js | 2 +- .../sieveOfEratosthenes.js | 34 ++++++++++++------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 95c64e56..33eefdcb 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ a set of rules that precisely define a sequence of operations. * [Euclidean Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD) * [Least Common Multiple](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/least-common-multiple) (LCM) * [Integer Partition](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/integer-partition) + * [Sieve of Eratosthenes](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/sieve-of-eratosthenes) - finding all prime numbers up to any given limit * **Sets** * [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/cartesian-product) - product of multiple sets * [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/power-set) - all subsets of a set diff --git a/src/algorithms/math/sieve-of-eratosthenes/README.md b/src/algorithms/math/sieve-of-eratosthenes/README.md index fe697fcf..876379a0 100644 --- a/src/algorithms/math/sieve-of-eratosthenes/README.md +++ b/src/algorithms/math/sieve-of-eratosthenes/README.md @@ -6,15 +6,18 @@ It is attributed to Eratosthenes of Cyrene, an ancient Greek mathematician. ## How it works -1. Create a boolean array of `n+1` positions (to represent the numbers `0` through `n`) +1. Create a boolean array of `n + 1` positions (to represent the numbers `0` through `n`) 2. Set positions `0` and `1` to `false`, and the rest to `true` 3. Start at position `p = 2` (the first prime number) -4. Mark as `false` all the multiples of `p` (that is, positions `2*p`, `3*p`, `4*p`... until you reach the end of the array) +4. Mark as `false` all the multiples of `p` (that is, positions `2 * p`, `3 * p`, `4 * p`... until you reach the end of the array) 5. Find the first position greater than `p` that is `true` in the array. If there is no such position, stop. Otherwise, let `p` equal this new number (which is the next prime), and repeat from step 4 -When the algorithm terminates, the numbers remaining `true` in the array are all the primes below `n`. +When the algorithm terminates, the numbers remaining `true` in the array are all +the primes below `n`. -An improvement of this algorithm is, in step 4, start marking multiples of `p` from `p*p`, and not from `2*p`. The reason why this works is because, at that point, smaller multiples of `p` will have already been marked `false`. +An improvement of this algorithm is, in step 4, start marking multiples +of `p` from `p * p`, and not from `2 * p`. The reason why this works is because, +at that point, smaller multiples of `p` will have already been marked `false`. ## Example @@ -26,4 +29,4 @@ The algorithm has a complexity of `O(n log(log n))`. ## References -[Wikipedia](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) +- [Wikipedia](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) diff --git a/src/algorithms/math/sieve-of-eratosthenes/__test__/sieveOfEratosthenes.test.js b/src/algorithms/math/sieve-of-eratosthenes/__test__/sieveOfEratosthenes.test.js index 2571b81c..f2bce60d 100644 --- a/src/algorithms/math/sieve-of-eratosthenes/__test__/sieveOfEratosthenes.test.js +++ b/src/algorithms/math/sieve-of-eratosthenes/__test__/sieveOfEratosthenes.test.js @@ -1,6 +1,6 @@ import sieveOfEratosthenes from '../sieveOfEratosthenes'; -describe('factorial', () => { +describe('sieveOfEratosthenes', () => { it('should find all primes less than or equal to n', () => { expect(sieveOfEratosthenes(5)).toEqual([2, 3, 5]); expect(sieveOfEratosthenes(10)).toEqual([2, 3, 5, 7]); diff --git a/src/algorithms/math/sieve-of-eratosthenes/sieveOfEratosthenes.js b/src/algorithms/math/sieve-of-eratosthenes/sieveOfEratosthenes.js index ce7e1f9f..6b96583b 100644 --- a/src/algorithms/math/sieve-of-eratosthenes/sieveOfEratosthenes.js +++ b/src/algorithms/math/sieve-of-eratosthenes/sieveOfEratosthenes.js @@ -1,25 +1,33 @@ /** - * @param {number} n + * @param {number} maxNumber * @return {number[]} */ -export default function sieveOfEratosthenes(n) { - const isPrime = new Array(n + 1).fill(true); +export default function sieveOfEratosthenes(maxNumber) { + const isPrime = new Array(maxNumber + 1).fill(true); isPrime[0] = false; isPrime[1] = false; + const primes = []; - for (let i = 2; i <= n; i += 1) { - if (isPrime[i] === true) { - primes.push(i); + for (let number = 2; number <= maxNumber; number += 1) { + if (isPrime[number] === true) { + primes.push(number); - // Warning: When working with really big numbers, the following line may cause overflow - // In that case, it can be changed to: - // let j = 2 * i; - let j = i * i; + /* + * Optimisation. + * Start marking multiples of `p` from `p * p`, and not from `2 * p`. + * The reason why this works is because, at that point, smaller multiples + * of `p` will have already been marked `false`. + * + * Warning: When working with really big numbers, the following line may cause overflow + * In that case, it can be changed to: + * let nextNumber = 2 * number; + */ + let nextNumber = number * number; - while (j <= n) { - isPrime[j] = false; - j += i; + while (nextNumber <= maxNumber) { + isPrime[nextNumber] = false; + nextNumber += number; } } }