-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path204 Count Primes.js
More file actions
55 lines (43 loc) · 1 KB
/
Copy path204 Count Primes.js
File metadata and controls
55 lines (43 loc) · 1 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
50
51
52
53
54
55
// Description:
// Count the number of prime numbers less than a non-negative number, n.
/**
* @param {number} n
* @return {number}
*/
// Ever so slightly better...156ms faster than ~76.20% and 128.5MB less than ~13.63%
var countPrimes = function(n) {
if (n <= 2) return 0;
let memo = [];
for (var i = 2; i < n; i++) {
memo[i] = true;
}
let sqRt = Math.sqrt(n - 1);
for (i = 2; i <= sqRt; i++) {
if (memo[i]) {
for (var j = 2 * i; j < memo.length; j += i) {
memo[j] = false;
}
}
}
let count = 0;
for (i = 2; i < memo.length; i++) {
if (memo[i]) count += 1;
}
return count;
};
/**
* @param {number} n
* @return {number}
*/
// Brute force 164ms faster than ~71.03% and 130.1MB less than ~10.60%...too much memory usage.
var countPrimes = function(n) {
let seen = [];
let count = 0;
for (let i = 2; i < n; i++) {
if (seen[i] === undefined) {
count += 1;
for (let j = 1; j * i < n; j++) seen[i * j] = 1;
}
}
return count;
};