📌  相关文章
📜  小于给定数字的素数 c++ 代码示例

📅  最后修改于: 2022-03-11 14:44:57.399000             🧑  作者: Mango

代码示例1
// This Function returns a vector containing all primes less than n using seive of eratosthenes
vector primes_less_than(long long n)
{
    vector ans(0);
    if (n <= 2)
        return ans;
    map is_prime;
    for(long long i=0; i < n; i++) 
          is_prime[i] = true;
    is_prime[0] = false;
    is_prime[1] = false;

    for (long long i = 2; i < sqrt(n); i++)
        if (is_prime[i])
            for (long long j = i * i; j < n; j += i)
                is_prime[j] = false;

    for(long long i=0; i < n; i++)
          if (is_prime[i] == true) 
              ans.push_back(i);
    return ans;
}