FREE PASCAL
Viết chương trình tính tổng các số nguyên tố
Yêu cầu: Viết chương trình nhập vào 2 số tự nhiên a,b (a>=2, a { "@context": "https://schema.org", "@type": "QAPage", "mainEntity": { "@type": "Question", "name": " FREE PASCAL Viết chương trình tính tổng các số nguyên tố Yêu cầu: Viết chương trình nhập vào 2 số tự nhiên a,b (a>=2, a=2, a
var a,b: integer;
s,i: integer;
f1,f2: text;
Function nt(n:integer):boolean;
var i:integer;
begin
nt:=false;
for i:=2 to trunc(sqrt(n)) do
if n mod i=0 then exit;
if n<2 then exit;
nt:=true;
end;
begin
assign(f1,’tongnto.inp’);
reset(f1);
assign(f2,’tongnto.out’);
rewrite(f2);
repeat
readln(f1,a,b);
until (a>=2) and (a<b);
s:=0;
for i:=a to b do
if nt(i) then s:=s+i;
write(f2,’tong: ‘,s);
close(f1); close(f2);
end.
const fi=’tongnto.inp’;
fo=’tongnto.oup’;
var f:text;
a,b,i,s:longint;
funtion nt(x:longint):boolean;
var i:longint;
begin
nt:=false;
if x<2 then exit;
for i:=1 to x div 2 do
if x mod i=0 then exit;
nt:=true;
end;
begin
assign(f,fi);reset(f);readln(a,b);close(f);
assign(f,fo);rewrite(f);
if (a<2) or (a>b) then write(f,’Du lieu vao khong xac dinh’)
else
begin
s:=0;
for i:=a to b do
if nt(i)=true then s:=s+i;
write(f,s);
end;
close(f);
end.