Skip to content

Commit 29403ed

Browse files
author
WinterPancake
committed
Added comments explaining what the module provides. Removed BigUint for being overkill. Removed the use and returning of the variable m. Renamed the names of the test cases to something more logical.
1 parent 5d94423 commit 29403ed

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src/math/triangular_number.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Triangular Numbers: Function to the Nth Triangular Number
22
// Wikipedia Reference : https://en.wikipedia.org/wiki/Triangular_number
3-
use num_bigint::BigUint;
4-
pub fn triangular_number(n: u64) -> BigUint {
5-
let m: BigUint = ((n | 1) * ((n + 1) / 2)).into();
6-
m
3+
4+
// This program provides a function to calculate the nth triangular number defined by T_n = 1 + 2 +
5+
// ... + n = (n^2 + 2)/ = n(n + 1)/2 = (n + 1) choose 2.
6+
7+
//returns the nth triangular number
8+
pub fn triangular_number(n: u64) -> u64 {
9+
(n | 1) * ((n + 1) / 2)
710
}
811

912
#[cfg(test)]
@@ -23,8 +26,11 @@ mod tests {
2326
}
2427

2528
test_triangular_number! {
26-
input_5: (6, BigUint::from(21u32)),
27-
input_9: (10, BigUint::from(55u32)),
28-
input_10: (100, BigUint::from(5050u32)),
29+
input_0: (0, 0),
30+
input_1: (1, 1),
31+
input_2: (6, 21),
32+
input_3: (10, 55),
33+
input_4: (21, 231),
34+
input_5: (100, 5050),
2935
}
3036
}

0 commit comments

Comments
 (0)