Autor Beitrag
Fabian
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 42



BeitragVerfasst: Do 31.10.02 20:12 
Hallo

Hat jemand von euch eine Idee wie eine Prozedur, die eine Primfaktorzerlegung durchführt aussehen könnte ?

Danke
DaFox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 189



BeitragVerfasst: Do 31.10.02 21:41 
Hi!

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
function Primfaktorzerlegung(n: Integer): String;
var 
  i: Integer;
begin
  Result := '';
  i := 2;
  repeat
    while (n mod i = 0) do
    begin
      n:= n div i;
      if Result <>'' then Result := Result + ' * ';
      Result := Result + IntToStr(i);
    end;
    if (i = 2) then i := i + 1 else i := i + 2;
    if (i > trunc(sqrt(n))) then i := n;
  until (n = 1);
  if (Pos('*', Result) = 0) then Result := 'Primzahl';
end;


Gruß,
Markus


Zuletzt bearbeitet von DaFox am Do 31.10.02 21:56, insgesamt 1-mal bearbeitet
DaFox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 189



BeitragVerfasst: Do 31.10.02 21:51 
... oder doch lieber rekursiv :wink:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
function Primfaktorzerlegung(n: Integer): String;
var
  i: Integer;
begin
  if (n mod 2 = 0) then
  begin
    Result := '2 * ' + Primfaktorzerlegung(n div 2);
    exit;
  end;
  for i := 1 to trunc(sqrt(n)) div 2 do
  begin
    if (n mod (2 * i + 1) = 0) then
    begin
      Result := IntToStr(2 * i + 1) + ' * ' + Primfaktorzerlegung(n div (2 * i + 1));
      exit;
    end;
  end;
  Result := IntToStr(n);
end;


Bitte beide Funktionen nicht einfach übernehmen, da natürlich noch ein paar Verbesserungen nötig sind! (Gib mal einen negativen Wert bzw. 0 ein; auch die hin und wieder abschließende 1 ist nervig)

Gruß,
Markus
CenBells
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1547

Win 7
Delphi XE5 Pro
BeitragVerfasst: Fr 01.11.02 00:59 
wenn dir das nicht reicht, kann ich dir ein prog zusenden, das die zerlegung durchführt.

mail mich einfach an
ken@cenbells.de

Gruß
Ken