计数质数

统计所有小于非负整数 n 的质数的数量。


image.png
class Solution(object):
    def countPrimes(self, n):
        """
        :type n: int
        :rtype: int
        """
        count = 0
        for i in range(n):
            if is_prime(i) == True:
                # print(i)
                count += 1
        return count



print('begin')
def is_prime(num):
    result = True
    if num == 1:
        result = False
    elif num == 0:
        result = False
    elif num == 2:
        result = True
    elif num > 1:
        for i in range(2, num):
            if num % i == 0:
                result = False
                break
    return result


print(Solution().countPrimes(10000))
print('end')

你可能感兴趣的:(计数质数)