program abcdef;
uses Crt;
var
k, lowerLimit, upperLimit, i, j, reversed, temp, remainder: integer;
isPrime, isPalindrome: boolean;
begin
clrscr;
write('Nhap so chu so k (1<=k<=9): ');
readln(k);
lowerLimit := 1;
for i := 1 to k - 1 do
lowerLimit := lowerLimit * 10;
upperLimit := lowerLimit * 10 - 1;
writeln('Cac so nguyen to doi xung co ', k, ' chu so la:');
for i := lowerLimit to upperLimit do
begin
// Kiểm tra số nguyên tố
isPrime := True;
if i < 2 then
isPrime := False
else
for j := 2 to trunc(sqrt(i)) do
if i mod j = 0 then
beginisPrime := False;break;end;
// Kiểm tra số đối xứng
if isPrime then
begin
reversed := 0;
temp := i;
while temp <> 0 dobeginremainder := temp mod 10;
reversed := reversed * 10 + remainder;
temp := temp div 10;
end;
isPalindrome := (i = reversed);
if isPalindrome then
writeln(i);
end;
end;
readln;
end.