Registriert seit: 8. Mär 2004
230 Beiträge
Delphi 7 Enterprise
|
Re: Primfaktorzerlegung läuft viel zu langsam...
28. Feb 2007, 12:00
So habe ich das gelöst:
Delphi-Quellcode:
function IsPrime(const Value: Cardinal): Boolean;
var
t: Cardinal;
begin
if (Value = 2) then
begin
Result := TRUE;
Exit;
end;
if (Value < 2) or (Value mod 2 = 0) then
begin
Result := FALSE;
Exit;
end;
t := 3;
while (t * t <= Value) do
begin
if (Value mod t = 0) then
begin
Result := FALSE;
Exit;
end
else
t := t + 2;
end;
Result := TRUE;
end;
// -----------------------------------------------------------------------------
function Factorize(const V: Cardinal): TFactors;
var
t, x, Count: Cardinal;
begin
t := 2;
x := V;
Count := 0;
SetLength(Result, 0);
while (t <= x) do
begin
if (x mod t = 0) then
begin
Count := Count + 1;
SetLength(Result, Count);
Result[Count - 1] := t;
x := x div t;
t := 2;
end
else
t := t + 1;
end;
end;
|
|
Zitat
|