calculateFalling static method
Calculates the falling factorial of x to the power of n.
The falling factorial, denoted by (x)n or P(x, n), is defined as the product of n terms, starting with x and each successive term decreasing by 1. It is equivalent to the number of permutations of n items chosen from x.
Definition: (x)n = x * (x-1) * (x-2) * ... * (x-n+1)
- If n = 0, (x)0 = 1 (this is an empty product).
- If n > x and x is a non-negative integer, (x)n = 0.
(This specific implementation will naturally return 0 if x becomes 0
or if a factor of 0 is encountered. For stricter adherence to the
permutation definition, an explicit check
if (n > x && x >= 0) return BigInt.zero;could be added).
Parameters:
x: The starting number (can be any integer).n: The number of terms to multiply (must be a non-negative integer).
Returns:
A BigInt representing the falling factorial of x to n.
Throws:
- ArgumentError: If
nis a negative number.
Implementation
static BigInt calculateFalling(int x, int n) {
if (n < 0) {
throw ArgumentError('Falling factorial is not defined for negative n.');
}
if (n == 0) return BigInt.one;
// Optional: For stricter permutation definition (P(x,n) = 0 if n > x and x >= 0)
// if (n > x && x >= 0) {
// return BigInt.zero;
// }
var result = BigInt.one;
for (int i = 0; i < n; i++) {
result *= BigInt.from(x - i);
}
return result;
}