Class Combinatorics
Provides binomial coefficients, permutations, Catalan numbers, Stirling numbers of the second kind, Bell numbers, and derangements. All methods are stateless and thread-safe.
提供二项式系数、排列数、Catalan 数、第二类 Stirling 数、Bell 数和错排数。 所有方法无状态且线程安全。
- Since:
- JDK 25, opencode-base-math V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic longbellNumber(int n) Computes the n-th Bell number (sum of Stirling numbers of the second kind).static longbinomial(int n, int k) Computes the binomial coefficient C(n, k) = n!static BigIntegerbinomialBig(int n, int k) Computes the binomial coefficient C(n, k) with arbitrary precision.static BigIntegercatalanBig(int n) Computes the n-th Catalan number with arbitrary precision.static longcatalanNumber(int n) Computes the n-th Catalan number: C(2n, n) / (n + 1).static longderangements(int n) Computes the number of derangements (subfactorial) !static longpermutation(int n, int k) Computes the permutation P(n, k) = n!static BigIntegerpermutationBig(int n, int k) Computes the permutation P(n, k) with arbitrary precision.static longstirlingSecond(int n, int k) Computes the Stirling number of the second kind S(n, k).
-
Method Details
-
binomial
public static long binomial(int n, int k) Computes the binomial coefficient C(n, k) = n! / (k! * (n-k)!). 计算二项式系数 C(n, k) = n! / (k! * (n-k)!)Uses the multiplicative formula to avoid intermediate overflow where possible. Throws
MathExceptionif the result overflowslong.使用乘法公式尽量避免中间溢出。如果结果超出
long范围则抛出MathException。- Parameters:
n- total elements, must be ≥ 0 / 总元素数,必须 ≥ 0k- chosen elements, must satisfy 0 ≤ k ≤ n / 选择元素数,必须满足 0 ≤ k ≤ n- Returns:
- C(n, k) / 二项式系数
- Throws:
IllegalArgumentException- if n < 0, k < 0, or k > nMathException- if result overflows long / 如果结果溢出 long
-
binomialBig
Computes the binomial coefficient C(n, k) with arbitrary precision. 使用任意精度计算二项式系数 C(n, k)- Parameters:
n- total elements, must be ≥ 0 / 总元素数,必须 ≥ 0k- chosen elements, must satisfy 0 ≤ k ≤ n / 选择元素数,必须满足 0 ≤ k ≤ n- Returns:
- C(n, k) as BigInteger / BigInteger 形式的二项式系数
- Throws:
IllegalArgumentException- if n < 0, k < 0, or k > n
-
permutation
public static long permutation(int n, int k) Computes the permutation P(n, k) = n! / (n-k)!. 计算排列数 P(n, k) = n! / (n-k)!- Parameters:
n- total elements, must be ≥ 0 / 总元素数,必须 ≥ 0k- chosen elements, must satisfy 0 ≤ k ≤ n / 选择元素数,必须满足 0 ≤ k ≤ n- Returns:
- P(n, k) / 排列数
- Throws:
IllegalArgumentException- if n < 0, k < 0, or k > nMathException- if result overflows long / 如果结果溢出 long
-
permutationBig
Computes the permutation P(n, k) with arbitrary precision. 使用任意精度计算排列数 P(n, k)- Parameters:
n- total elements, must be ≥ 0 / 总元素数,必须 ≥ 0k- chosen elements, must satisfy 0 ≤ k ≤ n / 选择元素数,必须满足 0 ≤ k ≤ n- Returns:
- P(n, k) as BigInteger / BigInteger 形式的排列数
- Throws:
IllegalArgumentException- if n < 0, k < 0, or k > n
-
catalanNumber
public static long catalanNumber(int n) Computes the n-th Catalan number: C(2n, n) / (n + 1). 计算第 n 个 Catalan 数:C(2n, n) / (n + 1)- Parameters:
n- the index, must be ≥ 0 / 索引,必须 ≥ 0- Returns:
- the n-th Catalan number / 第 n 个 Catalan 数
- Throws:
IllegalArgumentException- if n < 0MathException- if result overflows long / 如果结果溢出 long
-
catalanBig
Computes the n-th Catalan number with arbitrary precision. 使用任意精度计算第 n 个 Catalan 数- Parameters:
n- the index, must be ≥ 0 / 索引,必须 ≥ 0- Returns:
- the n-th Catalan number as BigInteger / BigInteger 形式的 Catalan 数
- Throws:
IllegalArgumentException- if n < 0
-
stirlingSecond
public static long stirlingSecond(int n, int k) Computes the Stirling number of the second kind S(n, k). 计算第二类 Stirling 数 S(n, k)Uses the recurrence S(n, k) = k * S(n-1, k) + S(n-1, k-1), with base cases S(n, 0) = 0 (n > 0), S(0, 0) = 1, S(n, n) = 1.
使用递推关系 S(n, k) = k * S(n-1, k) + S(n-1, k-1), 边界条件 S(n, 0) = 0 (n > 0),S(0, 0) = 1,S(n, n) = 1。
- Parameters:
n- number of elements, must be ≥ 0 / 元素数,必须 ≥ 0k- number of non-empty subsets, must satisfy 0 ≤ k ≤ n / 非空子集数,必须满足 0 ≤ k ≤ n- Returns:
- S(n, k) / 第二类 Stirling 数
- Throws:
IllegalArgumentException- if n < 0, k < 0, or k > n
-
bellNumber
public static long bellNumber(int n) Computes the n-th Bell number (sum of Stirling numbers of the second kind). 计算第 n 个 Bell 数(第二类 Stirling 数之和)B(n) = sum_{k=0}^{n} S(n, k).
- Parameters:
n- the index, must be ≥ 0 / 索引,必须 ≥ 0- Returns:
- the n-th Bell number / 第 n 个 Bell 数
- Throws:
IllegalArgumentException- if n < 0MathException- if result overflows long / 如果结果溢出 long
-
derangements
public static long derangements(int n) Computes the number of derangements (subfactorial) !n. 计算错排数(子阶乘)!nA derangement is a permutation where no element appears in its original position. Uses the recurrence !n = (n-1) * (!(n-1) + !(n-2)).
错排是指没有元素出现在其原始位置的排列。 使用递推关系 !n = (n-1) * (!(n-1) + !(n-2))。
- Parameters:
n- the number of elements, must be ≥ 0 / 元素数量,必须 ≥ 0- Returns:
- !n (the number of derangements) / 错排数
- Throws:
IllegalArgumentException- if n < 0MathException- if result overflows long / 如果结果溢出 long
-