19 行代码C++算法模板
线性筛(欧拉筛)
线性筛法,筛出 1 ~ N 中所有的质数,时间复杂度 O(N)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
bool f[N+5];
int prime[N], cnt = 0;
#质数#质数筛
线性筛法,筛出 1 ~ N 中所有的质数,时间复杂度 O(N)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
bool f[N+5];
int prime[N], cnt = 0;
埃氏筛法,筛出 1 ~ N 中所有的质数,时间复杂度 O(N log log N)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
bool f[N+5];
int prime[N], cnt = 0;
简单的质数判定代码,时间复杂度 O(sqrt(x))
bool is_prime(int x) {
if (x < 2) return false;
for (int i = 2; i*i <= x; i++)
if (x % i == 0)
return false;
return true;