Task

Implement one algorithm (or more) to compute the Gamma (



Γ


{\displaystyle \Gamma }

) function (in the real field only).

If your language has the function as built-in or you know a library which has it, compare your implementation's results with the results of the built-in/library function.

The Gamma function can be defined as:

  



Γ
(
x
)
=


∫

0


∞



t

x
−
1



e

−
t


d
t



{\displaystyle \Gamma (x)=\displaystyle \int _{0}^{\infty }t^{x-1}e^{-t}dt}


This suggests a straightforward (but inefficient) way of computing the 



Γ


{\displaystyle \Gamma }

 through numerical integration.



Better suggested methods:

 Lanczos approximation
 Stirling's approximation


0.577215664901 var gamma
-0.65587807152056 var coeff
-0.042002635033944 var quad
0.16653861138228 var qui
-0.042197734555571 var theSet
 
def recigamma var z /# n -- n #/
    z 6 power theSet *
    z 5 power qui *
    z 4 power quad *
    z 3 power coeff *
    z 2 power gamma *
    z + + + + +
enddef
 
/# without var
def recigamma
    dup 6 power theSet * swap
    dup 5 power qui * swap
    dup 4 power quad * swap
    dup 3 power coeff * swap
    dup 2 power gamma * swap
    + + + + +
enddef
#/
 
def gammafunc   /# n -- n #/
    dup 1 == if
    else
        dup abs 0.5 <= if
            recigamma 1 swap /
        else
            dup 1 - gammafunc swap 1 - *
        endif
    endif
enddef
 
0.1 2.1 .1 3 tolist
for
    dup print " = " print gammafunc print nl
endfor