LeetCode-【数学】判断素数

leetcode 204题目链接:计算质数

厄拉多塞筛法

def countPrimes(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n < 2:
            return 0
        res = 0
        arr = [0] * (n)
        for i in range(2,n):
            if arr[i] == 0:
                res+=1
                j = 1
                while j*i < (n):
                    arr[i*j]=1
                    j+=1
        return res

 

你可能感兴趣的:(python,LeetCode)