我正在寻找为所有实数 n 和整数 k 定义的二项式系数 ( choice(n,k) ) 的高效 Java 实现,即定义为:
最佳答案
import org.apache.commons.math3.special.Gamma;
/**
* Binomial coefficient for real numbers - the number of ways of picking y
* unordered outcomes from x possibilities
*
* @see http://mathworld.wolfram.com/BinomialCoefficient.html
*
* @param x
* @param y
* @return binomial coefficient to be generalized to noninteger arguments
*/
public double binomial(double x, double y) {
double res = Gamma.gamma(x + 1) / (Gamma.gamma(y + 1) * Gamma.gamma(x - y + 1));
if(Double.isNaN(res)){
return 0.0;
}
return res;
}
因此,对于输入binomial(0.5, 1.0)
,您应该得到0.5
,如Wolfram Alpha
binomial(2.5, 3) = 0.3125
binomial(2.0, 3) = 0.0
binomial(1.5, 3) = -0.0625
关于java - 实数值的二项式系数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8349808/