Skip to main content

Section 1.7 The Birthday Problem

Sage is basically a massive Python library. In this section we define a Python function and employ it with other Sage functions to study a curious result and to further demonstrate the versatility of Sage.

In a group of \(n\) people some may be born on the same day of the year, and others may be born on different days of the year. We can compute the probability that everybody in the group was born on a different day, and subtract that from \(1\) to get the probability that at least two people in the group have the same birthday.

This is a combinatorial problem if we consider assigning birthdays to people. First, without reusing days, and second in all possible ways. We obtain the following formula:

\begin{equation*} 1-\frac{\prod_{i=0}^{n-1}\,\left(365-i\right)}{365^n}. \end{equation*}

The curious result is that this expression is roughly \(50\%\) when \(n=23\text{!}\)

As promised, we now define a Python function, using the def keyword and accepting one mandatory argument, n, being the number of people in the room. When you run this cell, there will be no output, but the function birthday will be defined as a result.

We compute two sample values, including the behavior at \(n=23\text{.}\) Notice that the results are exact rational values, which we then approximate with decimal versions.

And here is the plot for \(1\leq n\leq 50\text{,}\) which is easy with the function we defined.

You can certainly find much more about this problem online. One interesting fact is that we have implicitly assumed birthdays are uniformly distributed throughout the year. This is not the case, and the non-uniform distribution makes the probabilities even greater.

Notice that much of this section is raw Python, but we have relied on Sage to work with large integers (already \(53\) decimal digits for \(n=23\)) so we can defer division and approximations to the very last steps of the computation.