/*************************************************************************
File Name: test.c
Author: cwj
Mail: 1003214597@qq.com
Created Time: Sat 14 Oct 2017 07:45:57 PM CST
************************************************************************/
//普通筛选法
#include<stdio.h>
int prime[100000000];
int vis[100000000];
int main()
{
int primeIndex=0;
freopen("prime.txt","w",stdout);
for (int i = 2; i < 100000000; ++i) {
if(!vis[i])prime[primeIndex++]=i;
for (int j = i*2; j <100000000; j+=i) {
vis[j]=1;
}
}
for (int i = 0; i < primeIndex; ++i) {
printf("%d",prime[i]+" ");
}
return 0;
}
//欧拉线性筛选法
#include<stdio.h>
int prime[100000000],vis[100000000];
int main()
{
freopen("prime.txt","w",stdout);
//线性素数筛
int top=0;
for(int i=2;i<=100000000;i++)
{
if(!vis[i])
prime[top++]=i;
for(int j=0;j<top&&prime[j]*i<=100000000;j++)
{
vis[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}
for(int i=0;i<top;i++)
printf("%d\n",prime[i]);
}
网友评论