Skip to content

Instantly share code, notes, and snippets.

@prashantvc
Created February 28, 2012 17:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prashantvc/1933772 to your computer and use it in GitHub Desktop.
Save prashantvc/1933772 to your computer and use it in GitHub Desktop.
Gamma function
double Gamma(double x)
{
if (x < 0)
{
return double.PositiveInfinity;
}
// Gamma(x) = (x-1)! so add 1, to make user gets
// expected result, I really don't want use increamental operator here.
x += 1;
double p1 = Math.Sqrt(2 * Math.PI / x);
double p2 = x + 1.0 / (12 * x - 1.0 / (10 * x));
p2 = Math.Pow(p2 / Math.E, x);
return Math.Round(p1 * p2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment