diff --git a/DIRECTORY.md b/DIRECTORY.md index 564a7813807..35187d29652 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -240,9 +240,11 @@ * [Quadratic Residue](https://github.com/TheAlgorithms/Rust/blob/master/src/math/quadratic_residue.rs) * [Random](https://github.com/TheAlgorithms/Rust/blob/master/src/math/random.rs) * [Relu](https://github.com/TheAlgorithms/Rust/blob/master/src/math/relu.rs) + * [Rising Factorial](https://github.com/TheAlgorithms/Rust/blob/master/rising_factorial.rs) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sieve_of_eratosthenes.rs) * [Sigmoid](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sigmoid.rs) * [Signum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/signum.rs) + * [Simplicial Polytopic Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simplicial_polytopic_number) * [Simpsons Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpsons_integration.rs) * [Softmax](https://github.com/TheAlgorithms/Rust/blob/master/src/math/softmax.rs) * [Sprague Grundy Theorem](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sprague_grundy_theorem.rs) diff --git a/src/math/mod.rs b/src/math/mod.rs index 7407465c3b0..c055845af48 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -63,9 +63,11 @@ mod prime_numbers; mod quadratic_residue; mod random; mod relu; +mod rising_factorial; mod sieve_of_eratosthenes; mod sigmoid; mod signum; +mod simplicial_polytopic_number; mod simpsons_integration; mod softmax; mod sprague_grundy_theorem; @@ -155,9 +157,11 @@ pub use self::prime_numbers::prime_numbers; pub use self::quadratic_residue::{cipolla, tonelli_shanks}; pub use self::random::PCG32; pub use self::relu::relu; +pub use self::rising_factorial::rising_factorial; pub use self::sieve_of_eratosthenes::sieve_of_eratosthenes; pub use self::sigmoid::sigmoid; pub use self::signum::signum; +pub use self::simplicial_polytopic_number::simplicial_polytopic_number; pub use self::simpsons_integration::simpsons_integration; pub use self::softmax::softmax; pub use self::sprague_grundy_theorem::calculate_grundy_number; diff --git a/src/math/rising_factorial.rs b/src/math/rising_factorial.rs new file mode 100644 index 00000000000..0dacc795979 --- /dev/null +++ b/src/math/rising_factorial.rs @@ -0,0 +1,33 @@ +use std::ops::Add; +use std::ops::Mul; +// Rising factorials are defined as the polynomial +// (x)_n = x(x + 1)(x + 2) ... (x + n - 1). +// +// For further reading: +// https://mathworld.wolfram.com/RisingFactorial.html +// https://en.wikipedia.org/wiki/Falling_and_rising_factorials +// +// Returns the nth rising factorial. +pub fn rising_factorial(x: u64, n: u64) -> u64 { + let mut result = x; + for i in 1..n { + let next_term = x.add(i); + result = result.mul(next_term); + } + result +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_rising_factorial() { + assert_eq!(rising_factorial(1, 0), 1); + assert_eq!(rising_factorial(1, 1), 1); + assert_eq!(rising_factorial(2, 1), 2); + assert_eq!(rising_factorial(1, 2), 2); // 1 * 2 + assert_eq!(rising_factorial(2, 2), 6); // 2 * 3 + assert_eq!(rising_factorial(3, 3), 60); // 3 * 4 * 5 + } +} diff --git a/src/math/simplicial_polytopic_number.rs b/src/math/simplicial_polytopic_number.rs new file mode 100644 index 00000000000..1348dc85970 --- /dev/null +++ b/src/math/simplicial_polytopic_number.rs @@ -0,0 +1,48 @@ +use crate::math::factorial; +use crate::math::rising_factorial; + +use std::ops::Div; + +// Simplicial polytopic numbers are a family of sequences of figurate +// numbers corresponding to the d-dimensional simplex for each +// dimensional simplex for each dimension d, where d is a nonnegative +// integer +// +// For further reading: +// https://oeis.org/wiki/Simplicial_polytopic_numbers +// https://en.wikipedia.org/wiki/Triangular_number +// +// This program returns the simplicial polytopic number for any +// d-dimensional simplex. For example (2, 2) would return the +// second triangular number 3. +pub fn simplicial_polytopic_number(n: u64, d: u64) -> u64 { + rising_factorial(n, d).div(factorial(d)) +} + +#[cfg(test)] +mod tests { + use super::*; + + macro_rules! test_simplicial_polytopic_number { + ($($name:ident: $inputs:expr,)*) => { + $( + #[test] + fn $name() { + let ((n, d), expected) =$inputs; + assert_eq!(simplicial_polytopic_number(n, d), expected); + } + )* + } + } + + test_simplicial_polytopic_number! { + input_0: ((1, 1), 1), + input_1: ((2, 1), 2), + input_2: ((2, 2), 3), + input_3: ((3, 3), 10), + input_4: ((2, 4), 5), + input_5: ((5, 5), 126), + input_6: ((9, 6), 3003), + input_7: ((6, 10), 3003), + } +}