Acwing 868. 筛质数 欧拉筛模板、埃氏筛模板

埃氏筛

#include <iostream>
#include <algorithm>

using namespace std;

const int N= 1000010;

int primes[N], cnt;
bool st[N];

void get_primes(int n)
{
    
    
    for (int i = 2; i <= n; i ++ )
    {
    
    
        if (st[i]) continue;
        primes[cnt ++ ] = i;
        for (int j = i + i; j <= n; j += i)
            st[j] = true;
    }
}

int main()
{
    
    
    int n;
    cin >> n;

    get_primes(n);

    cout << cnt << endl;

    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/49975/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

欧拉筛

#include <iostream>
#include <algorithm>

using namespace std;

const int N= 1000010;

int primes[N], cnt;
bool st[N];

void get_primes(int n)
{
    
    
    for (int i = 2; i <= n; i ++ )
    {
    
    
        if (!st[i]) primes[cnt ++ ] = i;
        for (int j = 0; primes[j] <= n / i; j ++ )
        {
    
    
            st[primes[j] * i] = true;
            if (i % primes[j] == 0) break;
        }
    }
}

int main()
{
    
    
    int n;
    cin >> n;

    get_primes(n);

    cout << cnt << endl;

    return 0;
}

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/49975/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/qq_52358098/article/details/113728570