Autor Beitrag
-delphin-
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 200



BeitragVerfasst: Mi 22.06.05 16:56 
Hallo, ich will das KgV zweier Zahlen berechnen. Es soll so aussehen, dass man in 2 Editfelder jeweils eine Zahl eingibt und im Memo dann das kleine gemeinsame Vielfache (kgV) ausgegeben wird (von 25 und 4 z.b. 100).
Dies soll so berechnet werden, weil ich mir dachte, dass es am einfachsten ist, dass man in einer schleife 25 immer mit einem mehr multipliziert (25,50,75,100) und überprüft, wann eine Zahl durch 4 teilbar ist.

mein quelltext:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
procedure TForm1.Button1Click(Sender: TObject);
var Zahl1, Zahl2, i : integer;
begin
  Memo1.Clear;
  //Sorge dafür, dass Zahl1 stets die größere ist
  If Edit1.Text>Edit2.Text then
  begin
    Zahl1 := StrToInt(Edit1.Text);
    Zahl2 := StrToInt(Edit2.Text);
  end else
  begin
    Zahl2 := StrToInt(Edit1.Text);
    Zahl1 := StrToInt(Edit2.Text);
  end;
  //Schleife
  repeat
  for i:=1 to Zahl2 do
  begin
    Zahl1 := (Zahl1 * i); //Zahl1 bei jedem Durchlauf mit i multiplizieren...
  end;
  until
    Zahl1 mod Zahl2 = 0//...bis sie durch Zahl2 teilbar ist
  If Zahl1 mod Zahl2 = 0 then //Sobald das der Fall ist...
  Memo1.Lines.Add(IntToStr(Zahl1)); //...im Memo ausgeben
end;

end.


Nur leider zeigt er mir im memo immer nur eine 0 an, es sei denn, die Zahlen sind gleich, dann zeigt er mir eine sehr hohe Zahl an (bei 4 und 4 96; bei 5-5 600; bei 6-6 4320 oO). Kann mir jemand sagen, wo der fehler ist??
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Mi 22.06.05 17:23 
In deinem Beisoiel wolltest du doch zu 25 jeweils 25 hinzuaddieren.

also
ausblenden Delphi-Quelltext
1:
zahl1:=zahl1+zahl1;					


bei dir wird
ausblenden Delphi-Quelltext
1:
2:
zahl1:=25*2=50;
zahl1:=50*3=150;

gerechnet
-delphin- Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 200



BeitragVerfasst: Mi 22.06.05 17:27 
ändert sich nix immernoch 0.. außer bei 4 und 4 kommt jetz 64 raus statt 96 (sollte aber nur 4 rauskommen)
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Mi 22.06.05 17:33 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var kgvvalue:integer;
begin 

kgvvalue:=zahl1;
repeat     
    kgvvalue := (kgvvalue + zahl);  
 // end;  hier hattest du vorher deine Schleife schon beendet
  until  (kgvvalue mod Zahl2 = 0);   

//hier dann Ausgabe
 
// Dein Verfahren funktioniert aber nur mit zwei Werten bei KGV von 3 Zahlen haperts

end;


Zuletzt bearbeitet von Allesquarks am Mi 22.06.05 17:38, insgesamt 2-mal bearbeitet
-delphin- Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 200



BeitragVerfasst: Mi 22.06.05 17:35 
es soll auch nur mit 2 gehen, tuts aber nicht
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Mi 22.06.05 17:37 
Vielleicht liegt es ja an der Ausgabe. Ansonsten könntest du es ja mal über Primfaktorzerlegung versuchen.

P.S.: Hab die Source oben nochmal geändert!
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Mi 22.06.05 17:38 
Man oh man, das machst du dir schwer ;).

[quote:d3ae412f53="[Delphi]N"]
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
  //Sorge dafür, dass Zahl1 stets die größere ist
  If Edit1.Text>Edit2.Text then 
  begin
    Zahl1 := StrToInt(Edit1.Text);
    Zahl2 := StrToInt(Edit2.Text);
  end else
  begin
    Zahl2 := StrToInt(Edit1.Text);
    Zahl1 := StrToInt(Edit2.Text);
  end;
[/quote:d3ae412f53]

Edit1.Text>Edit2.Text ergibt nicht das was du haben willst. Du müsstest, wenn du nachdenkst dürftest du wissen warum, die Texte erst in Integer umwandeln und dann erst vergleichen.
Kürzer geht es aber so (unit Math einbinden):

ausblenden Delphi-Quelltext
1:
2:
  Zahl1:=Max(StrToInt(Edit1.Text), StrToInt(Edit2.Text));
  Zahl1:=Min(StrToInt(Edit1.Text), StrToInt(Edit2.Text))

Sieht doch wesentlich kürzer und einfacher aus ;).

Des weiteren dürfte dein zweiter Teil manchmal eine Endloschleife ergeben. Des wegen hier eine bessere Variante:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
for i:=2 to Zahl2 do
 begin
  if (Zahl1 mod i=0and (Zahl2 mod i=0then
   begin
    Memo1.Lines.Add(IntToStr(i));
    break
   end
 end


Moderiert von user profile iconChristian S.: Name in Quote-Tag geändert
-delphin- Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 200



BeitragVerfasst: Mi 22.06.05 17:49 
folgender quelltext:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.Button1Click(Sender: TObject);
var Zahl1, Zahl2, {Zahl3,} i : integer;
begin
  Memo1.Clear;
 Zahl1:=Max(StrToInt(Edit1.Text), StrToInt(Edit2.Text));
  Zahl1:=Min(StrToInt(Edit1.Text), StrToInt(Edit2.Text));
  for i:=2 to Zahl2 do
 begin
  if (Zahl1 mod i=0and (Zahl2 mod i=0then
   begin
    Memo1.Lines.Add(IntToStr(i));
    break
   end
 end
end;

end.


ergebnis: 0 ;)
wo liegt der fehler?? oO
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Mi 22.06.05 17:52 
@@ Heiko:
Ich weiß nicht wofür dein letzter Code-Abschnitt gedacht war, allerdings sind z.B. 6 und 4 bei i=2 beide restlos teilbar, also deine Bedingung erfüllt i ist aber nicht das KGV. Kannst auch nicht einfach mit i multiplizieren (8 und 14 sind beide durch 2 teilbar das KGV ist aber nicht 14*2)
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Mi 22.06.05 17:58 
@[Delphi]N: Gib mal deine Testwerte.

Sry, ich hatte an den kleineten gemeinsamen Nenner gedacht :oops: .


Zuletzt bearbeitet von Heiko am Mi 22.06.05 18:04, insgesamt 1-mal bearbeitet
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Mi 22.06.05 18:00 
Probier' doch einfach mal diese Funktion da. ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
function GetKgV(zahl1, zahl2: Integer): Integer;
var
  temp: Integer;
begin
  if (zahl1 < 1or (zahl2 < 1then Exit;
  result := -1;
  if zahl1 mod zahl2 = 0 then result := zahl1;
  if zahl2 mod zahl1 = 0 then result := zahl2;
  if result <> -1 then Exit;
  if zahl1 > zahl2 then begin
    temp := zahl1;
    zahl1 := zahl2;
    zahl2 := temp;
  end;
  temp := zahl1;
  repeat temp := temp + 1 until (temp mod zahl1 = 0and (temp mod zahl2 = 0);
  result := temp;
end;


//UPDATE:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
function GetKgV(zahl1, zahl2: Integer): Integer;
begin
  if (zahl1 < 1or (zahl2 < 1then Exit;
  result := -1;
  if zahl1 mod zahl2 = 0 then result := zahl1;
  if zahl2 mod zahl1 = 0 then result := zahl2;
  if result <> -1 then Exit;
  if zahl1 > zahl2 then begin
    result := zahl1;
    zahl1 := zahl2;
    zahl2 := result;
  end;
  result := zahl1;
  repeat result := result + 1 until (result mod zahl1 = 0and (result mod zahl2 = 0);
end;
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Mi 22.06.05 18:08 
Es müsste auch das für das KgV funktionieren (ungetestet):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
i:=1;
while (Zahl1*i) mod Zahl2<>0 do
 begin
  inc(i)
 end;
Memo1.Lines.Add(IntToStr(Zahl1*i))


//Update
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
if (Sign(Zahl1)=1or (Sign(Zahl2)=1then
 begin
  i:=1;
  while (Zahl1*i) mod Zahl2<>0 do
   begin
    inc(i)
   end;
  Memo1.Lines.Add(IntToStr(Zahl1*i))
 end


Zuletzt bearbeitet von Heiko am Mi 22.06.05 18:17, insgesamt 3-mal bearbeitet
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Mi 22.06.05 18:09 
ist aber arg langsam immer nur 1 hinzuzuaddieren

Hier mal mit Primfaktorzerlegung:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function KGV(Zahl1,Zahl2:integer):integer;
var i,kgv:integer;nureinmal:boolean;
begin
kgv:=1;
For i:=2 to max(zahl1,Zahl2) do begin
  nureinmal:=false;
   if zahl1 mod i=0 then begin   kgv:=kgv*i;  zahl1:=zahl1/i;  nureinmal:=true;  end;
   if (zahl1 mod i=0)  then begin  
           if (nureinmal=false) then begin kgv:=kgv*i;  zahl2:=zahl2/i;  continue;  end
           else begin zahl2:=zahl2/i;end;
   end;
end;

end;


so nich getestet

//Update sorry geht nicht mit for schleife
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
function KGV(Zahl1,Zahl2:integer):integer;
var i,kgv,bounds:integer;nureinmal:boolean;
begin

kgv:=1; bounds:=max(zahl1,Zahl2); 

repeat inc(i)
  nureinmal:=false;
   if zahl1 mod i=0 then begin   kgv:=kgv*i;  zahl1:=zahl1/i;  nureinmal:=true; end;
   if (zahl1 mod i=0)  then begin  
           if (nureinmal=false) then begin kgv:=kgv*i;  zahl2:=zahl2/i;  end
           else begin zahl2:=zahl2/i;end;
   end;
   if (nureinmal=true) then begin dec(i);
until (i>=bounds);

end;


Zuletzt bearbeitet von Allesquarks am Mi 22.06.05 18:18, insgesamt 1-mal bearbeitet
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Mi 22.06.05 18:17 
user profile iconAllesquarks hat folgendes geschrieben:
ist aber arg langsam immer nur 1 hinzuzuaddieren

Klar... ;) Ist aber eine ziemlich einfache Lösung... Und bei kleinen Zahlen, wie sie IMHO [Delphi]N benutzt, ist's wahrscheinlich sogar schneller... :tongue:
Heiko
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3169
Erhaltene Danke: 11



BeitragVerfasst: Mi 22.06.05 18:23 
Meine dürfte eigentlich am schnellsten sein. Die dürfte man soagr nicht weiter optimieren können ;).
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 22.06.05 23:06 
Moin!

Ich hätte da noch den Vorschlag, das nach Euklid zu lösen: :wink:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
procedure TForm1.Button1Click(Sender: TObject);
  var
    z1,z2,r: Integer;
begin
  z1 := StrToIntDef(Edit1.Text,0); // Zahl 1
  z2 := StrToIntDef(Edit2.Text,0); // Zahl 2
  r := z1 mod z2;
  while (r <> 0do begin
    z1 := z2;
    z2 := r;
    r := z1 mod z2;
  end;
  r := z2;
  Edit3.Text := IntToStr(r); // ggT
  z1 := StrToIntDef(Edit1.Text,0);
  z2 := StrToIntDef(Edit2.Text,0);
  Edit4.Text := IntToStr((z1 *z2) div r); // kgV
end;


cu
Narses