python中有数学nCr函数吗? [英] Is there a math nCr function in python?
问题描述
可能存在重复:
统计:Python中的组合
计算组合和有效排列
python项目euler问题(问题53)
我期待看看是否内置了python中的数学库是nCr(n选择r)函数:
据我所知,这可以编程,但我认为我会在做之前检查它是否已经内置。
以下程序以有效的方式计算 nCr
计算阶乘因子等)
pre $ $ $ $ $ $ $导入运算符为
def ncr(n,r)
r = min(r,nr)
numer = reduce(op.mul,xrange(n,nr,-1),1)
denom = reduce(op.mul,xrange(1,r + 1) ,1)
return numer // denom
Possible Duplicates:
Statistics: combinations in Python
counting combinations and permutations efficiently
Project euler problem in python (problem 53)
I'm looking to see if built in with the math library in python is the nCr (n Choose r) function:
I understand that this can be programmed but I thought that I'd check to see if it's already built in before I do.
The following program calculates nCr
in an efficient manner (compared to calculating factorials etc.)
import operator as op
def ncr(n, r):
r = min(r, n-r)
numer = reduce(op.mul, xrange(n, n-r, -1), 1)
denom = reduce(op.mul, xrange(1, r+1), 1)
return numer//denom
这篇关于python中有数学nCr函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!