Last active
March 2, 2017 12:49
-
-
Save mathewcohle/56430b00be708895e90959bd99dfb57a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -Wall -g birthday.c -o birthday -lm | |
#include <math.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char **argv){ | |
double no_match = 1; | |
double matches_me; | |
double odds = 364./365; | |
int ct; | |
if(argc != 2){ | |
printf("Function accepts only one argument, maximum number of persons\n"); | |
exit(1); | |
} | |
int num_persons = atoi(argv[1]); | |
if(num_persons < 2){ | |
printf("Number of persons need to be at least 2\n"); | |
exit(1); | |
} | |
printf("Calculating for %i persons\n", num_persons); | |
printf("People\tMatches me\tAny match\n"); | |
for(ct = 2; ct <= num_persons; ct++) | |
{ | |
matches_me = 1 - pow(odds, ct-1); | |
no_match *= (1 - (ct-1)/365.); | |
printf("%i\t %.4f\t\t%.3f\n", ct, matches_me, (1-no_match)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Return solution to "birthday" problem, code snippet from http://ben.klemens.org/pdfs/gsl_stats.pdf [p. 24]