素数的高效算法---- Sieve of Eratosthenes

tech2022-09-14  96

class Solution { public: int countPrimes(int n) { vector<bool> isprime(n,true); int count=0; for(int i = 2;i*i < n; ++i){ if(isprime[i]){ for(int j = i*i;j < n;j += i){//使用j += i实现了i的倍数的排除 isprime[j] = false; } } } for(int i=2;i <n ;i ++){ if(isprime[i]) count++; } return count; } };

 

最新回复(0)