Skip to content

Commit e3cf400

Browse files
feat: add primes in range algorithm
1 parent 5c39e87 commit e3cf400

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎Maths/PrimesInRange.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @function primesInRange
3+
* @description Returns all prime numbers in a given range [start, end]
4+
* using the Sieve of Eratosthenes algorithm.
5+
* @param {number} start - The start of the range (inclusive)
6+
* @param {number} end - The end of the range (inclusive)
7+
* @returns {number[]} - Array of all prime numbers in the range
8+
* @see https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
9+
* @example primesInRange(1, 10) => [2, 3, 5, 7]
10+
* @example primesInRange(10, 20) => [11, 13, 17, 19]
11+
*/
12+
const primesInRange = (start, end) => {
13+
if (!Number.isInteger(start) || !Number.isInteger(end)) {
14+
throw new TypeError('Both inputs must be integers')
15+
}
16+
if (start < 1 || end < 1) {
17+
throw new TypeError('Both inputs must be positive integers')
18+
}
19+
if (start > end) {
20+
throw new RangeError('Start must be less than or equal to end')
21+
}
22+
const sieve = new Array(end + 1).fill(true)
23+
sieve[0] = false
24+
sieve[1] = false
25+
for (let i = 2; i <= Math.sqrt(end); i++) {
26+
if (sieve[i]) {
27+
for (let j = i * i; j <= end; j += i) {
28+
sieve[j] = false
29+
}
30+
}
31+
}
32+
return sieve
33+
.slice(start)
34+
.map((isPrime, i) => (isPrime ? start + i : null))
35+
.filter((n) => n !== null)
36+
}
37+
38+
export { primesInRange }

‎Maths/test/PrimesInRange.test.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { primesInRange } from '../PrimesInRange.js'
2+
3+
describe('primesInRange', () => {
4+
test('primesInRange(1, 10) returns [2,3,5,7]', () => {
5+
expect(primesInRange(1, 10)).toEqual([2, 3, 5, 7])
6+
})
7+
8+
test('primesInRange(10, 20) returns [11,13,17,19]', () => {
9+
expect(primesInRange(10, 20)).toEqual([11, 13, 17, 19])
10+
})
11+
12+
test('primesInRange(1, 2) returns [2]', () => {
13+
expect(primesInRange(1, 2)).toEqual([2])
14+
})
15+
16+
test('primesInRange(1, 1) returns []', () => {
17+
expect(primesInRange(1, 1)).toEqual([])
18+
})
19+
20+
test('primesInRange(14, 16) returns []', () => {
21+
expect(primesInRange(14, 16)).toEqual([])
22+
})
23+
24+
test('primesInRange(1, 50) has correct count', () => {
25+
expect(primesInRange(1, 50)).toHaveLength(15)
26+
})
27+
28+
test('primesInRange with same start and end that is prime', () => {
29+
expect(primesInRange(7, 7)).toEqual([7])
30+
})
31+
32+
test('throws TypeError for float input', () => {
33+
expect(() => primesInRange(1.5, 10)).toThrow(TypeError)
34+
})
35+
36+
test('throws TypeError for negative input', () => {
37+
expect(() => primesInRange(-1, 10)).toThrow(TypeError)
38+
})
39+
40+
test('throws RangeError when start > end', () => {
41+
expect(() => primesInRange(10, 5)).toThrow(RangeError)
42+
})
43+
})

0 commit comments

Comments
 (0)