File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -78,6 +78,7 @@ mod sylvester_sequence;
7878mod tanh;
7979mod trapezoidal_integration;
8080mod trial_division;
81+ mod triangular_number;
8182mod trig_functions;
8283mod vector_cross_product;
8384mod zellers_congruence_algorithm;
@@ -170,6 +171,7 @@ pub use self::sylvester_sequence::sylvester;
170171pub use self :: tanh:: tanh;
171172pub use self :: trapezoidal_integration:: trapezoidal_integral;
172173pub use self :: trial_division:: trial_division;
174+ pub use self :: triangular_number:: triangular_number;
173175pub use self :: trig_functions:: cosine;
174176pub use self :: trig_functions:: cosine_no_radian_arg;
175177pub use self :: trig_functions:: cotan;
Original file line number Diff line number Diff line change 1+ // Triangular Numbers: Function to the Nth Triangular Number
2+ // Wikipedia Reference : https://en.wikipedia.org/wiki/Triangular_number
3+
4+ pub fn triangular_number ( n : u64 ) -> u64 {
5+ let m: u64 = ( n | 1 ) * ( ( n + 1 ) / 2 ) ;
6+ return m;
7+ }
8+
9+ #[ cfg( test) ]
10+ mod tests {
11+ use super :: * ;
12+
13+ macro_rules! test_triangular_number {
14+ ( $( $name: ident: $inputs: expr, ) * ) => {
15+ $(
16+ #[ test]
17+ fn $name( ) {
18+ let ( n, expected) =$inputs;
19+ assert_eq!( triangular_number( n) , expected) ;
20+ }
21+ ) *
22+ }
23+ }
24+
25+ test_triangular_number ! {
26+ input_0: ( 1 , 1 ) ,
27+ input_1: ( 2 , 3 ) ,
28+ input_2: ( 3 , 6 ) ,
29+ input_3: ( 4 , 10 ) ,
30+ input_4: ( 5 , 15 ) ,
31+ input_5: ( 6 , 21 ) ,
32+ input_6: ( 7 , 28 ) ,
33+ input_7: ( 8 , 36 ) ,
34+ input_8: ( 9 , 45 ) ,
35+ input_9: ( 10 , 55 ) ,
36+ input_10: ( 11 , 66 ) ,
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments