Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solution to the "Birthday problem" (https://en.wikipedia.org/wiki/Birthday_problem) in Java
// from guava library
import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;
// change these two variables
final long nPeople = 23;
final long nDays = 365;
ContiguousSet<Long> matrix = ContiguousSet.create(
Range.closed(nDays - nPeople + 1, nDays),
DiscreteDomain.longs()
);
final double prod = matrix.stream()
.mapToDouble(Long::doubleValue)
.reduce((left, right) -> left * right)
.getAsDouble();
double res = 1 - prod / Math.pow(nDays, nPeople);
System.out.println("res: " + res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.