I’m a newbie and I’m trying to code the following factorial series in python to calculate the first matching number in a series within a finite range.
kd×d×(d−1)×⋯×(d−k+1)d×d×⋯d=k(d−1)!dk(d−k)!
so the expected value for the number needed for a match is
∑k=1d(k+1)k(d−1)!dk(d−k)!.
Due to the size of the numbers it errors:
OverflowError: long int too large to convert to float
So I’m using logs but still getting an error. Wondering if anyone has an good idea on this.
m = 365
q = 1
a=[]
for x in range(q,m):
#y = y + x*(1/365)
#####y = y + (factorial(x)/(factorial(m-x)*(exponent(m,x))))
a.append((log((factorial(m))/exponent(m,x)))*log((q+x)/m))
#y = [(m-x)*factorial(m-x)/m]
#print ("x: ",x," y: ",y)
#return "a:",a," product-sum:",[a*a for a in a]
return sum(a)
Sorry I see the equation above isn’t clear. Here’s what I’m trying to get at:
http://en.wikipedia.org/wiki/Birthday_problem#Average_number_of_people
EDIT: Just realized – I don’t think logs are going to help you out much here in the first place: you are trying to compute a sum, and logs aren’t very friendly with sums (they’re good for products).
The link you give suggests another approach (the formula immediately after the one you cite) which should avoid large numbers if you compute in the right order. But the accumulation of rounding errors might affect the result. In code
That said, using an asymptotic formula like the one given is probably a better way to compute this.