Line | |
---|
1 | // ========================================================= // |
---|
2 | // // |
---|
3 | // File : triangular.h // |
---|
4 | // Purpose : provide sums of small powers // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in Nov 21 // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // ========================================================= // |
---|
10 | |
---|
11 | #ifndef TRIANGULAR_H |
---|
12 | #define TRIANGULAR_H |
---|
13 | |
---|
14 | #ifndef CXXFORWARD_H |
---|
15 | #include "cxxforward.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | CONSTEXPR_INLINE long triangular_number(const long N) { |
---|
19 | // in german aka "Gausssche Summenformel" |
---|
20 | // SUM(k=[1..N]; k) |
---|
21 | return N*(N+1)/2; |
---|
22 | } |
---|
23 | |
---|
24 | CONSTEXPR_INLINE long squarepyramidal_number(const long N) { |
---|
25 | // in german aka "Quadratische Pyramidalzahl" |
---|
26 | // SUM(k=[1..N]; k^2) |
---|
27 | return N*(N+1)*(2*N+1)/6; |
---|
28 | } |
---|
29 | |
---|
30 | CONSTEXPR_INLINE long sum_of_triangular_numbers(const long L) { |
---|
31 | // SUM(N=[1..L]; SUM(k=[1..N]; k)) |
---|
32 | return (squarepyramidal_number(L) + triangular_number(L)) / 2; |
---|
33 | } |
---|
34 | |
---|
35 | #else |
---|
36 | #error triangular.h included twice |
---|
37 | #endif // TRIANGULAR_H |
---|
Note: See
TracBrowser
for help on using the repository browser.