function IsPrime(ANumber: Cardinal): Boolean;
var
DivCount: Integer;
Divisor: Cardinal;
begin
DivCount := 0;
if (ANumber > 0) then
begin
for Divisor := 1 to ANumber do
begin
if (ANumber mod Divisor) = 0 then
Inc(DivCount);
if (DivCount > 2) then
Break;
end;
end;
Result := (DivCount = 2);
end;
Beispiel :
procedure TForm1.Button1Click(Sender: TObject);
begin
if IsPrime(4) then
ShowMessage('prim')
else
ShowMessage('nicht prim');
end;
Keine Kommentare:
Kommentar veröffentlichen