Skip to main content

stirling2

Function stirling2 

Source
pub fn stirling2(n: impl Into<BigInt>, k: impl Into<BigInt>) -> Option<BigInt>
Expand description

Stirling number of the second kind S(n, k).

Counts the number of ways to partition a set of n elements into exactly k non-empty subsets.

Uses the recurrence S(n, k) = k·S(n−1, k) + S(n−1, k−1) with base cases S(0, 0) = 1 and S(n, 0) = S(0, k) = 0 for n, k > 0.

Returns Some(0) for negative inputs or when k > n. Returns None if the inputs don’t fit in u64.

§Examples

use symplex::combinatorics::stirling2;
use num_bigint::BigInt;

assert_eq!(stirling2(0, 0), Some(BigInt::from(1)));
assert_eq!(stirling2(4, 2), Some(BigInt::from(7)));
assert_eq!(stirling2(5, 3), Some(BigInt::from(25)));