program SoNguyenTo;
const
KICH_THUOC_MAX = 100;
var
mang: array[1..KICH_THUOC_MAX] of Integer;
soLuongPhanTu, i, j: Integer;
soLuongSoNguyenTo: Integer;
procedure NhapMang();
begin
Write('Nhap so phan tu cua mang: ');
ReadLn(soLuongPhanTu);
WriteLn('Nhap cac phan tu cua mang:');
for i := 1 to soLuongPhanTu do
begin
Write('mang[', i, '] = ');
ReadLn(mang[i]);
end;
end;
function KiemTraSoNguyenTo(so: Integer): Boolean;
var
k: Integer;
begin
if so <= 1 then
KiemTraSoNguyenTo := False
else if so <= 3 then
KiemTraSoNguyenTo := True
else if (so mod 2 = 0) or (so mod 3 = 0) then
KiemTraSoNguyenTo := False
else
begin
k := 5;
while k * k <= so do
begin
if (so mod k = 0) or (so mod (k + 2) = 0) then
begin
KiemTraSoNguyenTo := False;
Exit;
end;
k := k + 6;
end;
KiemTraSoNguyenTo := True;
end;
end;
procedure InCacSoNguyenToTrongMang();
begin
WriteLn('Cac so nguyen to trong mang la:');
for i := 1 to soLuongPhanTu do
begin
if KiemTraSoNguyenTo(mang[i]) then
Write(mang[i], ' ');
end;
WriteLn;
end;
function DemSoNguyenToTrongMang(): Integer;
begin
soLuongSoNguyenTo := 0;
for i := 1 to soLuongPhanTu do
begin
if KiemTraSoNguyenTo(mang[i]) then
soLuongSoNguyenTo := soLuongSoNguyenTo + 1;
end;
DemSoNguyenToTrongMang := soLuongSoNguyenTo;
end;
begin
NhapMang();
soLuongSoNguyenTo := DemSoNguyenToTrongMang();
WriteLn('So luong so nguyen to trong mang la: ', soLuongSoNguyenTo);
InCacSoNguyenToTrongMang();
end.