Description:
Count the number of prime numbers less than a non-negative number, n
1 int countPrimes(int n) { 2 int i,j; 3 bool *primer = malloc(sizeof(bool)*n); 4 for(i=0;i<n;i++) 5 { 6 primer[i]=true; 7 } 8 primer[0] = false; 9 primer[1] = false; 10 int limit = sqrt(n); 11 12 for(i = 2; i <=limit;i++) 13 { 14 for(j = i*i;j<n;j+=i) 15 { 16 primer[j]=false; 17 } 18 } 19 int result = 0; 20 for(i = 0;i<n;i++) 21 { 22 if(primer[i]) 23 result++; 24 } 25 return result; 26 27 }