-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.js
More file actions
49 lines (46 loc) · 1.18 KB
/
Copy pathprimes.js
File metadata and controls
49 lines (46 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Prime numbers.
*/
/**
* Tests whether `n` is prime by trial division.
*
* Only divisors up to `√n` need checking: if `n = a · b` then at least one of
* `a`, `b` is `≤ √n`.
*
* Time: O(√n) - Space: O(1)
*
* @param {number} n
* @returns {boolean}
*/
export function isPrime(n) {
if (!Number.isInteger(n) || n < 2) return false;
const limit = Math.floor(Math.sqrt(n));
for (let divisor = 2; divisor <= limit; divisor++) {
if (n % divisor === 0) return false;
}
return true;
}
/**
* Returns every prime `≤ n` using the Sieve of Eratosthenes.
*
* Starting from each prime `p`, mark its multiples (from `p²` upward) as
* composite; whatever stays unmarked is prime.
*
* Time: O(n log log n) - Space: O(n)
*
* @param {number} n
* @returns {number[]}
*/
export function primesUpTo(n) {
if (n < 2) return [];
const isComposite = new Array(n + 1).fill(false);
const primes = [];
for (let candidate = 2; candidate <= n; candidate++) {
if (isComposite[candidate]) continue;
primes.push(candidate);
for (let multiple = candidate * candidate; multiple <= n; multiple += candidate) {
isComposite[multiple] = true;
}
}
return primes;
}