-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path204.count-primes.js
More file actions
72 lines (65 loc) · 1.25 KB
/
Copy path204.count-primes.js
File metadata and controls
72 lines (65 loc) · 1.25 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* @lc app=leetcode id=204 lang=javascript
*
* [204] Count Primes
*
* https://leetcode.com/problems/count-primes/description/
*
* algorithms
* Easy (28.80%)
* Likes: 1012
* Dislikes: 390
* Total Accepted: 232.8K
* Total Submissions: 806.2K
* Testcase Example: '10'
*
* Count the number of prime numbers less than a non-negative number, n.
*
* Example:
*
*
* Input: 10
* Output: 4
* Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
*
*
*/
/**
* @param {number} n
* @return {number}
*/
/*
A range of prime
https://zh.wikipedia.org/wiki/%E5%9F%83%E6%8B%89%E6%89%98%E6%96%AF%E7%89%B9%E5%B0%BC%E7%AD%9B%E6%B3%95
A specific number is prime
https://zh.wikipedia.org/wiki/%E7%B4%A0%E6%80%A7%E6%B5%8B%E8%AF%95
remember 1 is not a prime by definition
for 10
2: 1
3: 1
4: 0
5: 1
6: 0
7: 1
8: 0
9: 0
*/
var countPrimes = function(n) {
let markedNumbers = [],
result = 0;
for (let i = 2; i < n; i++) {
if (markedNumbers[i] === undefined) {
// is Primes
markedNumbers[i] = 1;
result++;
// rm it's multiples,sieving
let j = 2;
while (i * j < n) {
markedNumbers[i * j] = 0;
j++;
}
}
}
console.log("flag array", markedNumbers);
return result;
};