```
def countPrimes(self, n):
if n < 3:
return 0
"Return all primes <= n."
#np1 = n + 1
s = [True] * n
s[0] = s[1] = False
sqrtn = int(round(n**0.5))
for i in xrange(2, sqrtn + 1):
if s[i]:
s[i*i: n: i] = [False] * len(xrange(i*i, n, i))
return s.count(True)
```

Explanation at the bottom of https://github.com/Lubndub/LeetCode/blob/master/CountPrimes.py