筛法求素数(Pascal)

2025-01-04 05:09:35
推荐回答(2个)
回答1:

Program Dt5_2_4;
Const N=200;
Type QSET = Set Of 2..N;
Var ss,sz:QSET;
Total,i,j:Integer;
Begin
sz := [2..N];
ss := [];
i := 2;
Repeat
While Not (i In sz) Do Inc(i);
ss := ss+[i];
For j := 1 To N Div i Do sz := sz-[i*j];
Until sz = [];
Total := 0;
For i := 2 To N Do
If i In ss Then Begin
Inc(Total);
Write(i:4);
If Total Mod 10 =0 Then WriteLn;
End;
WriteLn('Count=',Total);
ReadLn;
End.
我运行过!可以的,你试试!!!
望采纳!!!

回答2:

var zs:array[2..44722]of boolean;
begin
readln(n);
fillchar(zs,sizeof(zs),false);
for a:=2 to sqrt(n) do
begin
if not(zs[a]) then
b:=a shl 1;
while b<=n do
begin
zs[b]:=true;
inc(b,a)
end;
end;
for a:=2 to n do
if not(zs[a]) then writeln(a);
end;