-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution204.cs
43 lines (37 loc) · 856 Bytes
/
Solution204.cs
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
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution204
{
/// <summary>
/// 204. Count Primes - Medium
/// <a href="https://leetcode.com/problems/count-primes">See the problem</a>
/// </summary>
public int CountPrimes(int n)
{
var isPrime = new bool[n];
for (var i = 2; i < n; i++)
{
isPrime[i] = true;
}
for (var i = 2; i * i < n; i++)
{
if (!isPrime[i])
{
continue;
}
for (var j = i * i; j < n; j += i)
{
isPrime[j] = false;
}
}
var count = 0;
for (var i = 2; i < n; i++)
{
if (isPrime[i])
{
count++;
}
}
return count;
}
}