//=================================================== file = birthday.c ===== //= Program to solve the Birthday paradox = //=========================================================================== //= Notes: = //= 1) Solves for probability of two people in a room of K people = //= having the same birthday (birthday is one of N distinct values = //= such as 1 of 365 days of the year) = //= 2) Input values of K and N must be greater than 0 = //= 3) The birthday problem is described here, = //= http://www.mste.uiuc.edu/reese/birthday/intro.html = //=-------------------------------------------------------------------------= //= Example execution: = //= = //= --------------------------------------- birthday.c ----- = //= - Program to solve the birthday paradox - = //= -------------------------------------------------------- = //= Number of people in room (K) =======================> 23 = //= Number of possible birthdays (N) ===================> 365 = //= = //= Probability of two same birthdays = 0.507297 = //= -------------------------------------------------------- = //=-------------------------------------------------------------------------= //= Build: bcc32 birthday.c, cl birthday.c, gcc birthday.c = //=-------------------------------------------------------------------------= //= Execute: birthday = //=-------------------------------------------------------------------------= //= Author: Zornitza Genova = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~zgenova = //= Email: zgenova@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: ZG (07/17/01) - Genesis = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() and scanf() #include // Needed for ato*() //===== Main program ======================================================== void main(void) { unsigned int K; // Number of people in the room unsigned int N; // Number of possible birthdays double prob; // Probability of two same birthdays unsigned int i; // Loop counter unsigned char instring[255]; // Temporary input string // Output banner printf("--------------------------------------- birthday.c -----\n"); printf("- Program to solve the birthday paradox -\n"); printf("--------------------------------------------------------\n"); // Prompt for input printf("Number of people in room (K) =======================> "); scanf("%s", instring); K = atoi(instring); printf("Number of possible birthdays (N) ===================> "); scanf("%s", instring); N = atoi(instring); // Compute the probability of two same birthdays in the room prob = 1.0; for(i=1; i<=K; i++) prob = prob * ((double) (N - i + 1) / N); prob = 1.0 - prob; // Output the result printf("\n"); printf("Probability of two same birthdays = %f \n", prob); printf("--------------------------------------------------------\n"); }