First, let's compute the probability \overline{p(n)} that in a group of people all birthdays are distinct. If is greater than , then by the pigeonhole principle this probability is zero. If does not exceed , we reason as follows.
We choose the first person and note his birthday. Then we choose the second person: the probability that his birthday does not coinside with the first person's birthday is 1 - 1 / 365. For the third person, the probability that his birthday does not coinside with the birthdays of the first two people is 1 - 2 / 365.
Continuing this reasoning, for the last person the probability that their birthday does not coinside with the birthdays of all the previous ones is 1 - (n - 1) / 365.
Multiplying all these probabilities, we obtain the probability that all birthdays in the group are different:
Then, the probability that at least two people out of share the same birthday is equal to one minus the probability that all birthdays are distinct:
Algorithm implementation
In the array we’ll store the corresponding probability values .
double p[401];Let p[1] = 1. The remaining values of the array are computed using the formula given in the problem analysis.
p[1] = 1.0;
for (i = 2; i < 401; i++)
p[i] = p[i-1] * (1.0 - (i - 1.0) / 365);For each input value , compute the corresponding probability and print the result.
while (scanf("%d",&n) == 1)
printf("%.8lf%%\n",(1 - p[n]) * 100);