So suchen Sie in Delphi nach einer Primzahl

Ich möchte alle Primzahlen zwischen 1 und 10000 in ein dynamisches Array und alle Nicht-Primzahlen in ein anderes dynamisches Array einlesen und dann das Primarray in @ einleserichedit1 soweit ich habe:

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;

aber das scheint nicht zu funktionieren. Es setzt alle ganzen Zahlen inanotprime.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage