reference
Prime 筛
Erichsen screen
This is well understood that, considering each of the small to large number, this number will be a multiple of the number of engagement can be labeled, but this would be a lot number of repeating sieve screen, complexity is \ (O (n \ log \ logn) \) , so you can use Euler screen.
int Eratosthenes(int n) {
int cnt = 0;
memset(is_prime, 1, sizeof(is_prime));
is_prime[0] = is_prime[1] = 0;
for (int i = 2; i <= n; ++i)
if (is_prime[i]) {
prime[++cnt] = i;
for (int j = i * 2; j <= n; j += i) is_prime[j] = 0;
}
return cnt;
}
Euler screen
That linear sieve, sieve equivalent optimized version of the Eppendorf, so that each screen is a composite number only once.
Complexity: \ (O (n-) \)
void Euler(int n) {
int cnt = 0;
memset(vis, 0, sizeof(vis));
for (int i = 2; i <= n; ++i) {
if (!vis[i]) prime[++cnt] = i;
for (int j = 1; j <= cnt && i * prime[j] <= n; ++j) {
vis[i*prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
}
Euler function
Linear sieve.
void phi_table(int n) {
memset(phi, 0, sizeof(phi));
phi[1] = 1;
for (int i = 2; i <= n; ++i)
if (!phi[i]) {
for (int j = i; j <= n; j += i) {
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
}