-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0204-Count-primes.cs
More file actions
45 lines (37 loc) · 902 Bytes
/
0204-Count-primes.cs
File metadata and controls
45 lines (37 loc) · 902 Bytes
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
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0204.Count_primes
{
public class _0204_Count_primes
{
public int CountPrimes(int n)
{
if (n < 2)
return 0;
bool[] primes = new bool[n];
primes[0] = false;
primes[1] = false;
int i = 0;
for (i = 2; i < n; i++)
primes[i] = true;
i = 2;
while (i < n)
{
if (primes[i])
for (int j = 2; i * j < n; j++)
primes[i * j] = false;
i++;
}
int count = 0;
i = 2;
while (i < n)
{
if (primes[i])
count++;
i++;
}
return count;
}
}
}