TITLE: The probability that x birthdays lie within n days of each other
QUESTION [12 upvotes]: This is a question that has bugged me for quite some time: what is the chance that x people happen to have their birthdays within n days of each other?
A bit more specific, since this is how a colleague one phrased it: what is the probability that 5 people have their birthdays within 40 days? 
Both the birthdays and the "distance" are supposed to be random: there is no fixed time span (e.g., April 1 to May 10) that the birthdays are to lie within. The birthdays should be such, that two birthdays are always within 40 days of each other.
The thing that bugs me, is that it seems to be some kind of recursive calculation, and that I can't find a way to put it into a straightforward mathematical formulation.
To explain that, consider 2 people: the first person is free to have his or her birthday $b_1$ any day of the year, and the second person has 81 days to pick a birthday date $b_2$ from (the 40 day timespan is inclusive, so up to 40 days before $b_1$, plus up to days after $b_1$, plus one on $b_1$ itself. This may be more logically phrased as 41 days for some; I don't know what is best, so please be clear about it in your answer).
Now, for the third person, the number of birthdays he or she can have, is limited by the second person's birthday: if $b_2 = b_1$, then $b_3$ can be among 81 days, but if $b_2 = b_1 + 1$ or $b_2 = b_1 - 1$, there are only 80 days for each option, and 79 for $\|b_1 - b_2\| = 2$, etc.
For the fourth person, the limitation is given by person 2 and 3, complicating things; the fifth person makes things even more complicated.
I've also tried to go the "exclusion" way (what is the chance that 5 people do not share their birthdays within 40 days of each other), but I didn't get anywhere that way.
But perhaps I'm going entirely the wrong way about this.
By now, I've computed it in various way, and I'm quite confident of the answer, but I'm still looking for the mathematical formulation of the general (x birthdays, n days) problem.
The answer I've got, btw, is $7.581428 \cdot 10^{-4}$, or $\frac{13456201}{365^4}$. 
NB: this obviously assumes no leap years.
NB2: Extension of the Birthday Problem appears related, though I can't readily see if I can use any of that formulation here.

REPLY [3 votes]: Here's is another elementary approach based on combinatorial enumeration.
Restriction: This is only a partial answer valid for $1\leq n\leq 183$. A complete answer is planned to follow.

We assume the year having $365$ days. We assume a situation with $x \geq 2$ people and ask for a birthday of them within an interval of length $1\leq n\leq 183$ days.

A starter: $n=2,x=3$

To motivate the approach I would like to give a small example, namely asking for the probability $P(n=2,x=3)$ of three people having their birthday within two consecutive days.
First we determine the number $T$ of all possible configurations. 
  \begin{align*}
T=365^x=365^3\tag{1}
\end{align*}

Note: Please note, that if we agree that $(1)$ is correct, we observe that e.g. the tripel $(1,365,1)$ means, that the first person's birthday and the third person's birthday is the first day in the year. The second persons birthday is the last day in the year and the configurations $(1,365,1)$ and $(1,1,365)$ are different.
Now, we derive the number $X$ of proper configurations by simply enumerating them.

There are $X_1=365$ proper configurations of three people with birthday on exactly one day
  \begin{array}{lllr}
(1,1,1)&\quad\dots\quad&(364,364,364),&(365,365,365)\tag{2}\\
\end{array}
  and there are $X_2=6\cdot365$ proper configurations of three people having their birthday on exactly two consecutive days
  \begin{array}{lllr}
(1,1,2)&\quad\dots\quad&(364,364,365),&(365,365,1)\tag{3}\\
(1,2,1)&\quad\dots\quad&(364,365,364),&(365,1,365)\\
(2,1,1)&\quad\dots\quad&(365,364,364),&(1,365,365)\\
(1,2,2)&\quad\dots\quad&(364,365,365),&(365,1,1)\\
(2,1,2)&\quad\dots\quad&(365,364,365),&(1,365,1)\\
(2,2,1)&\quad\dots\quad&(365,365,364),&(1,1,365)\\
\end{array}

This gives a total of $X=X_1+X_2=7\cdot365$ proper configurations.

We conclude:
\begin{align*}
P(2,3)=\frac{X}{T}=\frac{7}{365^2}\simeq5.3\cdot10^{-5}
\end{align*}

Now we generalise this approach.

Generalisation: $P(n,x)$ for $x\geq 2$ and $1\leq n\leq 183$

First step: $n=1$

We start the easy way with $n=1$. If there are $x$ people and we ask for having their birthay within exactly one day, there are $365$ proper configurations corresponding to $(1)$, namely the $x$-tuples
  \begin{array}{lllr}
\underbrace{(1,1,\dots,1)}_{x}&\quad\dots\quad&\underbrace{(364,364,\dots,364)}_{x},&\underbrace{(365,365,\dots,365)}_{x}\\
\end{array}

So we get for

$n=1$:
\begin{align*}
X_1=365
\end{align*}

Second step: $2\leq n\leq 183$

Let's assume an interval of length $k$ with $2\leq k \leq n$ and let's ask for the number of proper configurations of people having their birthday within an interval of length exactly $k$.
So, we have to determine the number $Y_k$ of $x$-tuples which have values only within the interval and which also contain at least one left endpoint and at least one right endpoint of the interval. This number is
  \begin{align*}
Y_k=k^x-2(k-1)^x+(k-2)^x\qquad(x \geq 2)
\end{align*}

Observe that $k^x$ gives the number of all $x$-tuples with (the $k$ different) values from the interval. We subtract $2(k-1)^x$ which is the number of $x$-tuples having no left endpoint or having no right endpoint of the interval. Since we subtracted the number of $x$-tuples, which have neither left nor right endpoint  twice, we have to add $(k-2)^x$ for compensation according to the Inclusion-Exclusion Principle.
There are $365$ different intervals of length $k$, namely:

\begin{align*}
&[1,2,\dots,k]\\
&[2,3,\dots,k+1]\\
&\quad\dots\\
&[364,365]\cup[1,2,\dots,k-3,k-2]\\
&[365]\cup[1,2,\dots,k-2,k-1]\\
\end{align*}

And since we can vary $k$ between $2$ and $n$, we observe that $X_2$, the number of proper configurations of $x\geq 2$ people having their birthdays within $n$ days is after some simplifications for

$2 \leq n\leq 183$:
  \begin{align*}
X_2&=365\sum_{k=2}^{n}Y_k\\
&=365\sum_{k=2}^{n}\left(k^x-2(k-1)^x+(k-2)^x\right)\\
&=365\left(n^x-(n-1)^{x}-1\right)
\end{align*}

This gives a total of $X=X_1+X_2=365\left(n^x-(n-1)^x\right)$

We conclude:
\begin{align*}
P(n,x)=\frac{X}{T}=\frac{n^x-(n-1)^x}{365^{x-1}}\qquad(1\leq n \leq 183), \quad (x\geq2)
\end{align*}

in accordance with the answer of mjqxxxx if we additionally respect $n\leq 183$.

Note: Observe, that if we ask for the probability of two people having their birthday within $183$ days, the result should give $1$. Indeed we get
  \begin{align*}
P(n,x)=P(183,2)=\frac{183^2-182^2}{365}=\frac{365}{365}=1
\end{align*}
Note: Observe, that due to some circular overlappings of intervals in case of $184 \leq n \leq 365$ some proper configurations are repeatedly counted, so that for some values of $x$
  \begin{align*}
 P(n,x) \geq 1\qquad (184 \leq n \leq 365)
 \end{align*}

To correct the formula of $P(n,x)$ in this case should be the next activitiy :-)