Как посчитать факториал
Автор: http://www.swissdelphicenter.ch
{
The factorial of a positive integer is defined as:
n! = n*(n-1)*(n-2)*(n-3)*...*2*1
1! = 1
0! = 1
Example: 5! = 5*4*3*2*1
}
// Iterative Solution:
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;
// Recursive Solution:
function FacRecursive(n: Word): LongInt;
begin
if n > 1 then
Result := n * FacRecursive(n - 1)
else
Result := 1;
end;
|