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/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 } diff --git a/Maths/test/SieveOfEratosthenes.test.js b/Maths/test/SieveOfEratosthenes.test.js index 056693d39b..76aff7ca41 100644 --- a/Maths/test/SieveOfEratosthenes.test.js +++ b/Maths/test/SieveOfEratosthenes.test.js @@ -1,14 +1,12 @@ 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 +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((primeBool, index) => { - if (primeBool) { - expect(PrimeCheck(index)).toBeTruthy() - } + primes.forEach((prime) => { + expect(PrimeCheck(prime)).toBeTruthy() }) }) }) 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() - }) - }) -})