int countPrimes(int n) {
if(n<=1) return 0;
int count=0;
bool flag[n]={0}; // set all the elements in the array to 0.
for(int i=2;i*i<=n;i++){ //find the multiples of i and mark them by setting flag[ ] to TRUE
if(flag[i]==false){
for(int j=i*i;j<n;j+=i)
flag[j]=true;
}
}
for(int k=2;k<n;k++)// count primes
if(flag[k]==0) count++;
return count;
}