Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
(defn binom [n k]
(let [fact #(apply * (range 1 (inc %)))]
(/ (fact n)
(* (fact k) (fact (- n k))))))
int binom(int n, int k) {
int result = 1;
for (int i = 0; i < k; i++) {
result = result * (n - i) ~/ (i + 1);
}
return result;
}
integer, parameter :: i8 = selected_int_kind(18)
integer, parameter :: dp = selected_real_kind(15)
n = 100
k = 5
print *,nint(exp(log_gamma(n+1.0_dp)-log_gamma(n-k+1.0_dp)-log_gamma(k+1.0_dp)),kind=i8)
binom n k = product [1+n-k..n] `div` product [1..k]
const fac = x => x ? x * fac (x - 1) : x + 1
const binom = (n, k) => fac (n) / fac (k) / fac (n - k >= 0 ? n - k : NaN)
gmp_binomial($n, $k);
sub binom {
my ($n, $k) = @_;
if ($k > $n - $k) { $k = $n - $k }
my $r = 1;
for ( my $i = $n/$n ; $i <= $k;) {
$r *= $n-- / $i++
}
return $r
}
def binom(n,k)
(1+n-k..n).inject(:*)/(1..k).inject(:*)
end