File tree 2 files changed +22
-0
lines changed
2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ pub mod sieve_of_eratosthenes;
39
39
mod signum;
40
40
mod simpson_integration;
41
41
mod sine;
42
+ mod square_pyramidal_numbers;
42
43
mod square_root;
43
44
mod sum_of_digits;
44
45
mod trial_division;
@@ -91,6 +92,7 @@ pub use self::sieve_of_eratosthenes::sieve_of_eratosthenes;
91
92
pub use self :: signum:: signum;
92
93
pub use self :: simpson_integration:: simpson_integration;
93
94
pub use self :: sine:: sine;
95
+ pub use self :: square_pyramidal_numbers:: square_pyramidal_number;
94
96
pub use self :: square_root:: { fast_inv_sqrt, square_root} ;
95
97
pub use self :: sum_of_digits:: { sum_digits_iterative, sum_digits_recursive} ;
96
98
pub use self :: trial_division:: trial_division;
Original file line number Diff line number Diff line change
1
+ // https://en.wikipedia.org/wiki/Square_pyramidal_number
2
+ // 1² + 2² + ... = ... (total)
3
+
4
+ pub fn square_pyramidal_number ( n : u64 ) -> u64 {
5
+ n * ( n + 1 ) * ( 2 * n + 1 ) / 6
6
+ }
7
+
8
+ #[ cfg( test) ]
9
+ mod tests {
10
+
11
+ use super :: * ;
12
+
13
+ #[ test]
14
+ fn test0 ( ) {
15
+ assert_eq ! ( 0 , square_pyramidal_number( 0 ) ) ;
16
+ assert_eq ! ( 1 , square_pyramidal_number( 1 ) ) ;
17
+ assert_eq ! ( 5 , square_pyramidal_number( 2 ) ) ;
18
+ assert_eq ! ( 14 , square_pyramidal_number( 3 ) ) ;
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments