easy to understand Python Solution

  • 0

    Firstly take an array of Size n with all the values as True.
    Next make 0,1 as False and then from 4-n make all even number False.
    Now check from 3 to N if there is an odd multiple of i make it false.

    class Solution(object):
    def countPrimes(self, n):
    :type n: int
    :rtype: int
    isPrimes = [True]*n
    isPrimes[0:2] = [False]2
    isPrimes[4:n:2] = [False]len(isPrimes[4:n:2])
    for i in range(3,n,2):
    if isPrimes[i] == True:
    2] = [False]len(isPrimes[ii:n:i])

        return sum(isPrimes)


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.