here's the Stieltjes' approximation.
uses "console"
Function gamma ( x As Ext ) As Ext
'Stieltjes' approximation to the Gamma function
'x!=exp(log(2*Pi)/2-x+(x+1/2)*log(x)+cf(x))
'cf(x) = c1
' ---------------------
' c2
' x + ----------------
' c3
' x + ------------
' c4
' x + --------
' x + '
' '
' '
Dim As Ext cf, logpi2half, xt, gam, pp
Dim As Integer i
logpi2half = 0.91893853320467274178032973640562
' COEFFICIENTS FOR CONTINUED FRACTION
Dim As Ext C(14)
c( 1) = 0.0833333333333333333333333333333333
c( 2) = 0.0333333333333333333333333333333333
c( 3) = 0.252380952380952380952380952380952
c( 4) = 0.525606469002695417789757412398922
c( 5) = 1.01152306812684171174737212473062
c( 6) = 1.51747364915328739842849151949548
c( 7) = 2.26948897420495996090915067220988
c( 8) = 3.00991738325939817007314073420772
c( 9) = 4.02688719234390122616887595318144
c(10) = 5.00276808075403005168850241227619
c(11) = 6.28391137081578218007266315495165
c(12) = 7.49591912238403392975235470826751
c(13) = 9.04066023436772669953113936026048
c(14) = 10.4893036545094822771883713045926
' CONTINUED FRACTION
xt=x+8
CF = C(9)+Xt
For i = 8 To 2 Step -1
cf=xt+c(i)/cf
Next
cf=c(1)/cf
' POCHHAMMER POLYNOMIAL
PP = X * (X+1) * (X+2) * (X+3) * (X+4) * (X+5) * (X+6) * (X+7) * (X+8)
gam = Exp(logpi2half-xt+(xt+.5)*Log(xt)+cf)
Return gam/pp
End Function
Dim As Ext y, x = 1
While x>0
Console_Write "x "
x=Console_ReadLine()
If x=0 Then Exit While
y = gamma (x)
Console_WriteLine "Gamma "<rim$(Str$(x))&" = "&Format$(y,17)
Wend
Console_WriteLine "All done. Press any key to finish"
Console_WaitKey
Bookmarks