Skip to content

Instantly share code, notes, and snippets.

@gregorykelleher
Created November 30, 2015 13:03
Show Gist options
  • Save gregorykelleher/ffc875deffb68dcd309d to your computer and use it in GitHub Desktop.
Save gregorykelleher/ffc875deffb68dcd309d to your computer and use it in GitHub Desktop.
public static double birthday(int n) {
int dups = 0;
double value = 0.0;
for (int i = 0; i < n; i++) {
boolean foundDup = false;
int[] bday = new int[n];
// assign birthdays to n people
for (int i = 0; i < n; i++) {
bday[i] = (int) (Math.random() * 365) + 1;
}
// check, to see if there's a duplicate
for (int i = 1; i <= 365; i++) {
int num = 0;
for (int j = 0; j < n; j++) {
// comparing bdays to dates
if (bday[j] == i) {
num++;
}
}
if (num > 1) {
foundDup = true;
}
num = 0;
}
// count cases with duplicates
if (foundDup == true) {
dups++;
}
}
double value = (double) dups / n;
return (value*birthday(x));
}
@gregorykelleher
Copy link
Author

    double value = 0.0;
    int x = 0;


    if (n == 1) {
        return 0;
    }

    else {

     // P(N) = (1 - P(N-1)) * ((N-1)/365)

     value = 1 - birthday(n-1);
     x = (n-1)/365;

    }

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