Skip to content

Instantly share code, notes, and snippets.

@ardeshir
Last active December 27, 2015 12:19
Show Gist options
  • Save ardeshir/7325376 to your computer and use it in GitHub Desktop.
Save ardeshir/7325376 to your computer and use it in GitHub Desktop.
Calculate the possible match of someone's birthday with yours
#include <math.h>
#include <stdio.h>
typedef struct {
double one_match;
double none_match;
} bday_struct;
int upto = 40;
void calculate_days(bday_struct days[]);
void print_days(bday_struct days[]);
int main(int argc, char const *argv[]) {
bday_struct days[upto+1];
calculate_days(days);
print_days(days);
return 0;
}
void calculate_days(bday_struct days[]) {
int ct;
days[1].none_match = 1;
for(ct=2; ct<=upto; ct ++) {
days[ct].one_match = 1 - pow(364/365., ct - 1);
days[ct].none_match = days[ct-1].none_match * (1 - (ct - 1)/365.);
}
}
void print_days(bday_struct days[]) {
int ct;
printf("People\tMatches me\t Any match\n");
for(ct = 2; ct<=upto; ct ++) {
printf("%i\t%.3f\t\t%.3f\n", ct, days[ct].one_match, 1 - days[ct].none_match);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment