Created
January 7, 2016 00:39
-
-
Save defacto133/25f3ce8751dd6df342ed 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
def choose(n, k): | |
""" | |
A fast way to calculate binomial coefficients by Andrew Dalke (contrib). | |
""" | |
if 0 <= k <= n: | |
ntok = 1 | |
ktok = 1 | |
for t in xrange(1, min(k, n - k) + 1): | |
ntok *= n | |
ktok *= t | |
n -= 1 | |
return ntok // ktok | |
else: | |
return 0 | |
def a(N, P, n): | |
res = 1 | |
for i in range(n): | |
res *= (P-i)/float(N-i) | |
return res | |
def f(N, W, B, K, T): | |
res = choose(K, T) * a(N, W, T) * a(N-T,B,K-T) | |
#res = choose(K, T) * (float(B)/N)**(K-T) * (float(W)/N)**(T) | |
print(str(T) + " white balls: " + str(res)) | |
return res | |
N = 10 | |
W = 3 | |
B = 7 | |
K = 7 | |
print(sum([f(N, W, B,K, T) for T in range(0,4)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment