[R] birthday problem (factorial limit)
Thomas Lumley
tlumley at u.washington.edu
Sun Sep 28 22:32:09 CEST 2008
On Sun, 28 Sep 2008, Jörg Groß wrote:
> Hi,
>
> I tried to calculate the formula for the birthday problem
> (the probability that at least two people out of a group of n people share
> the same birthday)
>
> But the factorial-function allows me only to calculate factorials up to 170.
>
You might want to look at the pbirthday() and qbirthday() functions and
the examples on their help page. The function implements Diaconis &
Mosteller's approximation, which works for different values of two as well
as of n and 365. One of the examples compares to the exact calculation,
which it computes as x2<- 1-sapply(10:100,
function(n)prod((365:(365-n+1))/rep(365,n)))
This is obviously slow compared to the formula with factorials, but it
does only take two milliseconds on my laptop, so it's not *that* slow. The
phrase "premature optimization" springs to mind.
-thomas
More information about the R-help
mailing list