Kylix
Tips

NEW TIPS
Database (135)
Files (612)
Forms (606)
Graphic (536)
IDE (456)
Indy (433)
Internet / LAN (590)
IntraWeb (443)
Kylix (447)
Math (561)
Misc (591)
Multimedia (496)
Objects/
ActiveX (503)

OpenTools API (438)
Printing (459)
Strings (583)
System (721)
VCL (586)

Search Tip
Top15
Add new Tip

Forum

...calculate the factorial of a given number?
Author: Loïs Bégué
Homepage: http://www.begue.de
[ Print tip ]    

Tip Rating (5):  
     



{
  The factorial of a positive integer is defined as:
  Die Fakultät einer positiven Nummer ist wie folgt definiert:

  n! = n*(n-1)*(n-2)*(n-3)*...*2*1
  1! = 1
  0! = 1

  Example/Beispiel: 5! = 5*4*3*2*1
}

// Iterative Solution, Iterative Lösung:

function FacIterative(n: Word): Longint;
var
  
f: LongInt;
  i: Integer;
begin
  
f := 1;
  for i := 2 to do f := f * i;
  Result := f;
end;


// Recursive Solution, Rekursive Lösung:

function FacRecursive(n: Word): LongInt;
begin
  if 
n > 1 then
    
Result := n * FacRecursive(n-1)
  else
    
Result := 1;
end;

Rate this tip:

poor
very good


Copyright © Torry's Delphi Pages Torry's Delphi Pages Maintained by Simon Grossenbacher Notes? Comments? Feel free to send... Copyright © 1996-2001