Created
November 30, 2015 13:03
-
-
Save gregorykelleher/ffc875deffb68dcd309d 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
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)); | |
} | |
Author
gregorykelleher
commented
Nov 30, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment