美文网首页
Sieve Prime Numbers between 100

Sieve Prime Numbers between 100

作者: strivexj | 来源:发表于2017-10-18 12:20 被阅读0次
    /*************************************************************************
         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]);
    }
    

    相关文章

      网友评论

          本文标题:Sieve Prime Numbers between 100

          本文链接:https://www.haomeiwen.com/subject/yewsuxtx.html