Skip to content

Commit 2da705f

Browse files
author
WinterPancake
committed
Added simplicial_polytopic_number.rs and rising_factorial.rs to /src/math
1 parent 99e33d1 commit 2da705f

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,11 @@
240240
* [Quadratic Residue](https://github.com/TheAlgorithms/Rust/blob/master/src/math/quadratic_residue.rs)
241241
* [Random](https://github.com/TheAlgorithms/Rust/blob/master/src/math/random.rs)
242242
* [Relu](https://github.com/TheAlgorithms/Rust/blob/master/src/math/relu.rs)
243+
* [Rising Factorial](https://github.com/TheAlgorithms/Rust/blob/master/rising_factorial.rs)
243244
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sieve_of_eratosthenes.rs)
244245
* [Sigmoid](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sigmoid.rs)
245246
* [Signum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/signum.rs)
247+
* [Simplicial Polytopic Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simplicial_polytopic_number)
246248
* [Simpsons Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpsons_integration.rs)
247249
* [Softmax](https://github.com/TheAlgorithms/Rust/blob/master/src/math/softmax.rs)
248250
* [Sprague Grundy Theorem](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sprague_grundy_theorem.rs)

src/math/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ mod prime_numbers;
6363
mod quadratic_residue;
6464
mod random;
6565
mod relu;
66+
mod rising_factorial;
6667
mod sieve_of_eratosthenes;
6768
mod sigmoid;
6869
mod signum;
70+
mod simplicial_polytopic_number;
6971
mod simpsons_integration;
7072
mod softmax;
7173
mod sprague_grundy_theorem;
@@ -155,9 +157,11 @@ pub use self::prime_numbers::prime_numbers;
155157
pub use self::quadratic_residue::{cipolla, tonelli_shanks};
156158
pub use self::random::PCG32;
157159
pub use self::relu::relu;
160+
pub use self::rising_factorial::rising_factorial;
158161
pub use self::sieve_of_eratosthenes::sieve_of_eratosthenes;
159162
pub use self::sigmoid::sigmoid;
160163
pub use self::signum::signum;
164+
pub use self::simplicial_polytopic_number::simplicial_polytopic_number;
161165
pub use self::simpsons_integration::simpsons_integration;
162166
pub use self::softmax::softmax;
163167
pub use self::sprague_grundy_theorem::calculate_grundy_number;

src/math/rising_factorial.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::ops::Add;
2+
use std::ops::Mul;
3+
// Rising factorials are defined as the polynomial
4+
// (x)_n = x(x + 1)(x + 2) ... (x + n - 1).
5+
//
6+
// For further reading:
7+
// https://mathworld.wolfram.com/RisingFactorial.html
8+
// https://en.wikipedia.org/wiki/Falling_and_rising_factorials
9+
//
10+
// Returns the nth rising factorial.
11+
pub fn rising_factorial(x: u64, n: u64) -> u64 {
12+
let mut result = x;
13+
for i in 1..n {
14+
let next_term = x.add(i);
15+
result = result.mul(next_term);
16+
}
17+
result
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use super::*;
23+
24+
#[test]
25+
fn test_rising_factorial() {
26+
assert_eq!(rising_factorial(1, 0), 1);
27+
assert_eq!(rising_factorial(1, 1), 1);
28+
assert_eq!(rising_factorial(2, 1), 2);
29+
assert_eq!(rising_factorial(1, 2), 2); // 1 * 2
30+
assert_eq!(rising_factorial(2, 2), 6); // 2 * 3
31+
assert_eq!(rising_factorial(3, 3), 60); // 3 * 4 * 5
32+
}
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::math::factorial;
2+
use crate::math::rising_factorial;
3+
4+
use std::ops::Div;
5+
6+
// Simplicial polytopic numbers are a family of sequences of figurate
7+
// numbers corresponding to the d-dimensional simplex for each
8+
// dimensional simplex for each dimension d, where d is a nonnegative
9+
// integer
10+
//
11+
// For further reading:
12+
// https://oeis.org/wiki/Simplicial_polytopic_numbers
13+
// https://en.wikipedia.org/wiki/Triangular_number
14+
//
15+
// This program returns the simplicial polytopic number for any
16+
// d-dimensional simplex. For example (2, 2) would return the
17+
// second triangular number 3.
18+
pub fn simplicial_polytopic_number(n: u64, d: u64) -> u64 {
19+
rising_factorial(n, d).div(factorial(d))
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
macro_rules! test_simplicial_polytopic_number {
27+
($($name:ident: $inputs:expr,)*) => {
28+
$(
29+
#[test]
30+
fn $name() {
31+
let ((n, d), expected) =$inputs;
32+
assert_eq!(simplicial_polytopic_number(n, d), expected);
33+
}
34+
)*
35+
}
36+
}
37+
38+
test_simplicial_polytopic_number! {
39+
input_0: ((1, 1), 1),
40+
input_1: ((2, 1), 2),
41+
input_2: ((2, 2), 3),
42+
input_3: ((3, 3), 10),
43+
input_4: ((2, 4), 5),
44+
input_5: ((5, 5), 126),
45+
input_6: ((9, 6), 3003),
46+
input_7: ((6, 10), 3003),
47+
}
48+
}

0 commit comments

Comments
 (0)