forked from ShivamDubey7/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathCount_Primes.cpp
More file actions
41 lines (38 loc) · 739 Bytes
/
Count_Primes.cpp
File metadata and controls
41 lines (38 loc) · 739 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
#include <bits/stdc++.h>
using namespace std;
int countPrimes(int n){
bool p[n+1] = {false};
//p[0] = false, p[1] = false;
for(int i=2;i<n;i++){
p[i] = true;
}
if(n<2){
return 0;
}
else{
int cnt = 0;
for(int i=2;i*i<=n;i++){
if(p[i] == true){
for(int j=i*i;j<=n; j += i){
p[j] = false;
}
}
}
for(int i=0;i<n;i++){
if(p[i]==true){
cnt++;
}
}
return cnt;
}
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
cout<<countPrimes(n);
}
return 0;
}