我自己的更快的算法:
procedure TForm1.btn2Click(Sender: TObject);
const
PrimeNum='%d'
MMax=100000;
var
sDate, eDate : TDateTime;
i,j,tmp, iSqrt, MaxSqrt:Integer;
List: TList;
IsPrime: Boolean;
begin
sDate := Now;
with lst1.Items do
begin
List := TList.Create
//用数组应该更快
List.Capacity := 10000;
MaxSqrt := Trunc(Sqrt(MMax));
Clear;
BeginUpdate;
Add('2');
Add('3');
List.Add(Pointer(3));
j:=5;
while j<=MMax do
begin
IsPrime := True;
iSqrt := Trunc(Sqrt(j));
for i := 0 to List.Count - 1 do begin
tmp := Integer(List.Items);
if (tmp > iSqrt) then Break;
if ((j mod tmp)=0) then begin
IsPrime := False;
Break;
end;
end;
if IsPrime then begin
Add(Format(PrimeNum,[j]));
if j <= MaxSqrt then List.Add(Pointer(j));
end;
Inc(j, 2);
end;
EndUpdate;
List.Free;
end;
eDate := Now;
btn2.Caption := Format('%f s', [SecondSpan(eDate, sDate)]);
self.Caption := inttostr(lst1.Count);
end;