Skip to content

Commit 98baa86

Browse files
committed
Finally fix the requirements, passes vitest
1 parent 3e4176e commit 98baa86

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

Maths/SieveOfEratosthenes.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
const sieveOfEratosthenes = (n) => {
2-
/*
3-
* Calculates prime numbers till a number n
4-
* :param n: Number up to which to calculate primes
5-
* :return: A boolean list containing only primes
6-
*/
7-
const primes = new Array(n + 1)
8-
primes.fill(true) // set all as true initially
9-
primes[0] = primes[1] = false // Handling case for 0 and 1
10-
const sqrtn = Math.ceil(Math.sqrt(n))
11-
for (let i = 2; i <= sqrtn; i++) {
12-
if (primes[i]) {
13-
for (let j = i * i; j <= n; j += i) {
14-
/*
15-
Optimization.
16-
Let j start from i * i, not 2 * i, because smaller multiples of i have been marked false.
1+
/**
2+
* Function to get all prime numbers below a given number
3+
* This function returns an array of prime numbers
4+
* @see {@link https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes}
5+
*/
176

18-
For example, let i = 4.
19-
We do not have to check from 8(4 * 2) to 12(4 * 3)
20-
because they have been already marked false when i=2 and i=3.
21-
*/
22-
primes[j] = false
7+
function sieveOfEratosthenes(max) {
8+
const sieve = []
9+
const primes = []
10+
11+
for (let i = 2; i <= max; ++i) {
12+
if (!sieve[i]) {
13+
// If i has not been marked then it is prime
14+
primes.push(i)
15+
for (let j = i << 1; j <= max; j += i) {
16+
// Mark all multiples of i as non-prime
17+
sieve[j] = true
2318
}
2419
}
2520
}
Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
import { sieveOfEratosthenes } from '../SieveOfEratosthenes'
2-
import { PrimeCheck } from '../PrimeCheck'
3-
4-
describe('should return an array of prime booleans', () => {
5-
it('should have each element in the array as a prime boolean', () => {
6-
const n = 30
7-
const primes = sieveOfEratosthenes(n)
8-
primes.forEach((primeBool, index) => {
9-
if (primeBool) {
10-
expect(PrimeCheck(index)).toBeTruthy()
11-
}
12-
})
2+
3+
describe('sieveOfEratosthenes', () => {
4+
test('returns an empty array for max < 2', () => {
5+
expect(sieveOfEratosthenes(1)).toEqual([])
6+
})
7+
8+
test('returns [2] for max = 2', () => {
9+
expect(sieveOfEratosthenes(2)).toEqual([2])
10+
})
11+
12+
test('returns [2, 3] for max = 3', () => {
13+
expect(sieveOfEratosthenes(3)).toEqual([2, 3])
14+
})
15+
16+
test('returns [2, 3, 5, 7] for max = 10', () => {
17+
expect(sieveOfEratosthenes(10)).toEqual([2, 3, 5, 7])
18+
})
19+
20+
test('returns [2, 3, 5, 7, 11, 13, 17, 19] for max = 20', () => {
21+
expect(sieveOfEratosthenes(20)).toEqual([2, 3, 5, 7, 11, 13, 17, 19])
22+
})
23+
24+
test('returns [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] for max = 30', () => {
25+
expect(sieveOfEratosthenes(30)).toEqual([
26+
2, 3, 5, 7, 11, 13, 17, 19, 23, 29
27+
])
1328
})
1429
})

0 commit comments

Comments
 (0)