Linear sieve
Idea: Each number has one and only one screen
Resolution: Each number is only (non-native) screen factor to its maximum
Together provided the smallest prime factor of the number of x p, x = pq, is easy to know p <= q
We let q x is only weed out, choose an enumeration q
Enumeration q, retrieve all the p, weed out the number of x, left out
void sieve() {
for(int i=2;i<=m;i++) { //枚举q
if(v[i]==0) {
ps[++cnt]=i; //统计p
}
for(int j=1;j<=cnt;j++) { //枚举满足的p
if(i*ps[j]>m) break;
v[i*ps[j]]=1;
if(i%ps[j]==0) break; //条件判断
}
}
} //统计2到m的质数
The code means: Let q be the smallest prime factor of u, q = uv
1. If p <= u (p is a prime number), then p is the smallest prime factor of x = pq and satisfies the condition, the screen to x
2. If p> u, x = pq = puv = u * (pv), p x is not the smallest prime factor, this number will be PV (greater than q) filtering out, repeat, can not jump out of the screen x
So far, the accuracy and adequacy of the algorithm are proved.
(Original)