Skip to content

Instantly share code, notes, and snippets.

@lykkin
Last active December 1, 2016 03:28
Show Gist options
  • Save lykkin/53df1cbd29ed8692ac18b28ace451d79 to your computer and use it in GitHub Desktop.
Save lykkin/53df1cbd29ed8692ac18b28ace451d79 to your computer and use it in GitHub Desktop.
function fact(n) {
var res = 1
for (var i = 1; i <= n; i++) {
res *= i
}
return res
}
function choose(n, k) {
return fact(n)/(fact(k) * fact(n - k))
}
// out of n distinguishable choices, we have a subset of k elements
// this returns the probability of getting at least one of the k elements when choosing groups of j
function probWantedSubset(n, k, j) {
var res = 0
for (var i = 0; i < Math.min(j, k); i++) {
res += choose(k, j-i)*choose(n-k, i)
}
res /= choose(n, k)
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment