Add implementation of sterlings approximation
timw4mail/rusty-numbers/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2020-03-04 16:53:31 -05:00
parent 94f39fff07
commit 272b7360f5
1 changed files with 10 additions and 0 deletions

View File

@ -3,6 +3,9 @@
//! Playin' with Numerics in Rust
#![forbid(unsafe_code)]
use std::f64::consts::PI;
use std::f64::consts::E;
#[cfg_attr(tarpaulin, skip)]
pub mod bigint;
pub mod num;
@ -173,6 +176,13 @@ pub fn factorial(n: usize) -> Option<u128> {
}
}
pub fn approx_factorial(n: f64) -> f64 {
let power = (n / E).powf(n);
let root = (PI * 2.0 * n).sqrt();
power * root
}
#[cfg(test)]
#[cfg_attr(tarpaulin, skip)]
mod tests {