64,637
社区成员
发帖
与我相关
我的任务
分享
//以下是经典的开根号的算法
#include<math.h>
#include <stdio.h>
#define SIZE 1000
void main()
{
int i,n,cnt=0;
printf("%d ", 2); //素数2单独输出
for(n=3; n<=SIZE; n+=2) {
int temp=int(sqrt(n));
for(i=2; i<=temp; i++)
if(n %i == 0)
break; //执行break时为非正常结束循环
if(i>temp)
{
++cnt;
printf("%d ", n);
if(cnt%10==0)
printf("\n");
}//输出一个素数
}
printf("\n");
}
#include<math.h>
#include <stdio.h>
#include <time.h>
#define SIZE 1000
void main()
{
int i,n,cnt=0;
clock_t start,end;
start=clock();
printf("%d ", 2); //素数2单独输出
for(n=3; n<=SIZE; n+=2) {
int temp=int(sqrt(n));
for(i=2; i<=temp; i++)
if(n %i == 0)
break; //执行break时为非正常结束循环
if(i>temp)
{
++cnt;
printf("%d ", n);
if(cnt%10==0)
printf("\n");
}//输出一个素数
}
printf("\n");
end=clock();
printf("%lf",(long float)(end-start)/CLK_TCK);
}