Oh... das habe ich übersehen...
Jetzt die ultimative Variante ohne breaks, exits und nur einem sqrt
Delphi-Quellcode:
function Prim(const Value: Cardinal) : Boolean;
var
i, sr : Cardinal;
begin
Result := true
if (Value <= 1) or ((Value mod 2 = 0) and (Value <> 2)) then
Result := false
else if Value > 2 then
begin
i := 3;
sr := Trunc(Sqrt(Value)) + 1;
while (i <= sr) and (Result) do
begin
if Value mod i = 0 then
begin
Result := false;
end else
Inc(i, 2);
end;
end;
end;
Zum sqrt: Ich bin mir nicht sicher, ob es mit i² funktioniert...