# Trouble with Stirling's formula

1. Mar 23, 2013

### stepheckert

I am trying to solve for probabilities in systems with a large number of elements. To deal with the large factorials that appear in these formulas, I use Stirling's formula, lnm!=mlnm-m+(1/2)ln(2πm). My problem is that after I get the approximation and try to plug it into the probability formula, N!/(n!(N-n)!)(p^n)(q^(N-n)), I get overflows on my calculator. How can I get around this?

Here is an example problem:

Using Stirling's formula, calculate the probability of getting exactly 500 heads and 500 tails when flipping 1000 coins.

The probability equation looks like: (1000!/(2!998!))(1/2)^2(1/2)^998

I used lnm!=mlnm-m+(1/2)ln(2πm) to get: ln998!=5898.3 and ln1000!=5912.2

I wound up with somehting that looked like this: (e^5912.2/2e^5898.3)(1/2)^2(1/2)^998

How do I get my calculate to work with these kind of numbers??

Thanks for any help!!

2. Mar 24, 2013

### TSny

Looks like you switched from 500 heads and 500 tails to 998 heads and 2 tails (or 2 heads and 998 tails).
If you collect all of your factors of 2, you get 21001 in the denominator. Can you write that as e raised to some power? If so, you should be able to combine all of the exponential factors and express the answer as e raised to a large negative power.

3. Mar 26, 2013

### SteamKing

Staff Emeritus
The factor (1000!/(2! 998!)) can be reduced as follows:

1000! = 1000 * 999 * 998!

therefore (1000!/(2! 998!)) = 1000 * 999 * 998! / (2 * 998!) = 999000 / 2 = 499,500