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:
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!