fork(5) download
  1. def chance(flips=12, russels=8, target=1, remaining=40):
  2. if target == 0: return 1
  3. elif flips == 0: return 0
  4. else: return (
  5. russels/remaining * chance(flips-1, russels-1, target-1, remaining-1) +
  6. (remaining-russels)/remaining * chance(flips-1, russels, target, remaining-1)
  7. )
  8.  
  9. def pp(x):
  10. if x < 0.1: return "{:.2%}".format(x)
  11. else: return "{:.1%}".format(x)
  12.  
  13. print("flips/russels| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8")
  14. print("-------------+-------+-------+-------+-------+-------+-------+-------+-------")
  15. for i in range(1,13):
  16. print("{:2d} | {} | {} | {} | {} | {} | {} | {} | {}".format(
  17. i,
  18. pp(chance(i, target=1)),
  19. pp(chance(i, target=2)),
  20. pp(chance(i, target=3)),
  21. pp(chance(i, target=4)),
  22. pp(chance(i, target=5)),
  23. pp(chance(i, target=6)),
  24. pp(chance(i, target=7)),
  25. pp(chance(i, target=8))
  26. ))
Success #stdin #stdout 0.03s 9232KB
stdin
Standard input is empty
stdout
flips/russels|   1   |   2   |   3   |   4   |   5   |   6   |   7   |   8
-------------+-------+-------+-------+-------+-------+-------+-------+-------
 1           | 20.0% | 0.00% | 0.00% | 0.00% | 0.00% | 0.00% | 0.00% | 0.00%
 2           | 36.4% | 3.59% | 0.00% | 0.00% | 0.00% | 0.00% | 0.00% | 0.00%
 3           | 49.8% | 9.64% | 0.57% | 0.00% | 0.00% | 0.00% | 0.00% | 0.00%
 4           | 60.7% | 17.2% | 2.04% | 0.08% | 0.00% | 0.00% | 0.00% | 0.00%
 5           | 69.4% | 25.7% | 4.57% | 0.35% | 0.01% | 0.00% | 0.00% | 0.00%
 6           | 76.4% | 34.4% | 8.19% | 0.95% | 0.05% | 0.00% | 0.00% | 0.00%
 7           | 81.9% | 43.1% | 12.8% | 2.02% | 0.15% | 0.00% | 0.00% | 0.00%
 8           | 86.3% | 51.3% | 18.3% | 3.65% | 0.38% | 0.02% | 0.00% | 0.00%
 9           | 89.7% | 59.0% | 24.5% | 5.94% | 0.79% | 0.05% | 0.00% | 0.00%
10           | 92.4% | 65.9% | 31.2% | 8.94% | 1.45% | 0.12% | 0.00% | 0.00%
11           | 94.4% | 72.1% | 38.1% | 12.6% | 2.45% | 0.26% | 0.01% | 0.00%
12           | 96.0% | 77.5% | 45.2% | 17.0% | 3.86% | 0.48% | 0.03% | 0.00%