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;
}
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;
}
相关文章推荐
- Read and write excel file with format by pandas and xlwt libraries of python
- ASP.NET-Active Direcotry编程示例
- 将文件转为byte[],通过ByteArrayOutputStream 实现
- PHP里还有这些好用的技巧
- 禁止解析 php
- C++第五次实验
- 如何搭建一个GitHub在自己的服务器上?
- YII 学习之 控制器传值到模板
- Java:内省(Introspector)深入理解
- python模块 json
- 对JAVA的集合的理解
- Javaweb学习之servlet
- spring3.1之后配置messageConverter
- C#DataTable
- 利用 perl 调用 Java 类,通过读取文件 获取调用类返回值
- python模块 pickle
- eclipse和myeclipse文件折叠方法
- 解析C++中四种强制类型转换的区别详解
- C++实验5—数组分离
- C++实验5-数组分离