204Count Primes

来源:互联网 发布:手机mac地址是什么 编辑:程序博客网 时间:2024/06/01 15:22
class Solution(object):
    def countPrimes(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n<2:
            return 0
        A=[True]*n
        for i in range(2,int(n**0.5)+1):
            if A[i]:
                for j in range(i**2,n,i):
                    A[j]=False

        return sum(A)-2

https://www.youtube.com/watch?v=whwlDCExMpY



def eratosthenes(n):    P = [i for i in range(2, n+1)]    p = 0    while True:        for i in P[p + 1:]:            if i % P[p] == 0:                P.remove(i)        if P[p]**2 >= P[-1]:            break        p += 1    return P
https://zh.wikipedia.org/wiki/%E5%9F%83%E6%8B%89%E6%89%98%E6%96%AF%E7%89%B9%E5%B0%BC%E7%AD%9B%E6%B3%95