cho biết trong phạm vi từ M đến N có bao nhiêu số nguyên tố( N >=M , N,M nguyên dương)
2 câu trả lời
Program BTT;
Uses crt;
Var m,n,d,i: longint;
Function ngto(a: longint): boolean;
Var i: longint;
Begin
If a<2 then exit(false);
For i:=2 to a div 2 do
If a mod i = 0 then exit(false);
exit(true);
End;
Begin
Clrscr;
Write('Nhap m: '); Readln(m);
Write('Nhap n: '); Readln(n);
d:=0;
If m<=n then
For i:=m to n do
If ngto(i) then inc(d);
If m>n then Write('Sai yeu cau de')
Else
Write('Co ',d,' so nguyen to');
Readln;
End.
var m,n,i,d:longint;
function checkprime(x:longint):boolean;
var c:longint;
begin
if x<2 then exit(false);
for c:=2 to trunc(sqrt(x)) do if x mod c = 0 then exit(false) else exit(true);
end;
begin
write('Nhap m: '); readln(m);
write('Nhap n: '); readln(n);
for i:=m to n do if checkprime(i) then inc(d);
write('Tu ',m,' den ',n,' co ',d,' so nguyen to);
readln;
end.