```
class Solution {
public:
int countPrimes(int n) {
if(n<3)
return 0;
int *flag=new int[n];
fill(flag,flag+n,1);
int c=n-2,m=n/2;
for(int i=2;i<=m;i++)
{
if(flag[i])
{
for(int j=2;i*j<n;j++)
{
if(flag[i*j])
{
flag[i*j]=0;
c--;
}
}
}
}
delete []flag;
return c;
}
};
```