Evaluate binomial coefficients: Difference between revisions

Content added Content deleted
(Updated D entry)
Line 1,034: Line 1,034:
Output:
Output:
<lang>The Binomial Coefficient of 5 and 3 equals 10.</lang>
<lang>The Binomial Coefficient of 5 and 3 equals 10.</lang>

Another (more flexible and efficient) implementation. n and k are taken from command line. The use of BigInts allows to compute coefficients of arbitrary size:

<lang scala>object Binomial extends App {
def binomialCoefficient(n: Int, k: Int) =
(BigInt(n - k + 1) to BigInt(n)).product /
(BigInt(1) to BigInt(k)).product

val Array(n, k) = args.map(_.toInt)
println("The Binomial Coefficient of %d and %d equals %s.".format(n, k, binomialCoefficient(n, k)))
}</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==