```
class Solution {
public:
int countPrimes(int n) {
if(n<=1) return 0;
vector<bool> flag(n,true);
for(int index = 2; index<sqrt(n);index++){
if(flag[index]){
for(int j=index*index;j<n;j+=index){
flag[j]=false;
}
}
}
int sum;
for(int i=2;i<n;i++){
sum += (flag[i])?1:0;
}
return sum;
}
};
```