Skip to content

Instantly share code, notes, and snippets.

@defacto133
Created January 7, 2016 00:39
Show Gist options
  • Save defacto133/25f3ce8751dd6df342ed to your computer and use it in GitHub Desktop.
Save defacto133/25f3ce8751dd6df342ed to your computer and use it in GitHub Desktop.
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