计算质数问题

Posted wangyufeng99

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算质数问题相关的知识,希望对你有一定的参考价值。

class Solution 
    public int countPrimes(int n) 
        int[] isPrime = new int[n];
        Arrays.fill(isPrime, 1);
        int ans = 0;
        for (int i = 2; i < n; ++i) 
            if (isPrime[i] == 1) 
                ans += 1;
                if ((long) i * i < n) 
                    for (int j = i * i; j < n; j += i) 
                        isPrime[j] = 0;
                    
                
            
        
        return ans;
    

以上是关于计算质数问题的主要内容,如果未能解决你的问题,请参考以下文章