Category talk:Wren-big: Difference between revisions
Content added Content deleted
(→Source code: Ensure consistency between types.) |
(→Source code: Added multinomial and binomial methods to BigInt.) |
||
Line 687: | Line 687: | ||
return fact |
return fact |
||
} |
} |
||
// Returns the multinomial coefficient of n over a list f where sum(f) == n. |
|||
static multinomial(n, f) { |
|||
if (!(n is Num && n >= 0)) Fiber.abort("First argument must be a non-negative integer") |
|||
if (!(f is List)) Fiber.abort("Second argument must be a list.") |
|||
var sum = f.reduce { |acc, i| acc + i } |
|||
if (n != sum) { |
|||
Fiber.abort("The elements of the list must sum to 'n'.") |
|||
} |
|||
var prod = BigInt.one |
|||
for (e in f) { |
|||
if (e < 0) Fiber.abort("The elements of the list must be non-negative integers.") |
|||
if (e > 1) prod = prod * BigInt.factorial(e) |
|||
} |
|||
return BigInt.factorial(n)/prod |
|||
} |
|||
// Returns the binomial coefficent of n over k. |
|||
static binomial(n, k) { multinomial(n, [k, n-k]) } |
|||
// Returns whether or not 'n' is an instance of BigInt. |
// Returns whether or not 'n' is an instance of BigInt. |