Resolve duplicate entries for sieve of eratosthenes (#1770)

* remove intarr test

* Remove main file oops

* FIXES: #1666 , remove references to SieveOfEratosthenesIntArray

* Finally fix the requirements, passes vitest

* Updated Documentation in README.md

* FIXES: #1666 and conform to alg comment standards

---------

Co-authored-by: SpiderMath <SpiderMath@users.noreply.github.com>
This commit is contained in:
Shankha Suvra Dam
2025-01-12 15:57:54 +05:30
committed by GitHub
parent 85a55daf49
commit a62a46e732
6 changed files with 48 additions and 69 deletions

View File

@ -254,7 +254,6 @@
* [RowEchelon](Maths/RowEchelon.js) * [RowEchelon](Maths/RowEchelon.js)
* [ShorsAlgorithm](Maths/ShorsAlgorithm.js) * [ShorsAlgorithm](Maths/ShorsAlgorithm.js)
* [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js) * [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js)
* [SieveOfEratosthenesIntArray](Maths/SieveOfEratosthenesIntArray.js)
* [Signum](Maths/Signum.js) * [Signum](Maths/Signum.js)
* [SimpsonIntegration](Maths/SimpsonIntegration.js) * [SimpsonIntegration](Maths/SimpsonIntegration.js)
* [Softmax](Maths/Softmax.js) * [Softmax](Maths/Softmax.js)

View File

@ -1,25 +1,26 @@
const sieveOfEratosthenes = (n) => { /**
/* * @function sieveOfEratosthenes
* Calculates prime numbers till a number n * @description Function to get all the prime numbers below a given number using sieve of eratosthenes algorithm
* :param n: Number up to which to calculate primes * @param {Number} max The limit below which all the primes are required to be
* :return: A boolean list containing only primes * @returns {Number[]} An array of all the prime numbers below max
*/ * @see [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
const primes = new Array(n + 1) * @example
primes.fill(true) // set all as true initially * sieveOfEratosthenes(1) // ====> []
primes[0] = primes[1] = false // Handling case for 0 and 1 * @example
const sqrtn = Math.ceil(Math.sqrt(n)) * sieveOfEratosthenes(20) // ====> [2, 3, 5, 7, 11, 13, 17, 19]
for (let i = 2; i <= sqrtn; i++) { *
if (primes[i]) { */
for (let j = i * i; j <= n; j += i) { function sieveOfEratosthenes(max) {
/* const sieve = []
Optimization. const primes = []
Let j start from i * i, not 2 * i, because smaller multiples of i have been marked false.
For example, let i = 4. for (let i = 2; i <= max; ++i) {
We do not have to check from 8(4 * 2) to 12(4 * 3) if (!sieve[i]) {
because they have been already marked false when i=2 and i=3. // If i has not been marked then it is prime
*/ primes.push(i)
primes[j] = false for (let j = i << 1; j <= max; j += i) {
// Mark all multiples of i as non-prime
sieve[j] = true
} }
} }
} }

View File

@ -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 }

View File

@ -1,14 +1,29 @@
import { sieveOfEratosthenes } from '../SieveOfEratosthenes' import { sieveOfEratosthenes } from '../SieveOfEratosthenes'
import { PrimeCheck } from '../PrimeCheck'
describe('should return an array of prime booleans', () => { describe('sieveOfEratosthenes', () => {
it('should have each element in the array as a prime boolean', () => { test('returns an empty array for max < 2', () => {
const n = 30 expect(sieveOfEratosthenes(1)).toEqual([])
const primes = sieveOfEratosthenes(n) })
primes.forEach((primeBool, index) => {
if (primeBool) { test('returns [2] for max = 2', () => {
expect(PrimeCheck(index)).toBeTruthy() 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
])
}) })
}) })

View File

@ -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()
})
})
})

View File

@ -9,7 +9,7 @@
* *
* @author ddaniel27 * @author ddaniel27
*/ */
import { sieveOfEratosthenes } from '../Maths/SieveOfEratosthenesIntArray' import { sieveOfEratosthenes } from '../Maths/SieveOfEratosthenes'
function problem35(n) { function problem35(n) {
if (n < 2) { if (n < 2) {