**TI92P*mainSingle file dated Sat Oct 21 10:24:55 20Rgamma¸¥Z^(z)
Func
© Version 2023/10/21
© Copyright 2023 John Scott (https://johnscott.me)
© This program is free software subject to the GNU General Public License as published by the Free Software Foundation, either version three of the license or, at your option, any later version.
© This program computes an approximation to the gamma function using an improved variant of Stirling's approximation. It would appear, but has not been proven, that this algorithm yields near-exact results for a wide variety of parameters.
© If z is an integer greater than 0, then the gamma function is equal to (z-1)!. Let's do that.
If imag(z)=0 Then
If fPart(z)=0 Then
If z>0 Then
Return (z-1)!
Else
Return ¾
EndIf
EndIf
EndIf
© Our flavor of Stirling's approximation is near-exact, but it appears this is only the case if the number has real part greater than 10 or so. To support the region left in the complex plane, we'll use the defining property of the gamma function to scale up the real part of z, compute gamma(z), then step back down again.
Local scal
If real(z)<10 Then
10-floor(real(z))scal
Else
0scal
EndIf
© Now we approximate gamma(z + scal)
Local stirling
¨(2*Œ/(z+scal))*(z+scal)^(z+scal)*–^(z-scal+(210*(z+scal)^2+53)/(2520*(z+scal)^3+720*(z+scal)))stirling
© Lastly we scale the result back down
Return stirling/(‹(z+t,t,0,scal-1))
EndFuncTäåÜ?Ó