Skip to content

Commit faae973

Browse files
authored
Update 204-count-primes.js
1 parent 9add58e commit faae973

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

204-count-primes.js

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
const countPrimes = function(n) {
6+
const arr = Array(n).fill(0)
7+
8+
for(let i = 2; i * i < n; i++) {
9+
if(arr[i] !== 0) continue
10+
let j = i * i
11+
while(j < n) {
12+
arr[j] = 1
13+
j += i
14+
}
15+
}
16+
17+
let res = 0
18+
for(let i = 2; i < n; i++) {
19+
if(arr[i] === 0) res++
20+
}
21+
return res
22+
};
23+
24+
// another
25+
126
/**
227
* @param {number} n
328
* @return {number}

0 commit comments

Comments
 (0)