```
int countPrimes(int n) {
if (n < 2)
return 0;
bool tmp[n];
int i, j, res;
for (i = 0; i < n; ++i)
tmp[i] = 1;
tmp[0] = tmp[1] = 0;
for (i = 2; i < n; ++i){
if (tmp[i]){
for (j = 2 * i; j < n; j += i)
tmp[j] = 0;
}
}
res = 0;
for (i = 2; i < n; ++i){
if (tmp[i])
++res;
}
return res;
}
```

For example,in this function, if we can use a global array to preserve the prime numbers, we can get

this program run more fast! Becasue we can just search in this array and do not need to calculate

primes in every test .