Statistics: combinations in Python

前端 未结 18 1528
南旧
南旧 2020-11-27 10:14

I need to compute combinatorials (nCr) in Python but cannot find the function to do that in math, numpy or stat libraries. Something

18条回答
  •  孤城傲影
    2020-11-27 10:44

    This function is very optimazed.

    def nCk(n,k):
        m=0
        if k==0:
            m=1
        if k==1:
            m=n
        if k>=2:
            num,dem,op1,op2=1,1,k,n
            while(op1>=1):
                num*=op2
                dem*=op1
                op1-=1
                op2-=1
            m=num//dem
        return m
    

提交回复
热议问题