For each integer n ≥ 1 , let p ( n ) denote the probability that, in a room with n people chosen at random, two (or more) of them share a birthday. Find the value of n for which p ( n ) is as close as possible to 2 1 .
Details and assumptions
Every year has 365 days, a person's birthday is equally likely to fall on any of the possible 365 dates, and the birthdays of different people are independent of each other.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Your ball explanation isn't quite correct with the math.
It's more like, there is a bucket of 10 balls and each person has to pick one. The first person has a 10/10 chance to pick a ball that nobody has picked yet. The second person has a 9/10 chance to pick a ball that hasn't been picked yet.
The wording of your solution doesn't make sense. If you have 10 balls and each person has to pick a ball, but the previously picked balls aren't put back into the bucket, then p(n) is always 1 because 10/10 x 9/9 x 8/8 ... = 1
Thanks @Ashley Schauer for explaining it further. I was really confused by those wordings.
The probability of at least 2 from n people having the same birthday can be stated as:
p ( n ) = 1 − p ( n )
where p ( n ) = 3 6 5 n P n 3 6 5
So, the value of n so that p ( n ) is as close as possible to 2 1 ,
3 6 5 n P n 3 6 5 ≈ 2 1
Hence, the closest is p ( n ) = 0 . 5 0 7 , for n = 2 3 .
This is the Birthday Paradox .
The answer n=23 is correct but the value of P(n) = 0.492702765676...
We find n where p ( n ) = 2 1 , and then find the closest integer to n . To do this, we calculate the probability that no two people have the same birthday. Note that for 2 people, the probability of different birthdays is 3 6 5 3 6 5 ⋅ 3 6 5 3 6 4 = 3 6 5 3 6 4 . For n people, there are ( 2 n ) pairs. The probability that no two pairs share the same birthday is therefore ( 3 6 5 3 6 4 ) ( 2 n ) . Since the probability that at least one pair shares a birthday is 2 1 , the probability that no pair shares a birthday is 1 − 2 1 = 2 1 . So that means that ( 3 6 5 3 6 4 ) ( 2 n ) = 2 1 . Setting this up in logarithm form we get that lo g 3 6 5 3 6 4 2 1 = ( 2 n ) . Using a calculator to solve the log gives ( 2 n ) ≈ 2 5 2 . 6 5 . We round that to 2 5 3 . The equation becomes ( 2 n ) = 2 5 3 . Writing the binomial in factorial form gives 2 ! ( n − 2 ) ! n ! = 2 n ( n − 1 ) = 2 5 3 . Multiplying by 2 and expanding gives the quadratic n 2 − n − 5 0 6 = 0 , and using the quadratic equation gives the solutions n = − 2 2 , 2 3 . Discarding the extraneous negative solution we get that the answer is, almost perfectly, 2 3 (remembering that we rounded off the decimal before doing the quadratic). Therefore the answer is 2 3 .
Was I the only one that used a rigorous solution with no programs and no aid other than a simple scientific calculator?
I used iteration instead of recursion here.
Mathematically, the probability that n people have distinct birthdays as described in the question is ∏ m = 1 n ( 1 − 3 6 5 m ) . Since as n increases this product decreases, we only need to be concerned with 2 specific probabilities - just before and just after adding 1 to n will cause the probability to decrease beyond 0 . 5 .
I wrote the following Java code to solve the problem:
public static void main(String[] args) {
double x = 1;
double v = 1;
int c;
for(c = 365; v > 0.5; c--) {
x = v;
v = x * c/365;
}
System.out.println(v + " at " + (365 - c) + " people");
System.out.println(x + " at " + (365 - c + 1) + " people");
if(Math.abs(0.5 - v) < Math.abs(0.5 - x)) {
System.out.println(365 - c);
} else {
System.out.println(365 - c + 1);
}
}
Errata: The product should have 3 6 5 m − 1 .
My solution using Java:
class Main{
public static void main(String[] args){
double min = 1;
int res = 0;
for(int n=1; n<=999; n++){
double diff = Math.abs(calc(n)-0.5);
if(diff<min){
min = diff;
res = n;
}
}
System.out.println(res);
}
public static double calc(int n){
double result = 1.0;
for(int i=0; i<n; i++){
result = result*(365-i)/365;
}
return result;
}
}
p ( n ) = 2 1 → 1 − p ( n ) = 2 1 q ( n ) : = 1 − p ( n ) = 3 6 5 n 3 6 5 P n = q ( n − 1 ) 3 6 5 3 6 6 − n which 2 1 at n = 2 3
Let us define q ( n ) to be the probability that out of n people, no two share a common birthdate. It can be seen that q ( n ) = ∏ k = 0 n − 1 3 6 5 3 6 5 − k . In addition, one notices that p ( n ) = 1 − q ( n ) .
We write a simple function to compute q ( n ) :
double q(int n)
{
if(n <= 1) return 0.0; // Less than two people
if(n > 365) return 1.0; // Guaranteed due to pigeonhole principle.
double prob = 1.0;
for(int i = 0; i <= n-1; i++)
prob *= (365.0-i)/365.0;
return prob;
}
With this function, we can iterate on values of n from 1 to 3 6 5 and note the value of n where ∣ 2 1 − ( 1 − q ( n ) ) ∣ is minimized.
Long live C
#include <stdio.h>
#include <math.h>
int main() {
double p = 1;
int n, i;
n = 1;
while (p >= 0.5) {
n++;
for(i = 365, p = 1; i > 365 - n; p *= i--);
p /= pow(365,n);
};
printf("The probability is as close to 1/2 for %d people\n", n);
return 0;
}
The chance that at least 2 people share a birthday is the same as 1 - (probability no people share a birthday).
def birthday(n): prod = 1 for i in range(1,n): prod *= fractions.Fraction(365-i,365) return 1 - (prod.numerator / prod.denominator)
This is equivalent to 1 − 3 6 5 ( n − 1 ) ( 3 6 5 − n ) ! 3 6 4 !
The probability that two or more share a birthday is also the same as the 1 - p(no one shares a birthday). To find the value of n which makes 1 - p(no one shares a birthday) as close to 1/2 as possible, we can use this python script:
def birthday(p, numerator, count):
if p < 1/2.0:
return count
p = p * numerator/365.0
return birthday(n, numerator - 1, count + 1)
p(no one shares the same birthday) = everybody having different birthdays. The probability of that is 365/365 * 364/365 * 363/365 * .... This is because after person 1, person 2 can have a birthday on 364 days (1 is already taken), person 3 can then have a birthday on 363 days (2 are already taken), and so on.
1 - p(n) is the probability that no two or more people have birthday the same day. So the question is the same as finding n for which the probability that no two or more people have birthday the same day is 1/2;
The last line of output indicates the number of people
Here is my C code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
double porcentagem = 1; /* 1 - p(n) */
double dif = 0.5; /* difference between 1 - p(n) found and desired */
double last = 0.5; /* last difference computed */
int i; /* number of people */
for (i = 2; porcentagem > 0.5; i++) {
porcentagem = porcentagem * (365 - i + 1)/365.0;
last = abs(0.5 - porcentagem);
printf("porcentagem = %lf, i = %d\n", porcentagem, i);
if (last > dif) {
break;
} else {
dif = last;
}
}
return 0;
}
Let A be the event that of n people in a room, at least two of them share a birthday. Let A c denote the complementary event: all people in the room have distinct birthdays. Then P r ( A ) = 1 − P ( A c ) = 1 − p r o d i = 0 n 3 6 5 3 6 5 − n Computing this for n ∈ { 1 , … , 3 6 5 } shows n = 2 3 gives the probability closest to 0.5.
def f(n) :
s = 1
for i in range(n) :
s *= 1 - i/365.
return s
# Dichotomy
left, right = 1, 365
step = 2
while step >= 1 :
step = (right - left) / 2
if f(int(left + step)) > 0.5:
left += step
else :
right -= step
if f(left) + f(right) < 1 :
print(left)
else :
print(right)
First, we write a function that determines p(n) using a simple complimentary method. Then, we use this to find the desired n.
def birthdays(n):
prob = 1
for i in range(n):
prob *= (365-i)/365
return 1-prob
min(enumerate([abs(birthdays(i)-0.5) for i in range(365)]),key = lambda x: x[1])
My key technique was to find a formula for the probability and then check when the difference between that probability and 1 / 2 was least. The formula I found was p ( n ) = 1 − 3 6 5 n − 1 3 6 4 ⋅ 3 6 3 ⋯ ( 3 6 6 − n ) , which can be verified by considering the complement of p ( n ) (the probability that nobody shares a birthday).
def birthdays():
"""Finds when the probability that people share a birthday is about 1/2"""
closestN = 0
smallestDifference = 1
for n in range(2, 366):
probability = 1
# complementary probability
for multIndex in range(364, 365-n, -1):
probability *= (multIndex/365)
# multiplies by the probability that each person doesn't share a birthday
probability = 1 - probability
# takes the complement
difference = abs(probability - 0.5)
if difference < smallestDifference:
closestN = n
smallestDifference = difference
else:
if probability > 0.5:
break
# if the difference is getting larger, then we can stop looking
return closestN
Given that this is a well-known problem and there is no fun in solving it analytically, why not do the simulation and get the answer? Take N people and assign birthdays randomly to them. Repeat this T times and check how many times (S) a birthday match occurs. Increase N till S becomes >=T/2.
# include <cstdio>
# include <cstdlib>
int ar[367];
const int T=1000000;
int main()
{
//Once you reach 366 people, you will definitely have a match
for(int i=2;i<=366;i++)
{
//Number of scenarios with a birthday match
int cnt=0;
for(int t=0;t<T;t++)
{
//Start simulating a scenario
for(int j=0;j<i;j++)
{
//Assign a birthday randomly
ar[j]=rand()%365;
//Check if someone assigned till now has the same birthday
for(int k=0;k<j;k++)
{
if(ar[j]==ar[k])
{
//This is a birthday-match scenario
cnt++;
goto BPP;
}
}
}
BPP:;
}
if(2*cnt>=T)
{
//Probability >1/2
printf("%d\n",i);
break;
}
}
return 0;
}
The trick here is to instead compute the probability p ′ ( n ) that the room is full of people with distinct birthdays. Since p ( n ) + p ′ ( n ) = 1 , we know p ( n ) − 2 1 = 2 1 − p ′ ( n ) so p ( n ) and p ′ ( n ) are equidistant from 2 1 . Thus, we wish to find the value n minimizing the new function p'(n).
We come up with a formula for p ′ ( n ) inductively. If n is equal to 0 or 1 , then we have just one person in the room so there (trivially) cannot be two people sharing a birthday, so p ′ ( n ) = 1 . If the room has more than one person, say k , and everyone in the room has some unique birthday, then bringing one new person into the room decreases p ′ by the probability that this new person has a unique birthday. Since there are 365 days in the year and k of them are already taken by the room's current inhabitants, the probability that this { k + 1 }th person has a unique birthday is 3 6 5 3 6 5 − k .
Therefore, we have the recurrence relation
p ′ ( 0 ) = p ′ ( 1 ) = 1 , p ′ ( k + 1 ) = p ′ ( k ) ⋅ 3 6 5 3 6 5 − k .
This can either be implemented as a recursive function, or it can be rewritten as p ′ ( n ) = 3 6 5 n P ( 3 6 5 , n ) , where P ( a , b ) = a ⋅ ( a − 1 ) ⋅ … ⋅ ( a − b + 1 ) = b ! a ! .
Finally, to solve the problem, we plug in values one-by-one into our implementation of p'(), until we find the unique local minimum of ∣ p ′ ( n ) − 1 / 2 ∣ on the domain of integers in { 0 , 1 , 2 , … , 3 6 5 } , which happens at n = 2 3 .
Problem Loading...
Note Loading...
Set Loading...
Suppose you have 1 0 balls and each person has to pick a different ball. The first can choose from 1 0 . The second person only has 9 to choose from. So the probability of 2 people choosing different balls is: 1 0 / 1 0 × 9 / 1 0 = 0 . 9 . For three people 1 0 / 1 0 × 9 / 1 0 × 8 / 1 0 = 0 . 7 2 . This can also be applied to the birthday problem. The probability that three people do no have the same birthday is 3 6 5 / 3 6 5 × 3 6 4 / 3 6 5 × 3 6 3 / 3 6 5 = 0 . 9 9 Subtract that value from 1 and you get the probability that they do have the same birthday which is around 0 . 0 1 . What is required now is to write a python script to do similar a computations for us