procedure Primz(maxzahl: longint; ts: TStrings);
var x, y, z, i: longint;
a: array of longint;
procedure dazu;
begin
i := length(a);
setlength(a, i + 1);
a[i] := z;
end;
begin
ts.clear;
if maxzahl >= 2 then begin
a := nil;
z := 2;
dazu;
x := 3;
while x <= maxzahl do begin
y := 0;
while y <= i do begin
z := x mod a[y];
if z = 0 then y := i + 1 else inc(y);
end;
if z <> 0 then begin
z := x;
dazu;
end;
inc(x, 2);
end;
for x := 0 to length(a) - 1 do ts.add(inttostr(a[x]));
a := nil;
end;
end;
// Beispielaufruf
procedure TForm1.Button1Click(Sender: TObject);
begin
button1.enabled := false;
primz(5000, Listbox1.Items);
button1.enabled := true;
end;