# Algorithm to compute Gamma function

The question is simple. I would like to implement the Gamma function in my calculator written in C; however, I have not been able to find an easy way to programmatically compute an approximation to arbitrary precision.

Is there a good algorithm to compute approximations of the Gamma function?

Thanks!

-

Looks like the Lanczos approximation will suit my needs : http://en.wikipedia.org/wiki/Lanczos_approximation

-
This note should be of interest. –  Ｊ. Ｍ. Jul 27 '11 at 9:49

Do you want an algorithm for its complex domain or just for real numbers?

-
complex would be better –  houbysoft Jan 27 '11 at 15:47
For reals it in is the GNU C library (haven't checked if it is mandated by the standard). –  vonbrand Mar 1 '13 at 16:07
@vonbrand: not to arbitrary precision, though. –  houbysoft Jan 8 at 10:19

Someone asked a similar question yesterday. I thought of replacing $e^{-t}$ by a series. $$\Gamma (z) = \int_{0}^{\infty} t^{z-1} e^{-t} dt \approx \sum_{j=0}^{a} \frac{(-1)^j b^{j+z}}{(j + z) j !} . \text{Choose } a > b ,$$ but as J. M. points out, I should have checked this a bit better. Take great care in the choice of $a, b$.

-
Have you tried implementing this? :) –  Ｊ. Ｍ. Oct 9 '11 at 10:17
Not with more than one $z$. It can be very inaccurate depending on the choice of $a, b$. Now that I've tried a few more $z$, it looks like $a = b z^2$ is about right, but $b$ must also be chosen not too far from $z$. Example: $z = 6.6$, $b=2 \times z$, $a = b z^2$. This is only a guess! –  Samuel Hambleton Oct 9 '11 at 11:32

It is now part of the C++11 standard library.

http://en.cppreference.com/w/cpp/numeric/math/tgamma

-
What you link to is the C++ interface to the standard C library. –  vonbrand Mar 1 '13 at 16:09
This is not to arbitrary precision, though. –  houbysoft Jan 8 at 10:19