您的位置:首页 > 编程语言

Eratosthenes 法找素数 (含代码)

2016-05-05 09:35 337 查看
#include<iostream>

using namespace std;

void Eratosthenes(bool *a, int n)

{

   a[1]=false;//a[0]不用

   int i;

   for(i=2; i<=n; i++)//筛法默认是素数
  a[i]=true;

   int p=2; //第一个筛孔

   int j=p*p;

   int c=0;

   while(j<=n)

   {

      while(j<=n)
 {
    a[j]=false;
j+=p;
 }
 p++;
 while(!a[p])//查找下一个素数
 p++;
 j=p*p;

   }

  

}

int main()

{

   

   const int n=100;

   bool a
;

   Eratosthenes(a,  n);

    for(int i=2; i<=n; i++)

   {

     if(a[i])
cout<<i<<" ";

   }

   return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: