```
public class Solution {
public int countPrimes(int n) {
if(n<2){
return 0;
}
BitSet AllNumbersSet=new BitSet(n);
AllNumbersSet.clear();
for(int i=2;i<=n/2;i++){
for(int j=i*2;j<n;j=j+i){
AllNumbersSet.set(j);
}
}
return n-AllNumbersSet.cardinality()-2;
}
}
```