Viết chương trình in ra màn hình tất cả các số nguyên tố bé hơn hoặc bằng n

2 câu trả lời

Function ktnt(x:integer):boolean;

 begin

   Ktnt:=true;

   For j:=2

to round(sqrt(n)) do if n mod i =0 then 

      Begin

         Ktnt:=false;

          Break;

       End;

 end;

begin

...

for i:=1 to n do if ktnt(i)=true then write(i,’ ‘);

...

end;//nhap xuat tu lam

Var n,i:integer;

Function nt(x:integer):boolean;

Var i:integer;

BeginNt:=false;

If x<2 then exit;

For i:=2 to trunc(sqrt(x)) doIf x mod 2=0 then exit;

Nt:=true;

BeginWrite(‘nhap n:’);

readln(N);

For i:=2 to n doIf nt(i) then write(i,’ ‘);

Readln

End.

Câu hỏi trong lớp Xem thêm