O (n) Filter primes
#include <bits/stdc++.h>using namespace Std;const int M = 1e6 + ten; int mindiv[m];//min factorization int prim[m], pnum;//per number prime bool vis[m]; void Prim () {for (int i = 2; i < M; i + +) { if (!vis[i]) { mindiv[i] = i; prim[pnum++] = i; } for (int j = 0; J < Pnum; J + +) { if (I*prim[j] >= M) break; vis[I*prim[j]] = 1; Mindiv[i] = prim[j]; if (i% prim[j] = = 0) break;}}} int main () { prim (); return 0;}
Euler function O (n): Phi[i] is the number of the number of I coprime within the range of <= I
#include <bits/stdc++.h>using namespace Std;const int M = 1e6 + ten; int mindiv[m]; int prim[m], pnum; bool Vis[m]; I NT Phi[m]; Euler function int Euler () { phi[1] = 1; for (int i = 2; i < M; i + +) { if (Vis[i] && phi[i] = = 0) {Phi[i] = pnum;} if (!vis[i]) { prim[pnum++] = i; Phi[i] = i-1; MinDiv [i] = i; } for (int j = 0; J < Pnum; J + +) { if (i * prim[j] >= M) break; vis[I*prim[j]] = 1; MinDiv [i*prim[j]] = prim[j]; if (i% prim[j] = = 0) break; else phi[i*prim[j]] = phi[i] * phi[prim[j];} } } int main () { Euler (); return 0;}
Linear sieve && Euler functions for prime numbers