以下是求n以内的素数的程序。只要输入n的值,就能求出n以内所有的素数。
var n,i,s:integer; yes:boolean;
begin
write('N='); readln(n); s:=0;
for n:=2 to n do
begin
yes:=true; {先假定n是素数}
for i:=2 to trunc(sqrt(n)) do
if n mod i=0 then begin yes:=false; break; end;
if yes then
begin
inc(s); write(n:5); {计数,输出}
if s mod 10=0 then writeln; {控制每行输出10个数}
end;
end;
writeln;
writeln('Total=',s);
end.