B
B
Brunoz2021-12-03 11:58:51
Pascal
Brunoz, 2021-12-03 11:58:51

How to find the smallest prime number in an array, pascal?

const

  n = 100;

type
  massive = array[1..n] of integer;

function massiv(x: byte): char;
var
  i: byte;
  a: massive;
begin
  for i := 1 to x do          
  begin
    a[i] := random(100);        
    write(a[i]:3);
  end;
end;

var
  x: byte;

begin
  write('Введите размер массива: '); read(x);
  write('Массив: '); massiv(x);
  
end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2021-12-03
@HemulGM

you pulled up

type
  TArray = array of integer;

function IsPrime(const Value: integer): Boolean;
var
  N: integer;
begin
  for N := 2 to Value - 1 do
    if Value mod N = 0 then
      Exit(False);
  Result := True;
end;

function PrimeMin(const A: TArray): Integer;
var
  i: Integer;
begin
  Result := -1;
  for i := 0 to High(A) do
    if IsPrime(A[i]) then
      if (Result = -1) or (Result > A[i]) then
        Result := A[i];
end;

function FillRandom(Count: Integer): TArray;
var
  i: Integer;
begin
  Randomize;
  SetLength(Result, Count);
  for i := 0 to Count - 1 do
  begin
    Result[i] := Random(1000 - 2) + 2;
    write(Result[i]: 4);
  end;
  writeln;
end;

var
  Count: Integer;
  Items: TArray;

begin
  Write('Введите размер массива: ');
  Read(Count);
  Write('Массив: ');
  Items := FillRandom(Count);
  Write('Мин. простое: ', PrimeMin(Items));
  Readln;
  Readln;
end.

Just learn a little about the algorithm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question