Skip to content

Instantly share code, notes, and snippets.

@avi-levy
Last active October 30, 2022 05:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avi-levy/0ca97f5d33028b3e4f399f9dd20b69b8 to your computer and use it in GitHub Desktop.
Save avi-levy/0ca97f5d33028b3e4f399f9dd20b69b8 to your computer and use it in GitHub Desktop.
Evaluating formula for math.SE batched coupon collector problem
# https://math.stackexchange.com/questions/3278200/box-of-10-chocolates/3278285
from math import factorial
from fractions import Fraction
def binom(n,k): # binomial coefficient n choose k
return 0 if n < k else factorial(n)/factorial(k)/factorial(n-k)
def e(n,m): # expected number of days to replace all n chocolates if replaced m at a time
return binom(n,m)*sum(Fraction(pow(-1,s+1)*binom(n,s),binom(n,m)-binom(n-s,m)) for s in range(1,n+1))
e(10,3) # Fraction(8241679, 911064)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment