Autor Beitrag
Dodekaeder
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

WinXP,Win95
Delphi 5
BeitragVerfasst: Sa 25.02.06 19:37 
Hallo,
ich möchte die Fakultät der Variablen n berechnen.
Hab auch schon einen Quelltext zusammengebastelt, aber eswerden nicht die korrekten Werte(die ich mit meinem Taschenrechner überprüfen kann) ausgegeben.
Kann mir da jemand weiterhelfen?? :wink:

[Wie kann hier eigentlich so einen richtigen Quelltext wie aus Delphi einfügen?]
Moderiert von user profile iconraziel: Mit den Delphi-Tags, die ich eben für dich eingefügt hab ;)

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 n,i:integer;
begin
n:=Strtoint(edit1.text);
if n<=0
   then
   n:=1 // Eingabe fehler abfangen(n>=1)
        else
            begin
                 for i:=1 to n do   // 1*2*3...*n
                     begin
                           n:=n*i;
                     end;           //Ende for-schleife
            end;                    //Ende else-block

label1.caption:=floattostr(n);       // Fakultät n! ausgeben
end;


Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt
Ironwulf
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 733
Erhaltene Danke: 2



BeitragVerfasst: Sa 25.02.06 19:42 
der fehler liegt dort
n:= n * i;

müsste es nicht heißen n:= n + (n * i);

du musst ja das vorige ergebnis noch drauf addiern
Dodekaeder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

WinXP,Win95
Delphi 5
BeitragVerfasst: Sa 25.02.06 19:51 
hm tja jetzt kommen andere Werte raus, aber immer noch nicht die (richtigen) die rauskommen sollten.. :cry:
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Sa 25.02.06 20:05 
Zunächst mal: Dein Verfahren mit n:=n*i ist schon richtig. Was kommen denn für falsche Werte raus?

Das Hauptproblem dürfte aber darin liegen, dass du scheinbar zwei Variablen mit derselben Bezeichnung hast: Einmal das n als Grenze für die Schleife (also das "n" in "n!"), aber dann das Ergebnis auch n nennst. Das kann doch nur schief gehen...

_________________
We are, we were and will not be.
Dodekaeder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

WinXP,Win95
Delphi 5
BeitragVerfasst: Sa 25.02.06 20:15 
Ja ich hab das nochmal abgeändert...
n! ist jetzt nI

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:
procedure TForm1.Button1Click(Sender: TObject);
var
n,i:integer;
nI:real;
begin
n:=Strtoint(edit1.text);
if n<=0
   then
   n:=1
        else
        if n=1
        then
        nI:=1
        else
            begin
                 for i:=1 to n do   // 1*2*3...*n
                     begin
                           nI:=n+(n*i);
                     end;           //Ende for-schleife
            end;                    //Ende else-block

label1.caption:=floattostr(nI);       // Fakultät n! ausgeben
end;


Also für n=0 kommt 2,3599... irgend ein kauderwelsch raus
n=1 => 1
n=2 => 6 muss aber 2 sein
n=3 => 12 muss aber 6 sein
n=4 => 20 muss aber 24 sein

etc...

Moderiert von user profile iconGausi: Beitragsformatierung überarbeitet.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 25.02.06 20:16 
user profile iconDodekaeder hat folgendes geschrieben:
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 n,i:integer;
begin
n:=Strtoint(edit1.text);
if n<=0
   then
   n:=1 // Eingabe fehler abfangen(n>=1)
        else
            begin
                 for i:=1 to n do   // 1*2*3...*n
                     begin
                           n:=n*i;
                     end;           //Ende for-schleife
            end;                    //Ende else-block

label1.caption:=floattostr(n);       // Fakultät n! ausgeben
end;


Bei dem Source, würde ich wenn ich Delphi wäre, auch streiken ...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.Button1Click(Sender: TObject);
var n,i,r:integer;
begin
    n:=Strtoint(edit1.text);
    if n < 2 then
    begin
        r := 1; // Eingabe fehler abfangen(n>=1)
    end
    else
    begin
        r := 1;
        for i := n downto 2 do   // 1*2*3...*n
        begin
            r := r * i;
        end;           //Ende for-schleife
    end;                    //Ende else-block

    label1.caption:=floattostr(r);       // Fakultät n! ausgeben
end;

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Dodekaeder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

WinXP,Win95
Delphi 5
BeitragVerfasst: Sa 25.02.06 20:23 
Jo jetzt klappts
Danke schön :D
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 25.02.06 20:26 
Geht aber auch mit zwei Variablen ^^

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
var
    a, b: Integer;
begin
    a := 1;
    while b > 1 do
    Begin
        a := a * b;
        dec(b);
    end;
    Result := a;
end;

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 25.02.06 20:54 
wie wär es damit?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function fakultaet(n: integer): integer;
begin
 if n > 0 then
  result := fakultaet(n-1)*n
 else
  result := 1;
end;


das ganze mal rekursiv aufzäumt.....
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 25.02.06 20:58 
Liefert Dir für N = 2 Mrd (oder auch schon vorher) nen Stackoverflow und brauch wegen extremen Speicherzugriffen wesentlich länger als mein Algo *g*

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 25.02.06 21:10 
tja, mit N=16 bist eh schon an der grenze für das resultat in integer.

aber ist schon klar, rekursive algos beanspruchen den stack mehr als iterative. find trotzdem, sieht schnucklig aus ;-)

und n=20 ist das ende dieser routine....

da sprengt es auch den bereich des 64bit breiten integers. für n>20 werden dann schon mehr als 63bit zur abbildung benötigt. ... tja, die zahlen werden halt schnell astronomisch ;-)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
function fakultaet(n: Extended): Extended; overload;
begin
 if n > 0 then
  result := fakultaet(n-1)*n
 else
  result := 1;
end;

function fakultaet(n: int64): int64; overload;
begin
 if n > 0 then
  result := fakultaet(n-1)*n
 else
  result := 1;
end;


daher häng ich gleich noch eine gleitkommavariante hinten dran ;-)
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 25.02.06 22:20 
schonmal dran gedacht n als var parameter zu übergeben ?
Grenzgaenger
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 25.02.06 22:24 
wenn N als var übergeben würde, dann würde der inhalt direkt verändert. der rekursive algo. benötigt jedoch einen satz von lokalen variablen. daher scheidet var aus.

eine andere frage, wieso sollte man var verwenden? wegen des stacks? wie gesagt, selbst bei extendet ist zwischen 1700 und 1800 durchläufen der offen aus. das schafft auch ein kleiner stack ohne probleme.

grüsse