From ce13a0b67aa4269b9d315e81278ecd6ad1427eb9 Mon Sep 17 00:00:00 2001 From: Shankha Suvra Dam <71999854+SpiderMath@users.noreply.github.com> Date: Sat, 25 May 2024 12:52:11 +0530 Subject: [PATCH 1/6] remove intarr test --- Maths/test/SieveOfEratosthenesIntArray.test.js | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 Maths/test/SieveOfEratosthenesIntArray.test.js diff --git a/Maths/test/SieveOfEratosthenesIntArray.test.js b/Maths/test/SieveOfEratosthenesIntArray.test.js deleted file mode 100644 index e3a3be3002..0000000000 --- a/Maths/test/SieveOfEratosthenesIntArray.test.js +++ /dev/null @@ -1,12 +0,0 @@ -import { sieveOfEratosthenes } from '../SieveOfEratosthenesIntArray' -import { PrimeCheck } from '../PrimeCheck' - -describe('should return an array of prime numbers', () => { - it('should have each element in the array as a prime numbers', () => { - const n = 100 - const primes = sieveOfEratosthenes(n) - primes.forEach((prime) => { - expect(PrimeCheck(prime)).toBeTruthy() - }) - }) -}) From b037bb58f0d0ec97e31230d9dcd804e16844a178 Mon Sep 17 00:00:00 2001 From: Shankha Suvra Dam <71999854+SpiderMath@users.noreply.github.com> Date: Sat, 25 May 2024 12:52:34 +0530 Subject: [PATCH 2/6] Remove main file oops --- Maths/SieveOfEratosthenesIntArray.js | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 Maths/SieveOfEratosthenesIntArray.js diff --git a/Maths/SieveOfEratosthenesIntArray.js b/Maths/SieveOfEratosthenesIntArray.js deleted file mode 100644 index 56336ce7d8..0000000000 --- a/Maths/SieveOfEratosthenesIntArray.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Function to get all prime numbers below a given number - * This function returns an array of prime numbers - * @see {@link https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes} - */ - -function sieveOfEratosthenes(max) { - const sieve = [] - const primes = [] - - for (let i = 2; i <= max; ++i) { - if (!sieve[i]) { - // If i has not been marked then it is prime - primes.push(i) - for (let j = i << 1; j <= max; j += i) { - // Mark all multiples of i as non-prime - sieve[j] = true - } - } - } - return primes -} - -export { sieveOfEratosthenes } From e3ef5c35559165b0f2e8a01889514780827182c0 Mon Sep 17 00:00:00 2001 From: Shankha Suvra Dam <71999854+SpiderMath@users.noreply.github.com> Date: Sat, 25 May 2024 13:01:15 +0530 Subject: [PATCH 3/6] FIXES: #1666 , remove references to SieveOfEratosthenesIntArray --- Project-Euler/Problem035.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/Problem035.js b/Project-Euler/Problem035.js index c877acba5a..0b11cd0357 100644 --- a/Project-Euler/Problem035.js +++ b/Project-Euler/Problem035.js @@ -9,7 +9,7 @@ * * @author ddaniel27 */ -import { sieveOfEratosthenes } from '../Maths/SieveOfEratosthenesIntArray' +import { sieveOfEratosthenes } from '../Maths/SieveOfEratosthenes' function problem35(n) { if (n < 2) { From 98baa860fb83cc049f8e2b6e7428e46d34193c71 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:22:50 +0530 Subject: [PATCH 4/6] Finally fix the requirements, passes vitest --- Maths/SieveOfEratosthenes.js | 37 +++++++++++--------------- Maths/test/SieveOfEratosthenes.test.js | 37 ++++++++++++++++++-------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/Maths/SieveOfEratosthenes.js b/Maths/SieveOfEratosthenes.js index 01e141f2f0..56336ce7d8 100644 --- a/Maths/SieveOfEratosthenes.js +++ b/Maths/SieveOfEratosthenes.js @@ -1,25 +1,20 @@ -const sieveOfEratosthenes = (n) => { - /* - * Calculates prime numbers till a number n - * :param n: Number up to which to calculate primes - * :return: A boolean list containing only primes - */ - const primes = new Array(n + 1) - primes.fill(true) // set all as true initially - primes[0] = primes[1] = false // Handling case for 0 and 1 - const sqrtn = Math.ceil(Math.sqrt(n)) - for (let i = 2; i <= sqrtn; i++) { - if (primes[i]) { - for (let j = i * i; j <= n; j += i) { - /* - Optimization. - Let j start from i * i, not 2 * i, because smaller multiples of i have been marked false. +/** + * Function to get all prime numbers below a given number + * This function returns an array of prime numbers + * @see {@link https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes} + */ - For example, let i = 4. - We do not have to check from 8(4 * 2) to 12(4 * 3) - because they have been already marked false when i=2 and i=3. - */ - primes[j] = false +function sieveOfEratosthenes(max) { + const sieve = [] + const primes = [] + + for (let i = 2; i <= max; ++i) { + if (!sieve[i]) { + // If i has not been marked then it is prime + primes.push(i) + for (let j = i << 1; j <= max; j += i) { + // Mark all multiples of i as non-prime + sieve[j] = true } } } diff --git a/Maths/test/SieveOfEratosthenes.test.js b/Maths/test/SieveOfEratosthenes.test.js index 056693d39b..1a10b8bc7f 100644 --- a/Maths/test/SieveOfEratosthenes.test.js +++ b/Maths/test/SieveOfEratosthenes.test.js @@ -1,14 +1,29 @@ import { sieveOfEratosthenes } from '../SieveOfEratosthenes' -import { PrimeCheck } from '../PrimeCheck' - -describe('should return an array of prime booleans', () => { - it('should have each element in the array as a prime boolean', () => { - const n = 30 - const primes = sieveOfEratosthenes(n) - primes.forEach((primeBool, index) => { - if (primeBool) { - expect(PrimeCheck(index)).toBeTruthy() - } - }) + +describe('sieveOfEratosthenes', () => { + test('returns an empty array for max < 2', () => { + expect(sieveOfEratosthenes(1)).toEqual([]) + }) + + test('returns [2] for max = 2', () => { + expect(sieveOfEratosthenes(2)).toEqual([2]) + }) + + test('returns [2, 3] for max = 3', () => { + expect(sieveOfEratosthenes(3)).toEqual([2, 3]) + }) + + test('returns [2, 3, 5, 7] for max = 10', () => { + expect(sieveOfEratosthenes(10)).toEqual([2, 3, 5, 7]) + }) + + test('returns [2, 3, 5, 7, 11, 13, 17, 19] for max = 20', () => { + expect(sieveOfEratosthenes(20)).toEqual([2, 3, 5, 7, 11, 13, 17, 19]) + }) + + test('returns [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] for max = 30', () => { + expect(sieveOfEratosthenes(30)).toEqual([ + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 + ]) }) }) From ab2df020e88ae87ce0c739793f4d198237c3dfcc Mon Sep 17 00:00:00 2001 From: SpiderMath Date: Wed, 8 Jan 2025 08:55:28 +0000 Subject: [PATCH 5/6] Updated Documentation in README.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7f6484cae5..5e8e1f401a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -254,7 +254,6 @@ * [RowEchelon](Maths/RowEchelon.js) * [ShorsAlgorithm](Maths/ShorsAlgorithm.js) * [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js) - * [SieveOfEratosthenesIntArray](Maths/SieveOfEratosthenesIntArray.js) * [Signum](Maths/Signum.js) * [SimpsonIntegration](Maths/SimpsonIntegration.js) * [Softmax](Maths/Softmax.js) From 8f9269e1241c405dbe8975f04fd99c0e4d811968 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:32:19 +0530 Subject: [PATCH 6/6] FIXES: #1666 and conform to alg comment standards --- Maths/SieveOfEratosthenes.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Maths/SieveOfEratosthenes.js b/Maths/SieveOfEratosthenes.js index 56336ce7d8..681d8ba904 100644 --- a/Maths/SieveOfEratosthenes.js +++ b/Maths/SieveOfEratosthenes.js @@ -1,9 +1,15 @@ /** - * Function to get all prime numbers below a given number - * This function returns an array of prime numbers - * @see {@link https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes} + * @function sieveOfEratosthenes + * @description Function to get all the prime numbers below a given number using sieve of eratosthenes algorithm + * @param {Number} max The limit below which all the primes are required to be + * @returns {Number[]} An array of all the prime numbers below max + * @see [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) + * @example + * sieveOfEratosthenes(1) // ====> [] + * @example + * sieveOfEratosthenes(20) // ====> [2, 3, 5, 7, 11, 13, 17, 19] + * */ - function sieveOfEratosthenes(max) { const sieve = [] const primes = []