/*这是最经典的算法*/
data prime(keep=i);
i=2;output;
i=3;output;
do i=2 to 1000;
do j=2 to int(sqrt(i));
if mod(i,j)=0 then leave;
else if j=int(sqrt(i)) then output;
end;
end;
run;
data prime(keep=i);
do i=2 to 1000;
do j=2 to 1000;
if mod(i,j)=0 and i^=j then isprime=0;
end;
if isprime^=0 then output;
isprime=1;
end;
proc print;
run;
shiyiming提供的这个算法是比较快,在一般C++的书上提到求素数的算法,好像用这个的比较多。如果可以用机器性能换算法效率,下面的代码可能可读性会更强些:
[code:asu6zl7n]data prime(keep=x);
do x=2 to 1000 by 1;
do i=2 to x by 1 while (mod(x,i)^=0);
end;
if i=x then output;
end;
run;[/code:asu6zl7n]