Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- ''' Expected numbers of coupons required to collect m sets of n coupons
- Using integral from p.23 of Ferrante & Saltalamacchia
- http://mat.uab.cat/matmat/PDFv2014/v2014n02.pdf
- Written by PM 2Ring 2015.09.05
- '''
- from __future__ import print_function, division
- import sys
- from mpmath import mp
- #Set the precision
- #mp.dps = 30
- zero = mp.mpf(0)
- one = mp.mpf(1)
- #Factorial table
- factorials = []
- def make_func(n, m):
- fac = factorials[:m]
- def func(t):
- smt = sum(t**k / f for k,f in fac)
- return one - (one - smt * mp.exp(-t)) ** n
- return func
- def main():
- #n: number of coupons; mm: max number of sets
- n = int(sys.argv[1]) if len(sys.argv) > 1 else 101
- n = mp.mpf(n)
- mm = int(sys.argv[2]) if len(sys.argv) > 2 else 10
- #Build factorial table
- factorials.append((zero, one))
- for i in range(1, mm):
- ii = mp.mpf(i)
- factorials.append((ii, ii * factorials[i-1][1]))
- print('Expected numbers of coupons required to collect m sets of %d coupons' % n)
- print(' m: expected number')
- for m in range(mm):
- v = n * mp.quad(make_func(n, m), [zero, mp.inf])
- print('%2d: %s' % (m, mp.nstr(v, 12)))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement