素数筛法优化

#include<iostream>
#include<cstring>
using namespace std;
const int p=10000;
int main()
{
    bool isprime[10005];
    int a[10000],count;
    count=0;
    memset(isprime,true,sizeof(isprime));
    for(int i=2;i<=p;i++)
    {
        if(isprime[i])
        a[++count]=i;
        for(int j=1;j<=count&&a[j]*i<=p;j++)
        {
            isprime[a[j]*i]=false;
            if(i%a[j]==0)
            break;
        }
    }
    for(int i=1;i<=count;i++)
    cout<<a[i]<<" ";
}

猜你喜欢

转载自www.cnblogs.com/Leozi/p/10835404.html