Program Nguyen_to;
uses crt;
Var n,a,j,k,dem,kt,h1,h2:integer;
ngto:array[1..100]of integer;
{-----------------tim-so-nho-nhat-lon-hon-----------------------}
Function NT(n:integer):Boolean;
Var ok: Boolean;
i: integer;
Begin
ok:=true;
for i:=2 to n-1 do
if (n mod i)= 0 then ok:=ok and false;
if n < 2 then NT:=false else NT:=ok;
End;
{------------chuong-trinh-chinh---------------------}
Begin
clrscr;
Write('Nhap n: ');Readln(n);
a:=n;
Repeat
a:=a+1;
Until NT(a);
{--------------------tim-so-lon-nhat-nho-hon--------------------}
dem:=0;
for j:=2 to n do
begin
kt:=0;
for k:=2 to j-1 do
if j mod k=0 then kt:=1;
if kt=0 then
begin
inc(dem);
ngto[dem]:=j;
end;
end;
h1:=abs(a-n);
h2:=abs(ngto[dem]-n);
if h1<h2 then writeln('so nguyen to gan ',n,' nhat la: ',a)
else writeln('so nguyen to gan ',n,' nhat la: ',ngto[dem]);
Readln;
End.
có gì không hiểu thì cứ hỏi mình hoặc chị @Nguyễn Minh Lệ
Program hotrotinhoc;
var i,t1,n: integer;
function nt(x: integer): boolean;
var j: integer;
begin
nt:=false;
if x<2 then exit;
for j:=2 to trunc(sqrt(x)) do
if x mod j=0 then exit;
nt:=true;
end;
begin
write('N='); readln(n);
for i:=1 to n-1 do
if nt(i) then t1:=i;
write(t1);
readln
end.