204. Count Primes

一、题目

  1、审题

  2、分析

    统计 < n 的数字中质数的个数。

二、解答

  1、思路:

    方法一、

      采用一个大小为 n 的 bool 数组,标记非质数。

    public int countPrimes(int n) {
        
        int count = 0;
        boolean[] notPrime = new boolean[n];
        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;
    }
    
原文地址:https://www.cnblogs.com/skillking/p/9821314.html