Stirling series approximation of the gamma function

You're viewing an older version of this page (#110). View the current version.

View versions (1)

Interface

#include <codecogs/maths/special/gamma/stirling.h>

using namespace Maths::Special::Gamma;

This Stirling's series gives an approximate value for the factorial function x! or the Gamma function \Gamma(x) for x>>1.

The asymptotic series for a gamma function is given by

\Gamma(x) \sim \left ( \frac{x}{e} \right )^x \sqrt{2 \pi x} \left( 1 + \frac{1}{12 x} + \frac{1}{288 x^2} - \left. \frac{139}{51840 x^3} - \frac{571}{2488320 x^4} + ... \right )
(1)

The polynomial STIR within stirling are only valid for 33 <= x <= 172. Please note: The upper limit will depend on the accuracy of your computer.

Graphically this function has the form: \graph x=1:5 .log

See Also:

Gamma and Gamma_Simple

Accuracy:

In comparison to integer fractorial, this approximate it accurate to about 15dp at x=35, but obviously very poor for lower values of x (i.e. x<6)

Example 1

Compared the outut from Stirling with a traditional factorial

#include <codecogs/maths/special/gamma/stirling.h>
#include <stdio.h>

int main ()
{
  for(double x=33; x<=40; x+=0.5)
    printf("\n x=%lf stirling(x)=%.0lf",x, Maths::Special::Gamma::stirling(x));
}

Output:

x=33.000000 stirling(x)=263130836933693517766352317727113216
  x=33.500000 stirling(x)=1505856975626702287543640208112091136
  x=34.000000 stirling(x)=8683317618811887119307294612729626624
  x=34.500000 stirling(x)=50446208683494518220996649360199516160
  x=35.000000 stirling(x)=295232799039604195113013396920323801088
  x=35.500000 stirling(x)=1740394199580561001405912957537658863616
  x=36.000000 stirling(x)=10333147966386146640060809577425524490240
  x=36.500000 stirling(x)=61783994085109902705073076587151908405248
  x=37.000000 stirling(x)=371993326789901177492420297158468206329856
  x=37.500000 stirling(x)=2255115784106511937141198419741231238086656
  x=38.000000 stirling(x)=13763753091226345578872114833606270345281536
  x=38.500000 stirling(x)=84566841903994165920581434052426627138191360
  x=39.000000 stirling(x)=523022617466601117141859892252474974331207680
  x=39.500000 stirling(x)=3255823413303776398101457267888729462505734144
  x=40.000000 stirling(x)=20397882081197441587828472941238084160318341120

References

Cephes Math Library Release 2.8: June, 2000 http://mathworld.wolfram.com/StirlingsSeries.html

Parameters

x
a value.

Returns

an approximation of the factorial of x.
GPL Licence — free for non commercial use. See Licence details.

Interactive Calculator

x
Result