筛
作者:
Hanasaki
,
2021-12-02 11:35:13
,
所有人可见
,
阅读 215
// the Sieve of Eratosthenes
#include <bits/stdc++.h>
using namespace std;
signed main()
{
int n;
cin >> n;
vector<bool> NotPrime(n + 10);
vector<int> Prime(n + 10);
int tot = 0;
function<void(int)> EratoSieve = [&](int n)
{
for (int i = 2; i <= n; i ++)
{
if (!NotPrime[i])
{
Prime[tot ++] = i;
for (int j = i + i; j <= n; j += i) NotPrime[j] = true;
}
}
};
EratoSieve(n);
cout << tot << "\n";
return 0;
}
// the Sieve of Euler
#include <bits/stdc++.h>
using namespace std;
signed main()
{
int n;
cin >> n;
int tot = 0;
vector<int> Prime(n + 10);
vector<bool> NotPrime(n + 10);
function<void(int)> EulerSieve = [&](int n)
{
for (int i = 2; i <= n; i ++)
{
if (!NotPrime[i]) Prime[tot ++] = i;
for (int j = 0; Prime[j] <= n / i; j ++)
{
NotPrime[i * Prime[j]] = true;
if (i % Prime[j] == 0) break;
}
}
};
EulerSieve(n);
cout << tot << "\n";
return 0;
}
/*
筛法中若要数组Prime从1开始,需要++total,并且后续筛法中的j需要从1开始
否则则为0 -> total++ -> j from 0 to n
*/