Skip to content

Commit 77d2ea0

Browse files
committed
feat: update solutions to lc problem: No.0204
No.0204.Count Primes
1 parent c3636ba commit 77d2ea0

File tree

12 files changed

+380
-117
lines changed

12 files changed

+380
-117
lines changed

solution/0200-0299/0204.Count Primes/README.md

+102-18
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47-
如果 x 是质数,那么大于 x 的 x 的倍数 2x,3x,… 一定不是质数,因此我们可以从这里入手。
47+
**方法一:埃氏筛**
4848

49-
我们设 `primes[i]` 表示数 i 是不是质数,如果是质数则为 true,否则为 false。从小到大遍历每个数,如果这个数为质数,则将其所有的倍数都标记为合数(除了该质数本身),即 false,这样在运行结束的时候我们即能知道质数的个数
49+
如果 $x$ 是质数,那么大于 $x$ 的 $x$ 的倍数 $2x$,$3x$,… 一定不是质数,因此我们可以从这里入手
5050

51-
对于一个质数 x,我们从 2x 开始标记其实是冗余的,应该直接从 x⋅x 开始标记,因为 2x,3x,… 这些数一定在 x 之前就被其他数的倍数标记过了,例如 2 的所有倍数,3 的所有倍数等。
51+
我们设 $primes[i]$ 表示数 $i$ 是不是质数,如果是质数则为 $true$,否则为 $false$。从小到大遍历每个数,如果这个数为质数,则将其所有的倍数都标记为合数(除了该质数本身),即 $false$,这样在运行结束的时候我们即能知道质数的个数。
52+
53+
时间复杂度 $O(nloglogn)$。
5254

5355
<!-- tabs:start -->
5456

@@ -59,16 +61,14 @@
5961
```python
6062
class Solution:
6163
def countPrimes(self, n: int) -> int:
62-
if n < 2:
63-
return 0
64-
res = 0
65-
primes = [True for _ in range(n)]
64+
primes = [True] * n
65+
ans = 0
6666
for i in range(2, n):
6767
if primes[i]:
68-
res += 1
69-
for j in range(i * i, n, i):
68+
ans += 1
69+
for j in range(i + i, n, i):
7070
primes[j] = False
71-
return res
71+
return ans
7272
```
7373

7474
### **Java**
@@ -78,21 +78,105 @@ class Solution:
7878
```java
7979
class Solution {
8080
public int countPrimes(int n) {
81-
if (n < 2) return 0;
8281
boolean[] primes = new boolean[n];
8382
Arrays.fill(primes, true);
84-
int res = 0;
83+
int ans = 0;
8584
for (int i = 2; i < n; ++i) {
8685
if (primes[i]) {
87-
++res;
88-
if ((long) i * i < n) {
89-
for (int j = i * i; j < n; j += i) {
90-
primes[j] = false;
91-
}
86+
++ans;
87+
for (int j = i + i; j < n; j += i) {
88+
primes[j] = false;
89+
}
90+
}
91+
}
92+
return ans;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int countPrimes(int n) {
103+
vector<bool> primes(n, true);
104+
int ans = 0;
105+
for (int i = 2; i < n; ++i)
106+
{
107+
if (primes[i])
108+
{
109+
++ans;
110+
for (int j = i + i; j < n; j += i) primes[j] = false;
111+
}
112+
}
113+
return ans;
114+
}
115+
};
116+
```
117+
118+
### **Go**
119+
120+
```go
121+
func countPrimes(n int) int {
122+
primes := make([]bool, n)
123+
for i := range primes {
124+
primes[i] = true
125+
}
126+
ans := 0
127+
for i := 2; i < n; i++ {
128+
if primes[i] {
129+
ans++
130+
for j := i + i; j < n; j += i {
131+
primes[j] = false
132+
}
133+
}
134+
}
135+
return ans
136+
}
137+
```
138+
139+
### **JavaScript**
140+
141+
```js
142+
/**
143+
* @param {number} n
144+
* @return {number}
145+
*/
146+
var countPrimes = function (n) {
147+
let primes = new Array(n).fill(true);
148+
let ans = 0;
149+
for (let i = 2; i < n; ++i) {
150+
if (primes[i]) {
151+
++ans;
152+
for (let j = i + i; j < n; j += i) {
153+
primes[j] = false;
154+
}
155+
}
156+
}
157+
return ans;
158+
};
159+
```
160+
161+
### **C#**
162+
163+
```cs
164+
public class Solution {
165+
public int CountPrimes(int n) {
166+
var notPrimes = new bool[n];
167+
int ans = 0;
168+
for (int i = 2; i < n; ++i)
169+
{
170+
if (!notPrimes[i])
171+
{
172+
++ans;
173+
for (int j = i + i; j < n; j += i)
174+
{
175+
notPrimes[j] = true;
92176
}
93177
}
94178
}
95-
return res;
179+
return ans;
96180
}
97181
}
98182
```

solution/0200-0299/0204.Count Primes/README_EN.md

+97-15
Original file line numberDiff line numberDiff line change
@@ -45,38 +45,120 @@
4545
```python
4646
class Solution:
4747
def countPrimes(self, n: int) -> int:
48-
if n < 2:
49-
return 0
50-
res = 0
51-
primes = [True for _ in range(n)]
48+
primes = [True] * n
49+
ans = 0
5250
for i in range(2, n):
5351
if primes[i]:
54-
res += 1
55-
for j in range(i * i, n, i):
52+
ans += 1
53+
for j in range(i + i, n, i):
5654
primes[j] = False
57-
return res
55+
return ans
5856
```
5957

6058
### **Java**
6159

6260
```java
6361
class Solution {
6462
public int countPrimes(int n) {
65-
if (n < 2) return 0;
6663
boolean[] primes = new boolean[n];
6764
Arrays.fill(primes, true);
68-
int res = 0;
65+
int ans = 0;
6966
for (int i = 2; i < n; ++i) {
7067
if (primes[i]) {
71-
++res;
72-
if ((long) i * i < n) {
73-
for (int j = i * i; j < n; j += i) {
74-
primes[j] = false;
75-
}
68+
++ans;
69+
for (int j = i + i; j < n; j += i) {
70+
primes[j] = false;
7671
}
7772
}
7873
}
79-
return res;
74+
return ans;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int countPrimes(int n) {
85+
vector<bool> primes(n, true);
86+
int ans = 0;
87+
for (int i = 2; i < n; ++i)
88+
{
89+
if (primes[i])
90+
{
91+
++ans;
92+
for (int j = i + i; j < n; j += i) primes[j] = false;
93+
}
94+
}
95+
return ans;
96+
}
97+
};
98+
```
99+
100+
### **Go**
101+
102+
```go
103+
func countPrimes(n int) int {
104+
primes := make([]bool, n)
105+
for i := range primes {
106+
primes[i] = true
107+
}
108+
ans := 0
109+
for i := 2; i < n; i++ {
110+
if primes[i] {
111+
ans++
112+
for j := i + i; j < n; j += i {
113+
primes[j] = false
114+
}
115+
}
116+
}
117+
return ans
118+
}
119+
```
120+
121+
### **JavaScript**
122+
123+
```js
124+
/**
125+
* @param {number} n
126+
* @return {number}
127+
*/
128+
var countPrimes = function (n) {
129+
let primes = new Array(n).fill(true);
130+
let ans = 0;
131+
for (let i = 2; i < n; ++i) {
132+
if (primes[i]) {
133+
++ans;
134+
for (let j = i + i; j < n; j += i) {
135+
primes[j] = false;
136+
}
137+
}
138+
}
139+
return ans;
140+
};
141+
```
142+
143+
### **C#**
144+
145+
```cs
146+
public class Solution {
147+
public int CountPrimes(int n) {
148+
var notPrimes = new bool[n];
149+
int ans = 0;
150+
for (int i = 2; i < n; ++i)
151+
{
152+
if (!notPrimes[i])
153+
{
154+
++ans;
155+
for (int j = i + i; j < n; j += i)
156+
{
157+
notPrimes[j] = true;
158+
}
159+
}
160+
}
161+
return ans;
80162
}
81163
}
82164
```
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
class Solution {
2-
public:
3-
4-
vector<int> primes;
5-
6-
int isPrime(int num){
7-
8-
int i = 1;
9-
while( i < primes.size() && sqrt(num) >= primes[i] ){
10-
if( num % primes[i] == 0 ) return 0;
11-
i++;
12-
}
13-
return 1;
14-
}
15-
2+
public:
163
int countPrimes(int n) {
17-
18-
if( n <= 2 ) return 0;
19-
20-
primes.push_back(2);
21-
22-
for(int i = 3; i < n; i += 2){
23-
if( isPrime(i) )
24-
primes.push_back(i);
4+
vector<bool> primes(n, true);
5+
int ans = 0;
6+
for (int i = 2; i < n; ++i)
7+
{
8+
if (primes[i])
9+
{
10+
++ans;
11+
for (int j = i; j < n; j += i) primes[j] = false;
12+
}
2513
}
26-
27-
return primes.size();
14+
return ans;
2815
}
29-
};
16+
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
public class Solution {
22
public int CountPrimes(int n) {
3-
var count = 0;
4-
var notPrime = new bool[n];
5-
for (var i = 2; i < n; ++i)
3+
var notPrimes = new bool[n];
4+
int ans = 0;
5+
for (int i = 2; i < n; ++i)
66
{
7-
if (!notPrime[i])
7+
if (!notPrimes[i])
88
{
9-
++count;
10-
for (var j = i + i; j < n; j += i)
9+
++ans;
10+
for (int j = i + i; j < n; j += i)
1111
{
12-
notPrime[j] = true;
12+
notPrimes[j] = true;
1313
}
1414
}
1515
}
16-
return count;
16+
return ans;
1717
}
1818
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func countPrimes(n int) int {
2+
primes := make([]bool, n)
3+
for i := range primes {
4+
primes[i] = true
5+
}
6+
ans := 0
7+
for i := 2; i < n; i++ {
8+
if primes[i] {
9+
ans++
10+
for j := i + i; j < n; j += i {
11+
primes[j] = false
12+
}
13+
}
14+
}
15+
return ans
16+
}

0 commit comments

Comments
 (0)