-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount Primes.cpp
More file actions
50 lines (48 loc) · 1.09 KB
/
Count Primes.cpp
File metadata and controls
50 lines (48 loc) · 1.09 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
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
//int countPrimes(int n) {
// vector<bool> num(n - 1, true);
// num[0] = false;
// int res = 0, limit = sqrt(n);
// for (int i = 2; i <= limit; ++i) {
// if (num[i - 1]) {
// for (int j = i * i; j < n; j += i) {
// num[j - 1] = false;
// }
// }
// }
// for (int j = 0; j < n - 1; ++j) {
// if (num[j]) ++res;
// }
// return res;
// }
int countPrimes(int n) {
int cont=0;
bool isP[n-1];
for (int i=0;i<n-1;i++)
isP[i]=false;
isP[0]=true;
for (int i=2;i<=sqrt(n);i++)
{
if (isP[i-1])
{
for (int j=i*i;j<n;j+=i)
{
isP[j-1]=true;
}
}
}
for (int i=1;i<n;i++)
if (!isP[i-1])
cont++;
return cont;
}
int main()
{
int n=7;
int cont=countPrimes(n);
cout<<cont;
return 0;
}