Stirling's approximation
In mathematics, Stirling's approximation (or Stirling's formula) is an approximation for factorials. It is a good approximation, leading to accurate results even for small values of formula_1. It is named after James Stirling, though a related but less precise result was first stated by Abraham de Moivre. The version of the formula typically used in applications is formula_2 (in Big Theta notation, as formula_3), or, by changing the base of the logarithm (for instance in the worst-case lower bound for comparison sorting), formula_4 Specifying the constant in the error term gives , yielding the more  precise formula: formula_5 where the sign ~ means that the two quantities are asymptotic: their ratio tends to 1 as formula_1 tends to infinity. The following version of the bound holds for all formula_7, rather than only asymptotically: formula_8 Derivation. Roughly speaking, the simplest version of Stirling's formula can be quickly obtained by approximating the sum formula_9 with an integral: formula_10 The full formula, together with precise estimates of its error, can be derived as follows. Instead of approximating formula_11, one considers its natural logarithm, as this is a slowly varying function: formula_12 The right-hand side of this  equation minus formula_13 is the approximation by the trapezoid rule of the integral formula_14 and the error in this approximation is given by the Euler–Maclaurin formula: formula_15 where formula_16 is a Bernoulli number, and is the remainder term in the Euler–Maclaurin formula. Take limits to find that formula_17 Denote this limit as formula_18. Because the remainder in the Euler–Maclaurin formula satisfies formula_19 where big-O notation is used, combining the equations above yields the approximation formula in its logarithmic form: formula_20 Taking the exponential of both sides and choosing any positive integer formula_21, one obtains a formula  involving an unknown quantity formula_22. For , the formula is formula_23 The quantity formula_22 can be found by taking the limit on both sides as formula_1 tends to infinity and using Wallis' product, which shows that formula_26. Therefore, one obtains Stirling's formula: formula_27 Alternative derivation. An alternative formula for formula_11 using the gamma function is formula_29 (as can be seen by repeated integration by parts). Rewriting and changing variables , one obtains formula_30 Applying Laplace's method one has formula_31 which recovers Stirling's formula: formula_32 In fact, further corrections can also be obtained using Laplace's method. For example, computing  two-order expansion using Laplace's method yields (using little-o notation) formula_33 and gives Stirling's formula to two orders: formula_34 A complex-analysis version of this method is to consider formula_35 as a Taylor coefficient of the exponential function formula_36, computed by Cauchy's integral formula as formula_37 where denotes the Stirling numbers of the first kind. From this one obtains a version of Stirling's series formula_38 which converges when . Versions suitable for calculators. The approximation formula_39 and its equivalent form formula_40 can be obtained by rearranging Stirling's extended formula and observing a coincidence between the  resultant power series and the Taylor series expansion of the hyperbolic sine function. This approximation is good to more than 8 decimal digits for with a real part greater than 8. Robert H. Windschitl suggested it in 2002 for computing the gamma function with fair accuracy on calculators with limited program or register memory. Gergő Nemes proposed in 2007 an approximation which gives the same number of exact digits as the Windschitl approximation but is much simpler: formula_41 or equivalently, formula_42 An alternative approximation for the gamma function stated by Srinivasa Ramanujan (Ramanujan 1988) is formula_43 for . The equivalent approximation  for has an asymptotic error of and is given by formula_44 The approximation may be made precise by giving paired upper and lower bounds; one such inequality is formula_45 History. The formula was first discovered by Abraham de Moivre in the form formula_46 De Moivre gave an approximate rational-number expression for the natural logarithm of the constant. Stirling's contribution consisted of showing that the constant is precisely formula_47.