素数打表法

来源:互联网 发布:怎么做淘宝店铺模板 编辑:程序博客网 时间:2024/06/05 00:28

输出1000之内的素数

#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#define N 1000using namespace std;int a[N];void prime(){    memset(a,0,sizeof(a));    a[0]=1;a[1]=1;    for(int i=2;i<sqrt(N);i++)    {        if(a[i])            continue;        for(int j=i*i;j<N;j+=i)        {            a[j]=1;        }    }}int main(){    int n,i;    prime();    int count1=0;    for(int i=2;i<N;i++)    {        if(a[i]==0)        {            printf("%d ",i);            count1++;          if(count1%10==0)            {                cout<<endl;            }        }    }    return 0;}


0 0
原创粉丝点击