A: Sieve of Eratosthenes (Sieve of Eratosthenes)
1. The basic idea of the algorithm:
If a number is prime , then its multiples certainly non-quality , linear table using the pre-defined way to play table quality non-marking, the remaining number is prime.
2. The selection process:
//#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <algorithm> #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdio.h> #include <queue> #include <stack>; #include <map> #include <set> #include <ctype.h> #include <string.h> #include <vector> #define ME(x , y) memset(x , y , sizeof(x)) #define SF(n) scanf("%d" , &n) #define rep(i , n) for(int i = 0 ; i < n ; i ++) #define INF 0x3f3f3f3f #define PI acos(-1) the using namespace STD; { typedef Long Long LL; const int MOD = 1E9 +. 7; const int N = 100009; int VIS [N]; int A [N]; // prime number table int main () { VIS [. 1] = 0; for (int i = 2; i <= N ; i ++) // marked as prime VIS [I] =. 1; int m = Floor (sqrt (N) +0.5); for (int I = 2; I <= m; I ++ ) { IF (VIS [I]) { for (int J = I * I; J <= N; J + = I) { VIS [J] = 0; // use prime number multiples is composite filter } } } L = 0 int; for (int I =. 1; I <= N; I ++) IF (VIS [I]) { a[l++] = i ; cout << i << endl ; } } return 0; }
Summary: Eratosthenes screening method, while high efficiency, but Eratosthenes screening done a lot of wasted effort, a number will be screened to several times, the last time complexity is O (nloglogn), for the average prime algorithm in terms of already very efficient, but Euler screening time complexity is only O (n).
II: Euler screening
1. The basic idea of the algorithm:
prime prime array is incremented when i divisible prime [j], then i * prime [j + 1] This composite number must have been prime [j] multiplied by some number screened out.
Because i contains prime [j], prime [j ] [j + 1] is smaller than the prime. The next prime empathy. So I do not go on the screen.
I% prme satisfied before [j] == 0, and this condition is satisfied when the first condition changes, prime [j] must be prime [j] * i minimum quality factor.
//#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <algorithm> #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdio.h> #include <queue> #include <stack>; #include <map> #include <set> #include <ctype.h> #include <string.h> #include <vector> #define ME(x , y) memset(x , y , sizeof(x)) #define SF(n) scanf("%d" , &n) #define rep(i , n) for(int i = 0 ; i < n ; i ++) #define INF 0x3f3f3f3f #define PI acos(-1) the using namespace STD; for (int I = 0; I <L; I ++) typedef Long Long LL; const int MOD = 1E9 +. 7; const int N = 100009; int VIS [N]; int A [N]; // prime number table int main () { VIS [. 1] = 0; for (int i = 2; i <= N ; i ++) // marked as prime VIS [I] =. 1; int L = 0; for (int I = 2; I <= N; I ++) { IF (VIS [I] ) { A [L ++] = I; } for (int J = 0; J <L && I * A [J] <= N; J ++) { VIS [A [J] * I] = 0; IF (I% a [j] == 0) break ; // every composite number will be to the minimum quality factor sieve } } COUT << a [I] << endl; return 0; }