Factorial

IMPLEMENTATION
Unit: internal function, external function without procedure statement
 
Parameters: a integer N>=0
 
Returns: FACT(N)=1*2*...*N
 


FACT: procedure
parse arg N
F = 1
do J = 2 to N; F = F * J; end
return F

 

An excellent approximation of FACT(N) was obtained by James Stirling in the 18 century. We use it in the following STIRLING program for computing of the approximation of the factorial by the Stirling's formula.

 


STIRLING: procedure
parse arg N, P
Pi = PI(P); E = E(P)
Sqrt2pin = SQRT(2 * Pi * N, P)
return,
  Sqrt2pin * (N / E)**N * (1 + 1 / (12 * N))

 

EXAMPLE
The FACT(10) computes the value 3628800 and STIRLING(10) computes the value 3628684.7
 
As N increases, the approximation to the factorial becomes more and more accurate.
 
FACT(108001) returns (in numeric digits 32 settings)
8.1428599075279953478037134061743E+496713 after 1.56 sec
 
STIRLING(108001,32) returns
8.1428599075255713876213611726275E+496713 after 0.02 sec
 
By the way, Calculator in Windows 2000 (PC 130MB RAM, 500MHz) displays 8.1428599075279953478037134061347E+496713 after (about) 900 seconds (15 minuts)

 
CONNECTIONS


Cover Contents Index Main page Rexx page   Mail

last modified 30th July 2001
Copyright © 2000-2001 Vladimir Zabrodsky
Czech Republic