Skip to main content

partition_count

Function partition_count 

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

Number of integer partitions of n.

An integer partition of n is a way to write n as a sum of positive integers (order doesn’t matter). For example, p(5) = 7 because 5 = 5 = 4+1 = 3+2 = 3+1+1 = 2+2+1 = 2+1+1+1 = 1+1+1+1+1.

Uses Euler’s pentagonal number theorem recurrence: p(n) = Σ_{k≠0} (-1)^{k+1} p(n − k(3k−1)/2) with p(0) = 1 and p(n) = 0 for n < 0.

Returns Some(0) for negative inputs, Some(1) for n = 0. Returns None if n doesn’t fit in usize.

§Examples

use symplex::combinatorics::partition_count;
use num_bigint::BigInt;

assert_eq!(partition_count(0), Some(BigInt::from(1)));
assert_eq!(partition_count(5), Some(BigInt::from(7)));
assert_eq!(partition_count(10), Some(BigInt::from(42)));