Autor Beitrag
Simson
Hält's aus hier
Beiträge: 3

Win xp
Delphi 4,2005pro
BeitragVerfasst: Fr 04.11.05 13:44 
Hallo zusammen,
Ich habe ein kleines programmiertechnisches Problem...bin ziemlicher Delphi Anfänger und so...

Jedenfalls soll ich ein Prog zum (3a+1)-Problem (de.wikipedia.org/wiki/Collatz-Problem) schreiben. Wir sollen dann eine Tabelle produzieren, die für die Startzahlöen von a1 bis a2 den Anfangswert, die Schrittzahl und den Maximalwert der Rechnung enthält.

hier mal mein code:
ausblenden volle Höhe 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:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
procedure TForm1.Button1Click(Sender: TObject);
var
a:integer;
a1:integer;
a2:integer;
c:integer;
i:integer;
j:integer;
m:integer;
function ulam(a:integer):integer;
         begin
         c:=0;
         m:=0;
         while a>1 do begin
                      inc(c);
                      if odd(a) then a:=(3*a+1)
                                else a:=(a div 2);
                      if m<a then m:=a;
                      end;
         ulam:=c;
         end;

function max(a:integer):integer;
         begin
         c:=0;
         m:=0;
         while a>1 do begin
                      inc(c);
                      if odd(a) then a:=(3*a+1)
                                else a:=(a div 2);
                      if m<a then m:=a;
                      max:=m;
                      end;
         end;
begin

a1:=strtoint(edit2.text);
a2:=strtoint(edit3.text);

label1.caption:=inttostr(c);
label2.caption:=inttostr(m);


for i:=1 to (a2-a1+1) do begin
                         stringgrid1.cells[0,i]:='a'+inttostr(i);
                         stringgrid1.cells[1,i]:=inttostr((a1-1)+i);
                         for j:=a1 to a2 do begin
                                            stringgrid1.cells[2,i]:=inttostr(ulam(j));
                                            stringgrid1.cells[3,i]:=inttostr(max(j));
                                            end;                        
                         end;




end;

procedure TForm1.FormCreate(Sender: TObject);
begin
stringgrid1.cells[1,0]:='Anfangswert';
stringgrid1.cells[2,0]:='Schrittzahl';
stringgrid1.cells[3,0]:='Maximalwert';
end;


ich hoffe das ganze ist nicht zu wirr...

er gibt auch zahlen in das stringgrid aus...aber die sind definitiv falsch...

für konstruktive hinweise wäre ich sehr dankbar!
inselberg
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 458



BeitragVerfasst: Fr 04.11.05 14:12 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
function collatz(a:longint):longint;
begin
 if odd(a) then result:=(3*a+1) else result:=(a div 2);
end;


procedure TForm1.Button1Click(Sender: TObject);
var a:longint;
begin
 a:=strtoint(edit1.text);
 memo1.lines.add('a0:='+inttostr(a));
 while a>1 do
 begin
  a:=collatz(a);
  memo1.lines.add(inttostr(a));
 end;
end;


eigentlich genau so wie du auch... mit den werten von wiki verglichen.
du koenntest ja mal en zahlenbeispiel geben mit dem es nicht funktioniert.

schrittweite und obergrenze (a > max ?) sind mir nicht klar.

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt

_________________
hans bist du das ?
Simson Threadstarter
Hält's aus hier
Beiträge: 3

Win xp
Delphi 4,2005pro
BeitragVerfasst: Fr 04.11.05 14:44 
ich glaube auch nicht, dass das Problem beim Algorithmus liegt, sondern bei der ausgabe in die tabelle oder so.
mit schrittzahl ist die anzahl der durchläufe des algorithmus gemeint, bis a=1 ist. (inc(c)).
maximalwert ist die höchste in der rechnung vorkommende zahl (max:=m).

zahlenbeispiel:

a=2
->schrittzahl=0
->maximalwert=4457472
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Fr 04.11.05 14:44 
Hallo!

Mit Schrittzahl ist gemeint, wie oft denn nun die Collatzfunktion aufgerufen wird, bis das Ergebnis 1 ist. Der Maximalwert ist wohl die größte Zahl, die aus den Funktionsaufrufen hervorging.

Edit: Ich bin zu langsam geworden. :)

Cu,
Udontknow
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Fr 04.11.05 14:46 
Moin!

Hier mein Vorschlag:
ausblenden volle Höhe 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:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[0,0] := 'Anfangswert';
  StringGrid1.Cells[1,0] := 'Schrittzahl';
  StringGrid1.Cells[2,0] := 'Maximalwert';
end;

procedure TForm1.BtnStartClick(Sender: TObject);
  var
    a1,a2,
    c,m,
    i,j: Integer;

  function Collatz(const a: Integer): Integer;
  begin
    if Odd(a) then
      Result := 3*a +1
    else
      Result := a div 2;
  end;

  procedure CalcCollatz(a: Integer; var cnt, max: Integer);
  begin
    cnt := 0;
    max := 0;
    while (a > 1) do begin
      a := Collatz(a);
      if (a > max) then
        max := a;
      Inc(cnt);
    end;
  end;

begin
  a1 := StrToIntDef(StartZahl.Text,0);
  a2 := StrToIntDef(EndZahl.Text,0);
  if (a2 < a1) then begin
    i := a1;
    a1 := a2;
    a2 := i;
  end;
  StringGrid1.RowCount := 1;
  j := 1;
  for i := a1 to a2 do begin
    Inc(j);
    StringGrid1.RowCount := j;
    StringGrid1.Cells[0,j-1] := IntToStr(i);
    CalcCollatz(i,c,m);
    StringGrid1.Cells[1,j-1] := IntToStr(c);
    StringGrid1.Cells[2,j-1] := IntToStr(m);
  end;
end;

cu
Narses
Simson Threadstarter
Hält's aus hier
Beiträge: 3

Win xp
Delphi 4,2005pro
BeitragVerfasst: Fr 04.11.05 14:54 
uu yeah, das is ne wesentlich bessere lösung. herzlichsten dank.
ich glaube das problem war das forschleifengewurschtel...
thx nochmal an alle!