use num_bigint::BigInt;
use num_traits::{One, ToPrimitive, Zero};
use crate::core::CalcError;
pub fn perm(n: &BigInt, k: &BigInt) -> Result<BigInt, CalcError> {
if k.is_zero() {
return Ok(BigInt::one());
}
if k > n {
return Ok(BigInt::zero());
}
const MAX_PERMUTATION_K: u64 = 10000;
let k_u64 = k.to_u64().ok_or(CalcError::overflow())?;
if k_u64 > MAX_PERMUTATION_K {
return Err(CalcError::overflow());
}
let mut result = BigInt::one();
let mut current = n.clone();
for _ in 0..k_u64 {
result *= ¤t;
current -= 1;
}
Ok(result)
}
pub fn comb(n: &BigInt, k: &BigInt) -> Result<BigInt, CalcError> {
if k.is_zero() || k == n {
return Ok(BigInt::one());
}
if k > n {
return Ok(BigInt::zero());
}
let k_opt = if k < &(n - k) { k.clone() } else { n - k };
const MAX_COMBINATION_K: u64 = 10000;
let k_u64 = k_opt.to_u64().ok_or(CalcError::overflow())?;
if k_u64 > MAX_COMBINATION_K {
return Err(CalcError::overflow());
}
let mut result = BigInt::one();
let mut current = n.clone();
for i in 0..k_u64 {
result *= ¤t;
result /= BigInt::from(i + 1);
current -= 1;
}
Ok(result)
}
pub fn catalan(n: &BigInt) -> Result<BigInt, CalcError> {
if n.is_zero() {
return Ok(BigInt::one());
}
const MAX_CATALAN_N: u64 = 5000;
let n_u64 = n.to_u64().ok_or(CalcError::overflow())?;
if n_u64 > MAX_CATALAN_N {
return Err(CalcError::overflow());
}
let two_n = n * 2;
let c_2n_n = comb(&two_n, n)?;
Ok(c_2n_n / (n + 1))
}
pub fn stirling_first(n: &BigInt, k: &BigInt) -> Result<BigInt, CalcError> {
if n.is_zero() && k.is_zero() {
return Ok(BigInt::one());
}
if n.is_zero() || k.is_zero() {
return Ok(BigInt::zero());
}
if k > n {
return Ok(BigInt::zero());
}
const MAX_STIRLING_N: u64 = 5000;
let n_u64 = n.to_u64().ok_or(CalcError::overflow())?;
let k_u64 = k.to_u64().ok_or(CalcError::overflow())?;
if n_u64 > MAX_STIRLING_N || k_u64 > MAX_STIRLING_N {
return Err(CalcError::overflow());
}
let mut dp: Vec<Vec<BigInt>> =
vec![vec![BigInt::zero(); k_u64 as usize + 1]; n_u64 as usize + 1];
dp[0][0] = BigInt::one();
for i in 1..=n_u64 as usize {
for j in 1..=k_u64 as usize {
if j > i {
break;
}
dp[i][j] = BigInt::from(i - 1) * &dp[i - 1][j] + &dp[i - 1][j - 1];
}
}
Ok(dp[n_u64 as usize][k_u64 as usize].clone())
}
pub fn stirling_second(n: &BigInt, k: &BigInt) -> Result<BigInt, CalcError> {
if n.is_zero() && k.is_zero() {
return Ok(BigInt::one());
}
if n.is_zero() || k.is_zero() {
return Ok(BigInt::zero());
}
if k > n {
return Ok(BigInt::zero());
}
const MAX_STIRLING_N: u64 = 5000;
let n_u64 = n.to_u64().ok_or(CalcError::overflow())?;
let k_u64 = k.to_u64().ok_or(CalcError::overflow())?;
if n_u64 > MAX_STIRLING_N || k_u64 > MAX_STIRLING_N {
return Err(CalcError::overflow());
}
let mut dp: Vec<Vec<BigInt>> =
vec![vec![BigInt::zero(); k_u64 as usize + 1]; n_u64 as usize + 1];
dp[0][0] = BigInt::one();
for i in 1..=n_u64 as usize {
for j in 1..=k_u64 as usize {
if j > i {
break;
}
dp[i][j] = &dp[i - 1][j - 1] + &dp[i - 1][j] * BigInt::from(j);
}
}
Ok(dp[n_u64 as usize][k_u64 as usize].clone())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_perm_basic() {
assert_eq!(
perm(&BigInt::from(5), &BigInt::from(2)).unwrap(),
BigInt::from(20)
);
}
#[test]
fn test_perm_k_zero() {
assert_eq!(
perm(&BigInt::from(5), &BigInt::from(0)).unwrap(),
BigInt::from(1)
);
}
#[test]
fn test_perm_k_greater_than_n() {
assert_eq!(
perm(&BigInt::from(3), &BigInt::from(5)).unwrap(),
BigInt::from(0)
);
}
#[test]
fn test_perm_n_equals_k() {
assert_eq!(
perm(&BigInt::from(5), &BigInt::from(5)).unwrap(),
BigInt::from(120)
);
}
#[test]
fn test_comb_basic() {
assert_eq!(
comb(&BigInt::from(10), &BigInt::from(3)).unwrap(),
BigInt::from(120)
);
}
#[test]
fn test_comb_k_zero() {
assert_eq!(
comb(&BigInt::from(5), &BigInt::from(0)).unwrap(),
BigInt::from(1)
);
}
#[test]
fn test_comb_k_equals_n() {
assert_eq!(
comb(&BigInt::from(5), &BigInt::from(5)).unwrap(),
BigInt::from(1)
);
}
#[test]
fn test_comb_k_greater_than_n() {
assert_eq!(
comb(&BigInt::from(3), &BigInt::from(5)).unwrap(),
BigInt::from(0)
);
}
#[test]
fn test_comb_symmetry() {
assert_eq!(
comb(&BigInt::from(10), &BigInt::from(3)).unwrap(),
comb(&BigInt::from(10), &BigInt::from(7)).unwrap()
);
}
#[test]
fn test_catalan_zero() {
assert_eq!(catalan(&BigInt::from(0)).unwrap(), BigInt::from(1));
}
#[test]
fn test_catalan_one() {
assert_eq!(catalan(&BigInt::from(1)).unwrap(), BigInt::from(1));
}
#[test]
fn test_catalan_five() {
assert_eq!(catalan(&BigInt::from(5)).unwrap(), BigInt::from(42));
}
#[test]
fn test_catalan_sequence() {
let expected = [1, 1, 2, 5, 14, 42, 132, 429];
for (i, &e) in expected.iter().enumerate() {
assert_eq!(catalan(&BigInt::from(i as u64)).unwrap(), BigInt::from(e));
}
}
#[test]
fn test_stirling_zero_zero() {
assert_eq!(
stirling_second(&BigInt::from(0), &BigInt::from(0)).unwrap(),
BigInt::from(1)
);
}
#[test]
fn test_stirling_n_zero_k_positive() {
assert_eq!(
stirling_second(&BigInt::from(0), &BigInt::from(5)).unwrap(),
BigInt::from(0)
);
}
#[test]
fn test_stirling_k_zero_n_positive() {
assert_eq!(
stirling_second(&BigInt::from(5), &BigInt::from(0)).unwrap(),
BigInt::from(0)
);
}
#[test]
fn test_stirling_k_greater_than_n() {
assert_eq!(
stirling_second(&BigInt::from(2), &BigInt::from(5)).unwrap(),
BigInt::from(0)
);
}
#[test]
fn test_stirling_known_values() {
assert_eq!(
stirling_second(&BigInt::from(3), &BigInt::from(2)).unwrap(),
BigInt::from(3)
);
assert_eq!(
stirling_second(&BigInt::from(4), &BigInt::from(2)).unwrap(),
BigInt::from(7)
);
assert_eq!(
stirling_second(&BigInt::from(5), &BigInt::from(2)).unwrap(),
BigInt::from(15)
);
assert_eq!(
stirling_second(&BigInt::from(4), &BigInt::from(3)).unwrap(),
BigInt::from(6)
);
}
}