LeetCode count-primes

2021-01-21

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

JavaScript

var countPrimes = function (n) {
        const table = Array(n + 1).fill(true);
        for (let i = 2; i <= n; i++) {
            if (table[i]) {
                const step = i;
                for (let num = step + step; num <= n; num += step) {
                    table[num] = false;
                }
            }
        }
        let count = 0;
        for (let i = 2; i < n; i++) {
            if (table[i]) {
                count++;
            }
        }
        return count;
    };