Events and Probability Spaces · Probability of Union
Lesson 5
from itertools import permutations
for n in range(2, 10):
total, good = 0, 0
for p in permutations(range(n)):
total += 1
if all(p[i] != i for i in range(n)):
good += 1
print(f'{n} {good/total:.7f} {total/good:.7f}')2 0.5000000 2.0000000
3 0.3333333 3.0000000
4 0.3750000 2.6666667
5 0.3666667 2.7272727
6 0.3680556 2.7169811
7 0.3678571 2.7184466
8 0.3678819 2.7182633
9 0.3678792 2.7182837