Category talk:Wren-long: Difference between revisions

→‎Source code: Added multinomial and binomial methods to ULong class.
(→‎Source code: Ensure consistency between types.)
(→‎Source code: Added multinomial and binomial methods to ULong class.)
Line 156:
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 && n <= 20)) {
Fiber.abort("First argument must be a non-negative integer <= 20.")
}
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 = 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 * factorial(e)
}
return 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 ULong.
9,476

edits