204. Count Primes
Count the number of prime numbers less than a non-negative number, n.
Example:
Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
class Solution:
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
prime = [1] * (n + 1)
num = 0
for i in range(2,n):
if prime[i]:
num += 1
for j in range(2,n):
if i*j<=n:
prime[i*j] = 0
else:
break
return num
经典的素数筛法