First, for example, to discuss the Sieve method for the prime number within 1000000
Eratosthenes Sieve Method (Eratosthenes sieve method)
Complexity of Time: O (N*LOGLOGN)
Space complexity: O (N)
Code:
#include <map>#include <set>#include <list>#include <cmath>#include <deque>#include <queue>#include <stack>#include <bitset>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <sstream>#include <utility>#include <iostream>#include <algorithm>#include <functional>#define LL Long Long#define INF 0x7fffffffusing namespace STD;Const intMAXN =1000005;BOOLVIS[MAXN];intPRIME[MAXN];intTotvoidInit () {tot =0;memset(Vis,false,sizeof(VIS)); for(inti =2; i < MAXN; i + +) {if(!vis[i]) {Prime[tot + +] = i; for(intj = i *2; J < Maxn; J + = i) {Vis[j] =true; } } }}intMain () {init (); for(inti =0; I < -; i + +) {cout<< Prime[i] <<" "; }return 0;}
Euler Sieve method (Euler sieve method)
Each number is filtered only by its smallest mass factor, so the time complexity is O (N).
This type of linear sieve method
Code:
#include <map>#include <set>#include <list>#include <cmath>#include <deque>#include <queue>#include <stack>#include <bitset>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <sstream>#include <utility>#include <iostream>#include <algorithm>#include <functional>#define LL Long Long#define INF 0x7fffffffusing namespace STD;Const intMAXN =1000005;BOOLVIS[MAXN];intPRIME[MAXN];intTotvoidInit () {tot =0;memset(Vis,false,sizeof(VIS)); for(inti =2; i < MAXN; i + +) {if(!vis[i]) Prime[tot + +] = i; for(intj =0; J < tot; J + +) {if(i * prime[j] > MAXN) Break; Vis[i * Prime[j]] =true;if(i% prime[j] = =0) Break; } }}intMain () {init (); for(inti =0; I < -; i + +) {cout<< Prime[i] <<" "; }return 0;}
Euler function by linear sieve method
Code:
#include <map>#include <set>#include <list>#include <cmath>#include <deque>#include <queue>#include <stack>#include <bitset>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <sstream>#include <utility>#include <iostream>#include <algorithm>#include <functional>#define LL Long Long#define INF 0x7fffffffusing namespace STD;Const intMAXN =1000005;BOOLVIS[MAXN];intPRIME[MAXN];intFAI[MAXN];intTotvoidInit () {memset(Vis,false,sizeof(VIS)); fai[1] =1; tot =0; for(inti =2; i < MAXN; i + +) {if(!vis[i]) {Prime[tot + +] = i; Fai[i] = i-1; } for(intj =0; J < tot; J + +) {if(i * prime[j] >= MAXN) Break; Vis[i * Prime[j]] =true;if(i% prime[j] = =0) {Fai[i * prime[j]] = fai[i] * Prime[j]; Break; }Else{Fai[i * prime[j]] = fai[i] * (Prime[j]-1); } } }}intMain () {init (); for(inti =1; I < -; i + +) {cout<< Fai[i] <<" "; }return 0;}
The linear sieve method to find the Möbius function
Code:
#include <map>#include <set>#include <list>#include <cmath>#include <deque>#include <queue>#include <stack>#include <bitset>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <sstream>#include <utility>#include <iostream>#include <algorithm>#include <functional>#define LL Long Long#define INF 0x7fffffffusing namespace STD;Const intMAXN =1000005;BOOLVIS[MAXN];intPRIME[MAXN];intMU[MAXN];//Möbius functionintTotvoidInit () {memset(Vis,false,sizeof(VIS)); mu[1] =1; tot =0; for(inti =2; i < MAXN; i + +) {if(!vis[i]) {Prime[tot + +] = i; Mu[i] =-1; } for(intj =0; J < tot; J + +) {if(i * prime[j] >= MAXN) Break; Vis[i * Prime[j]] =true;if(i% prime[j] = =0) {Mu[i * Prime[j]] =0; Break; }Else{Mu[i * prime[j]] =-mu[i]; } } }}intMain () {init (); for(inti =1; I < -; i + +) {cout<< Mu[i] <<" "; }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Number theory-linear sieve method and integrable function