Simple and fast


  • 0
    X
    public int countPrimes(int n) {
        final boolean[] notPrime = new boolean[n];
        int count = 0;
    
        for (int i = 2; i < n; i++) {
            if (notPrime[i] == false) {
                count++;
                for (int j = 2; i * j < n; j++) {
                    notPrime[i * j] = true;
                }
            }
        }
    
        return count;
    }
    

Log in to reply
 

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