Eratosthenes求素数法

#include <iostream>
using namespace std;
#define Max 22500
int main()
{
unsigned int i,range,factor,k;
int sieve[Max]={0};
printf("please input the range:");
scanf("%d",&range);
for (i=2;i<=range;i++)
{
sieve[i-1]=1;
}

factor=2;
while (factor<=range)
{
if (sieve[factor-1]==1) //找出最小的数
{
printf("%d/t",factor);
k=factor;
while(k<=range)//去除该数的倍数
{
sieve[k-1]=-1;
k=k+factor; //此处注意表示k的倍数!!
}
}
factor++;
}
return 0;
}

你可能感兴趣的:(OS)