From 272b7360f50f1283def4a3681223804f2a5e0439 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 4 Mar 2020 16:53:31 -0500 Subject: [PATCH] Add implementation of sterlings approximation --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5d7c946..46f9c62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { } } +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 {