counting combinations and permutations efficiently

后端 未结 13 1964
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 12:38

I have some code to count permutations and combinations, and I\'m trying to make it work better for large numbers.

I\'ve found a better algorithm for permutations th

13条回答
  •  眼角桃花
    2020-12-02 13:14

    from numpy import prod
    
    def nCr(n,r):
        numerator = range(n, max(n-r,r),-1)
        denominator = range(1, min(n-r,r) +1,1)
        return int(prod(numerator)/prod(denominator))
    

提交回复
热议问题