JS Solution (since JS solutions are so few)


  • 0
    N
    /**
     * @param {number} n
     * @return {number}
     */
    var countPrimes = function(n) {
        if (n <= 1) return 0;// 1 is not a prime number
        
        var isPrime = [],
            count = 0;
    
        for (var i = 2; i < n; i++) {
            if (isPrime[i] == undefined) { // cannot use !isPrime[i] (isPrime[i] == false) has to be undefined 
                isPrime[i] = true;
                count++;
                for (var j = 2; j * i < n; j++) {
                    // mark the prime's multiples as nonprime
                    isPrime[i*j] = false;
                }
            }
        }
        
        return count;
    };
    
    

Log in to reply
 

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