Linear sub-sieve

#include <bits / STDC ++ H.>
the using namespace STD;
const int = MAXN 1E7;
BOOL VIS [MAXN];
int Prime [MAXN];
int F (int n) // count the number of prime numbers from 1 to n
{
    int CNT = 0;
    Memset (VIS, to false, the sizeof (VIS));
    for (int I = 2; I <= n-; I ++)
    {
        IF (VIS [I]!)
            Prime [CNT ++] = I;
        for (int J = 0; J <CNT && I * Prime [J] <= n-; J ++)
        {
            VIS [I * Prime [J]] =. 1;
            IF (I% Prime [J] == 0)
                BREAK;
        }
    }
    return CNT ;
}
/ * when i divisible prime [j]
then i * prime [j + 1] this composite number is definitely prime [j] is multiplied by a number screened out.
Because i contains prime [j], prime [j ] [j + 1] is smaller than the prime. * /

int main ()
{
    int n;cin>>n;
    cout<<f(n)<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/ljy08163268/p/11703770.html