Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
* [Sum Of Harmonic Series](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_harmonic_series.rs)
* [Sylvester Sequence](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sylvester_sequence.rs)
* [Tanh](https://github.com/TheAlgorithms/Rust/blob/master/src/math/tanh.rs)
* [Tetrahedral Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/tetrahedral_number.rs)
* [Trapezoidal Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trapezoidal_integration.rs)
* [Trial Division](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trial_division.rs)
* [Trig Functions](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trig_functions.rs)
Expand Down
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ mod sum_of_geometric_progression;
mod sum_of_harmonic_series;
mod sylvester_sequence;
mod tanh;
mod tetrahedral_number;
mod trapezoidal_integration;
mod trial_division;
mod trig_functions;
Expand Down Expand Up @@ -168,6 +169,7 @@ pub use self::sum_of_geometric_progression::sum_of_geometric_progression;
pub use self::sum_of_harmonic_series::sum_of_harmonic_progression;
pub use self::sylvester_sequence::sylvester;
pub use self::tanh::tanh;
pub use self::tetrahedral_number::tetrahedral_number;
pub use self::trapezoidal_integration::trapezoidal_integral;
pub use self::trial_division::trial_division;
pub use self::trig_functions::cosine;
Expand Down
36 changes: 36 additions & 0 deletions src/math/tetrahedral_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Tetrahedral Numbers: Function to the Nth Tetrahedral Number
// Wikipedia Reference : https://en.wikipedia.org/wiki/Tetrahedral_number
//
// This program provides a function to calculate the nth triangular
// number defined by T_n = 1 + 2 + ... + n = (n(n + 1)(n + 2))/6 =
// (n + 2) choose 3.

pub fn tetrahedral_number(n: u64) -> u64 {
((n) * (n + 1) * (n + 2)) / 6
}

#[cfg(test)]
mod tests {
use super::*;

macro_rules! test_tetrahedral_number {
($($name:ident: $inputs:expr,)*) => {
$(
#[test]
fn $name() {
let (n, expected) =$inputs;
assert_eq!(tetrahedral_number(n), expected);
}
)*
}
}

test_tetrahedral_number! {
input_0: (0, 0),
input_1: (1, 1),
input_2: (6, 56),
input_3: (8, 120),
input_4: (11, 286),
input_5: (34, 7140),
}
}