partition_p

Function partition_p 

Source
pub fn partition_p<T>(n: T) -> T
where T: PrimInt + ConstZero + ConstOne,
Expand description

Partition function.

Partition function $p$ is defined as the number of ways an integer can be written as a sum of positive integers. These sums are called partitions of the integer. It is calculated using the recurrence relation: $$ p(n) = \begin{cases} 0 & \text{if}\quad n < 0 \\ 1 & \text{if}\quad n = 0 \\ \sum_{k=1}^n (-1)^{k+1} \cdot (p(n - \frac{k \cdot (3k - 1)}{2}) + p(n - \frac{k \cdot (3k + 1)}{2})) & \text{if}\quad n > 0 \end{cases} $$

§Arguments

  • n - The integer to find the number of partitions of.

§Returns

  • The number of partitions of the integer.

§Panics

  • If n is too large to fit in a usize.

§Example

use pmath::partition_p;

// Partitions of 5:
// {5}
// {4, 1}
// {3, 2}
// {3, 1, 1}
// {2, 2, 1}
// {2, 1, 1, 1}
// {1, 1, 1, 1, 1}
// p(5) = 7
assert_eq!(partition_p(5), 7);