階乗
>>> import math
>>> math.factorial(3)
6
>>> math.factorial(4)
24
>>> math.factorial(5)
120
順列の場合の数
>>> from scipy.special import perm
>>> perm(4, 2, exact=True)
12
>>> perm(5, 2, exact=True)
20
順列の生成
>>> import itertools
>>> lst = [1, 2, 3]
>>> itertools.permutations(lst, 2)
<itertools.permutations object at 0x7fd410efef10>
>>> type(itertools.permutations(lst, 2))
<class 'itertools.permutations'>
>>> list(itertools.permutations(lst, 2))
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
>>> list(itertools.permutations(lst, 3))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
>>> list(itertools.permutations(lst))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
組み合わせの場合の数
>>> from scipy.special import comb
>>> comb(4, 2, exact=True)
6
>>> comb(4, 3, exact=True)
4
>>> comb(5, 3, exact=True)
10
>>> comb(5, 4, exact=True)
5
組み合わせの生成
>>> import itertools
>>> lst = [1, 2, 3]
>>> list(itertools.combinations(lst, 2))
[(1, 2), (1, 3), (2, 3)]