标题: 列出10000以内的素数??? [打印本页] 作者: shiyiming 时间: 2008-7-28 16:42 标题: 列出10000以内的素数??? 用sas数据步列出10000以内的素数,编写程序……
哪位大侠帮忙下作者: shiyiming 时间: 2008-7-29 00:19 标题: Re: 列出10000以内的素数??? Do you have to do it in SAS? I got this C code from internet, but I just don't have time to recode it in SAS. Try if you can code it in SAS under this idea. Good luck.
[code:6z79nodi]#include<stdio.h>
#include<time.h>
#define MAX 100000010
int n,p[MAX],tot=0;
double s,t;
FILE *fp;
void prime()
{ int i,j,t=sqrt(n)+1;
for(i=2;i<t;i++)
if(p[i])
{ fprintf(fp,"%d\n",i);
tot++;
j=i+i;
while(j<n)
{ p[j]=0;
j+=i;
}
}
for(i=t+1;i<n;i++)
if(p[i])
{ tot++;
fprintf(fp,"%d\n",i);
}
}
main()
{ int i;
fp=fopen("prime.txt","w");
scanf("%d",&n);
s=clock();
for(i=0;i<n;i++)
p[i]=1;
prime();
t=clock();
fprintf(fp,"Num = %d\nTime = %.0lf ms\n",tot,t-s);
fclose(fp);
}
本机测试结果:10000000用时1156ms(1.156秒)
100000000用时80秒(较慢,主要是内存太少,反复读硬盘的原因)
[/code:6z79nodi]作者: shiyiming 时间: 2008-7-29 07:28 标题: Re: 列出10000以内的素数??? 早在N年前就发过了,拜托大家在提问前一定自己先在坛子里搜一下,很多问题都是重复的。
<!-- l --><a class="postlink-local" href="http://www.mysas.net/forum/viewtopic.php?f=4&t=97">viewtopic.php?f=4&t=97</a><!-- l -->作者: shiyiming 时间: 2008-7-29 17:28 标题: Re: 列出10000以内的素数??? <!-- l --><a class="postlink-local" href="http://www.mysas.net/forum/viewtopic.php?f=4&t=97&p=12011&hilit=%E7%B4%A0+%E6%95%B0#p12011">viewtopic.php?f=4&t=97&p=12011&hilit=%E7%B4%A0+%E6%95%B0#p12011</a><!-- l -->作者: shiyiming 时间: 2008-8-5 09:23 标题: Re: 列出10000以内的素数??? 谢谢3楼作者: shiyiming 时间: 2009-7-4 00:27 标题: Re: 列出10000以内的素数??? data a;
do t=2 to 10000;
output;
end;
data a1;
set a;
i=2;
do while (mod(t,i)^=0);
i+1;
end;
if i<t then delete;
else y=t;
proc print;var y;run;
我是初学的编了一个别见笑