#include<iostream>
using namespace std;
const int N = 100010;
int n;
bool isprime[N];
int primes[N];
void euler_sieve(int n)
{
fill(isprime, isprime + n + 1, 1);
isprime[0] = isprime[1] = 0;
int id = 0;
for (int i = 2; i <= n; i++)
{
if (isprime[i])
primes[++id] = i;
for (int j = 1; j <= id && i * primes[j] <= n; j++)
{
isprime[i * primes[j]] = 0;
if (i % primes[j] == 0) break;
}
}
}
int main()
{
cin >> n;
euler_sieve(n);
for (int i = 2; i <= n; i++)
{
if (isprime[i])
cout << i << " ";
}cout << endl;
return 0;
}
欧拉筛素数
最新推荐文章于 2024-08-31 15:48:32 发布