Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

How to calculate the factorial of a given number

Title: How to calculate the factorial of a given number ...calculate the factorial of a given number? { The factorial of a positive integer is defined as: 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 n do f := f * i; Result := f; end; function FacRecursive(n: Word): LongInt; begin if n 1 then Result := n * FacRecursive(n-1) else Result := 1; end;