8/23/2020

[LeetCode] 204. Count Primes

 Problem : https://leetcode.com/problems/count-primes/


class Solution:
    def countPrimes(self, n: int) -> int:
        
        isPrime = [True] * (n + 2)
        
        isPrime[0] = False
        isPrime[1] = False
        
        
        i = 2
        
        while i * i < n:
            if isPrime[i]:
                j = i * i
                while j < n:
                    isPrime[j] = False
                    j += i
            
            i += 1
        
        result = 0
        for i in range(n):
            if isPrime[i]:
                result += 1
        
        return result

No comments:

Post a Comment