我正在寻找为所有实数 n 和整数 k 定义的二项式系数 ( choose(n,k) ) 的有效 Java 实现,即定义为:
问问题
719 次
1 回答
2
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
于 2013-06-10T11:58:19.550 回答