Leetcode 204 Solution

This article provides solution to leetcode question 204 (count-primes)

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

Solution

class Solution {
public:
    int countPrimes(int n) {
        vector<bool> a;
        a.resize(n + 1);

        a[0] = true;
        a[1] = true;

        for (int i = 2; i < n; i++)
        {
            if (a[i])
                continue;

            for (int j = 2; j <= n / i; j++)
            {
                a[i * j] = true;
            }
        }

        int total = 0;
        for (int i = 0; i < n; i++)
            if (a[i] == false) total++;

        return total;
    }
};