Skip to content

Commit 9a57953

Browse files
author
WinterPancake
committed
Added tetrahedral_number.rs
1 parent 99e33d1 commit 9a57953

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
* [Sum Of Harmonic Series](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_harmonic_series.rs)
254254
* [Sylvester Sequence](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sylvester_sequence.rs)
255255
* [Tanh](https://github.com/TheAlgorithms/Rust/blob/master/src/math/tanh.rs)
256+
* [Tetrahedral Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/tetrahedral_number.rs)
256257
* [Trapezoidal Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trapezoidal_integration.rs)
257258
* [Trial Division](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trial_division.rs)
258259
* [Trig Functions](https://github.com/TheAlgorithms/Rust/blob/master/src/math/trig_functions.rs)

src/math/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ mod sum_of_geometric_progression;
7676
mod sum_of_harmonic_series;
7777
mod sylvester_sequence;
7878
mod tanh;
79+
mod tetrahedral_number;
7980
mod trapezoidal_integration;
8081
mod trial_division;
8182
mod trig_functions;
@@ -168,6 +169,7 @@ pub use self::sum_of_geometric_progression::sum_of_geometric_progression;
168169
pub use self::sum_of_harmonic_series::sum_of_harmonic_progression;
169170
pub use self::sylvester_sequence::sylvester;
170171
pub use self::tanh::tanh;
172+
pub use self::tetrahedral_number::tetrahedral_number;
171173
pub use self::trapezoidal_integration::trapezoidal_integral;
172174
pub use self::trial_division::trial_division;
173175
pub use self::trig_functions::cosine;

src/math/tetrahedral_number.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Tetrahedral Numbers: Function to the Nth Tetrahedral Number
2+
// Wikipedia Reference : https://en.wikipedia.org/wiki/Tetrahedral_number
3+
use num_bigint::BigUint;
4+
pub fn tetrahedral_number(n: u64) -> BigUint {
5+
let m: BigUint = (((n) * (n + 1) * (n + 2)) / 6).into();
6+
m
7+
}
8+
9+
#[cfg(test)]
10+
mod tests {
11+
use super::*;
12+
13+
macro_rules! test_tetrahedral_number {
14+
($($name:ident: $inputs:expr,)*) => {
15+
$(
16+
#[test]
17+
fn $name() {
18+
let (n, expected) =$inputs;
19+
assert_eq!(tetrahedral_number(n), expected);
20+
}
21+
)*
22+
}
23+
}
24+
25+
test_tetrahedral_number! {
26+
input_6: (6, BigUint::from(56u32)),
27+
input_8: (8, BigUint::from(120u32)),
28+
input_34: (34, BigUint::from(7140u32)),
29+
}
30+
}

0 commit comments

Comments
 (0)