Как проверить простое число в Delphi

Я хочу прочитать все простые числа от 1 до 10000 в динамический массив и все не простые числа в другой динамический массив, а затем прочитать простой массив вrichedit1 пока что у меня есть:

procedure primearrays;
var
  j, k, l, i, m: integer; // k is the number I am testing for prime number
  // j is used in the for loop to check all numbers smaller than k to see if k is dividable by j
  // l is just a variable set to k mod j to make the if run more smoothly
  // i is the length of the array anotprime
  // m is used to set the length of the array aprime
  bflag: boolean; // bflag is to show if this number is a prime number
  aprime, anotprime: array of integer;
  // aprime is the array of prime and anotprime is the array of nonprime numbers
begin
  j := 0;
  i := 0;
  l := 0;
  richedit1.Lines.Clear;
  bflag := false;
  for k := 2 to 10000 do
  begin
    j := 0;
    while not(j = (k - 1)) do
    begin
      inc(j);
      l := k mod j;
      if (l = 0) then
      begin
        bflag := false;
        inc(i);
        setlength(anotprime, i);
        anotprime[i - 1] := k;
        j := k - 1;
      end
      else
      begin
        bflag := true;
      end;
    end;
    m := -1;
    if (bflag) then
    begin
      inc(m);
      setlength(aprime, m);
      aprime[m - 1] := k;
      richedit1.Lines.Add(inttostr(aprime[l-1]));
    end;
  end;
end;

но это не похоже на работу. Он помещает все целые числа вanotprime.

Ответы на вопрос(2)

Ваш ответ на вопрос