def p(items_in_bucket, cur_items, m):
if items_in_bucket not in m:
m[items_in_bucket] = {}
if cur_items in m[items_in_bucket]:
return m[items_in_bucket][cur_items]
if items_in_bucket == 0:
# Out of all cur_items, none ended up in the bucket
m[items_in_bucket][cur_items] = pow(1-1/buckets,cur_items)
elif items_in_bucket == cur_items:
# Out of all cur_items, all ended up in the bucket
m[items_in_bucket][cur_items] = pow(1/buckets, cur_items)
else:
# P(already have enough items in this bucket)*P(item will hit another bucket) +
# P(missing only one item in this bucket)*P(item will hit this bucket)
m[items_in_bucket][cur_items] = \
p(items_in_bucket, cur_items-1, m)*(1-1/buckets) + \
p(items_in_bucket-1, cur_items-1, m)*1/buckets
return m[items_in_bucket][cur_items]
items = 996
bucket_total_colors = {
100:"\033[92m", # green
1000:"\033[91m", # red
10000:"\033[94m"} # blue
for buckets in [100,1000,10000]:
print(bucket_total_colors[buckets])
print(f"{buckets} buckets, {items} items (Poisson λ={items/buckets:0.4})\n")
memo = {}
for i in range(20):
prob = p(i,items,memo)
print(f"{i}-item buckets: {prob*buckets:.3f}, P={prob:.7f}, {prob*100:.5f}%")
print("\033[0m")