The OpenD Programming Language

binomial

Computes the binomial coefficient of n and k. It is also known as "n choose k" or more formally as _n!/_k!(_n-_k). If a fixed-length integer type is used and an overflow happens, 0 is returned.

Uses the generalized binomial coefficient for negative integers and floating point number

R
binomial
(
R = ulong
T
)
(
const T n
,
const T k
)
if (
isArithmetic!(R, T) &&
(
(
is(typeof(T.min < 0)) &&
is(typeof(T.init & 1))
)
||
!is(typeof(T.min < 0))
)
)

Parameters

n T

arbitrary arithmetic type

k T

arbitrary arithmetic type

Return Value

Type: R

Binomial coefficient

Examples

assert(binomial(5, 2) == 10);
assert(binomial(6, 4) == 15);
assert(binomial(3, 1) == 3);

import std.bigint: BigInt;
assert(binomial!BigInt(1000, 10) == BigInt("263409560461970212832400"));

Meta