用pascal编写1到100所有素数的和

来源:百度知道 编辑:UC知道 时间:2024/06/12 08:35:58

方法1:最原始的方法(枚举,编译过):
var answer,i,j:longint;//循环变量定义为LONGINT类型,便于改动
can:boolean;
begin
answer:=0;
for i:=2 to 100 do
begin
can:=true;
for j:=2 to trunc(sqrt(i)) do
if i mod j=0 then
begin
can:=false;
break;
end;
if can then answer:=answer+i;
end;
writeln(answer);
end.
方法2:用筛选法求素数,然后累加:(没编译)
var i:integer;
answer:longint;
temp:array[2..100]of boolean;
procedure make_primetable(n:longint);
var i,j:longint;
begin
fillchar(temp,sizeof(temp),true
for i:=1 to trunc(sqrt(n)) do
if temp[i] then
begin
j:=i;
while j<=n do
begin
inc(j,i);
temp[j]:=false;
end;
end;
end;
begin
make_primetable(100);
answer:=0;
for i:=