You are on page 1of 2

Gamma Function

Purushothaman.V.N
July 3, 2011

The gamma function is defined by the integral


Z
tx1 et dt
(x) =
0

and satisfies the recurrence relation


(x + 1) = x(x)
and reflection formula

sin x
x
(1 + x)(1 x) =
sin (x)
x
(1 x) =
(1 + x) sin (x)
(x)(1 x) =

It can be used to calculate -functions less than 1. Since sin (x) is zero for
integer x, -function is unbounded for negative integers but not for negative fractions. The following approximation method,derived by Lanczos, is
employed for calculating the -function numerically. For x > 0,



a1
a2
an
(1 + x) = k k5 ek 2 a0 +
+
+ .... +
+
x+1 x+2
x+n
where k = x + 5.5,  the error term and ai expansion coefficients. For
|| < 2 1010 , n = 6 is sufficient. The coefficients are given by
a0 = 2.5066282746310005, a1 = 76.18009172947146
a2 = 86.50532032941677, a3 = 24.01409824083091
a4 = 1.231739572450155, a5 = 1.208650973866179103 , a6 = 5.395239384953106



a1
a2
an
k5
+
+ .... +
ln (1 + x) = ln(k )k+ln( 2)+ln a0 +
x+1 x+2
x+n
" 
#
2
a1
a2
an
k5
ln (x) = ln (k ) k + ln
a0 +
+
+ .... +
x
x+1 x+2
x+n
1

def gamma(x0):
a=[1.000000000190015, 76.18009172947146, -86.50532032941677,
24.01409824083091, -1.231739572450155, 0.001208650973866179,
-0.00005395239384953]
s=a[0]
for i in range(1,7):s+=a[i]/(x0+i)
s*=sqrt(2*pi)/x0
k=x0+5.5
return (k-5)*log(k)-k+log(s)
x=input(Give the value whose Gamma function is required)
print The value gamma(%f) = %f%(x,exp(gamma(x)))
But reasonable accuracy can be achieved by just taking the first five terms
and approximating elements of a to 2 decimal places.
def gamma(x0):
a=[1.0,76.18,-86.51,24.01,-1.23]
s=a[0]
for i in range(1,5):s+=a[i]/(x0+i)
s*=sqrt(2*pi)/x0
k=x0+5.5
return (k-5)*log(k)-k+log(s)
x=input(Give the value whose Gamma function is required)
print value of gamma(%f) = %f%(x,exp(gamma(x)))

You might also like