Problem:
How many people must be in a room to reach 50% of the same birthday for both of them?
In order to answer this question, set:
1, set K is the total number of rooms, each person is numbered, then the number is 1,2,3 K
2, set all the years are 365 days, the maximum number of days n=365
3, BI represents the first person's birthday days, so 1<=bi<=360,1<=i<=k
Public classMain { /*** The first person's birthday happens in the probability of "R days" for: * * p{bi=r} = 1/n*/ /*** Personal and J personal Birthdays, "the probability of all falls on the R Day" is: * * p{bi=r and Bj=r} = p{bi=r}*p{bj=r} = (1/n) ^2*/ /*** I personally and J personal birthdays, "are the probabilities falling on the same day" for? * This falls on the same day and is not designated to fall on that date, so it can be either the 1th day or the next day or * * P{BI=BJ} * = p{bi=1}*p{bj=1} + p{bi=2}*p{bj=2}+ +p{bi=n}*p{bj=n} * = (1/n) ^2 + (1/n) ^2 + + (1/n) ^2 * = 1/n*/ /** * The original question is: find "at least two people birthday equal" * In other words: 1 minus all birthdays are not the same probability. * So next we're going to find the probability that everyone's birthdays are different. * * Set: * 1, there are k individuals, this K personal birthdays are different events: Bk * 2, then K personal birthdays are different events probability is: P{BK} * 3, there is a person I , there are many individuals 1-j, of which j<i (that is, the number of individuals from 1 to J, and J number is also less than I number) * This personal and 1-j personal birthdays are not the same event: Ai * (i.e.: I and 1 of the birthday is different, and I and 2 of the birthday is different ...) And I is different from J's birthday. * Note: Here 1-j between individual birthdays are not mandatory) * 4, this personal and 1-j personal birthdays are not the same as the probability of the event is P{ai} * * So we can launch: * Bk = A1 * A2 * A3 * ··· * Ak * because: * BK refers to the event that the K-person, "22" birthdays vary. * A1 refers to events ranging from 1 people to "22" birthdays. * A2 refers to the event of a 2nd person and a 1th person's birthday. * A3 refers to the 3rd person and the 1th person's birthday, and the 3rd person and the 2nd birthday of each other. * A4 refers to the 4th person and the 1th person's birthday, and the 4th person and the 2nd birthday of each other. The 4th person and the 3rd person's birthday are different. * ····· * So A1 to AK all the events that happen, is BK this event. * * Conversion to probability is: * P{BK} = p{a1} * p{a2} * p{a3} * * P{ak} * * At this point we will consider this question from another angle, * B (k-1) refers to the (k-1) individual, "22" birthday events. * this b (k-1) event means that the 1th person to the k-1 individual's birthday "is not equal", * at this time if we want to "add the K-person", and "still want them all birthdays are not equal (that is, to reach the event BK)", * only need to meet "K person's birthday and 1th birthday is different, and 2nd person Birthday is different ... And the birthday of the person (k-1) is different "this event can be. * (that is, it requires only fullFoot K differs from 1th to K-1, while B (k-1) means "1th to K-1" is 22 different) * converted to a formula: * Bk = B (k-1) * Ak * * from the previous thinking point of view, * According to this formula: Bk = B (k-1) * Ak, I know: * This (k-1) individual each have their own birthdays, and do not repeat, that is, 365 days a year, their birthdays accounted for (k-1) days. * If the AK event is to be achieved, the first K's birthday "cannot be any day of this (k-1) day". * Then the probability that the AK event (that is, the k-1 birthdays are unequal) is: (n (k-1))/N, (K's birthday must be a day outside this (k-1) day). * i.e.: P{ak} = (n (k-1))/n * * * In summary, we can get the following formula: * P{BK} * = P{b (k-1)} * P{ak} * = P{b (k-2)} * P{a (k-1)} * P{ak} * = P{b (k-3)} * P{a (k-2)} * P{a (K-1)} * P{ak} * * = p{a1} * p{a2} * p{a3} * * P{a (K-1)} * P{ak} * = 1 * (((N-(1))/N) * (((N-(2))/n) * * ((n (k-2))/N) * (((n (k-1))/n) * = 1 * (On (1/n)) * ((2/n)) * * ((k-2)/N) * ((k-1)/N)*/ /*** * by the original title "Birthday Equal opportunity to reach 50%", * So: 1-P{BK} >=50%, that is: P{BK} <= 1/2 * * The following is the use of some mathematical knowledge solved: * According to inequalities: 1 +x<=e^x, "-((k-1)/N)" as x in the inequality, get: * P{BK} <= e^ (-(1/n)) * * e^ (-((k-1)/n)) * P{BK} <= e^ ((-k* (k-1))/2n) * e^ ((-k* (k-1))/2n) <= 1/2 (original title) * * Will be n=365 when * Solution: K>=23 * Therefore, the number of people in a room must reach 23, in order to make two of them the same birthday opportunity to reach 50%*/ Public Static voidMain (string[] arg) {intSum_day = 365; intSum_people =Birth_paradox (Sum_day); System.out.println ("When a year has" +sum_day+ "); System.out.println ("The number of people in a room must reach the" +sum_people+ "to make the two of them have the same birthday chance to reach 50%.); } /*** When a year is sum_day, * How many people must be in a room to achieve the same chance for two of them to reach 50% *@paramsum_day Total number of days in a year *@returnat least how many people are needed to meet the requirements .*/ Public Static intBirth_paradox (Doublesum_day) { //total number of persons required intsum_people; //Initial to P{a1}=1 DoublePb = 1; /*** From the beginning of the sum_people=1 individuals to look for their 22 birthday, whether the event probability is established. * If not established, then sum_people+1. */ for(Sum_people = 1;sum_people<=sum_day+1;sum_people++){ //P{ak} DoublePa = ((sum_day)-sum_people+1)/Sum_day; //P{BK} = P{b (k-1)} * P{ak}Pb = pb*Pa; //if 1-P{BK} >=50% if((1-PB) >= 0.5){ returnsum_people; } } returnsum_people; }}
Algorithm problem--Birthday paradox