Skip to content

Instantly share code, notes, and snippets.

@mathewcohle
Last active March 2, 2017 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathewcohle/56430b00be708895e90959bd99dfb57a to your computer and use it in GitHub Desktop.
Save mathewcohle/56430b00be708895e90959bd99dfb57a to your computer and use it in GitHub Desktop.
// 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;
}
@mathewcohle
Copy link
Author

Return solution to "birthday" problem, code snippet from http://ben.klemens.org/pdfs/gsl_stats.pdf [p. 24]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment