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


import "/fmt" for Fmt
import "/math" for Math
 
var stirling = Fn.new { |x| (2 * Num.pi / x).sqrt * (x / Math.e).pow(x) }
 
System.print(" x\tStirling\t\tLanczos\n")
for (i in 1..20) {
    var d = i / 10
    System.write("%(Fmt.f(4, d, 2))\t")
    System.write("%(Fmt.f(16, stirling.call(d), 14))\t")
    System.print("%(Fmt.f(16, Math.gamma(d), 14))")
}