program PrimeSequence;
const
MAX = 100;
type
ArrayOfInteger = array[1..MAX] of Integer;
var
arr: ArrayOfInteger;
n, i, j, count, maxCount, startIndex, maxStartIndex, sum: Integer;
isPrime: Boolean;
// Function to check if a number is prime
function CheckPrime(num: Integer): Boolean;
var
k: Integer;
begin
if num < 2 then
CheckPrime := False
else
begin
CheckPrime := True;
for k := 2 to Trunc(Sqrt(num)) do
begin
if num mod k = 0 then
begin
CheckPrime := False;
Break;
end;
end;
end;
end;
begin
// Input
Write('Nhap so phan tu cua day (2 < n < 100): ');
Readln(n);
// Check input validity
while (n <= 2) or (n >= 100) do
begin
Write('Nhap lai so phan tu cua day (2 < n < 100): ');
Readln(n);
end;
WriteLn('Nhap cac so nguyen duong:');
for i := 1 to n do
Readln(arr[i]);
// Find prime numbers in the sequence and the longest prime sequence
Write('Cac so nguyen to cua day la: ');
maxCount := 0;
sum := 0;
startIndex := 1;
maxStartIndex := 1;
for i := 1 to n do
begin
if CheckPrime(arr[i]) then
begin
Write(arr[i], '; ');
count := 1;
sum := arr[i];
for j := i + 1 to n do
begin
if CheckPrime(arr[j]) then
begin
count := count + 1;
sum := sum + arr[j];
end
else
Break;
end;
if count > maxCount then
begin
maxCount := count;
maxStartIndex := startIndex;
end
else if (count = maxCount) and (sum > arr[maxStartIndex]) then
begin
maxStartIndex := startIndex;
end;
startIndex := i + 1;
end;
end;
WriteLn;
// Output the longest prime sequence
Write('Day nguyen to lon nhat co ', maxCount, ' phan tu: ');
for i := maxStartIndex to maxStartIndex + maxCount - 1 do
Write(arr[i], '; ');
end.