Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve duplicate entries for sieve of eratosthenes #1770

Merged
1 change: 0 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 22 additions & 21 deletions Maths/SieveOfEratosthenes.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
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 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 = []

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
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
}
}
}
Expand Down
24 changes: 0 additions & 24 deletions Maths/SieveOfEratosthenesIntArray.js

This file was deleted.

37 changes: 26 additions & 11 deletions Maths/test/SieveOfEratosthenes.test.js
Original file line number Diff line number Diff line change
@@ -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
])
})
})
12 changes: 0 additions & 12 deletions Maths/test/SieveOfEratosthenesIntArray.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion Project-Euler/Problem035.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author ddaniel27
*/
import { sieveOfEratosthenes } from '../Maths/SieveOfEratosthenesIntArray'
import { sieveOfEratosthenes } from '../Maths/SieveOfEratosthenes'

function problem35(n) {
if (n < 2) {
Expand Down
Loading