C++ Library Extensions 2022.12.09
To help learn modern C++ programming
044-functional.cpp
Go to the documentation of this file.
1#include <iostream>
2
3auto& cout = std::cout;
4auto endl = "\n";
5auto endL = "\n\n";
6
7/*
8
9 For your convenience, this is Pascal's Triangle formula
10
11 n C r = n-1 C r-1 + n-1 C r
12
13 */
14
15// N and R are constant expression or compile-time expressions
16// we cannot modify the values of N and R, as in functional programming paradigm
17// once the values of N and R are initialized, we cannot change their states.
18//
19// Functional Programming is based on Recursion.
20
21// this is primary template.
22// primary template determines the number of template parameters.
23template<size_t N, size_t R>
25{
26 // in functional programming paradigm, once we initialize an object
27 // we cannot modify its state. So, keyword constexpr makes value immutable.
28 constexpr static size_t value = pascal_triangle< N-1, R-1 >::value // n-1 C r-1
29 + // +
30 pascal_triangle< N-1, R >::value; // n-1 C r
31};
32
33template<size_t N>
34struct pascal_triangle<N, N> // nCn = 1
35{
36 constexpr static size_t value = 1;
37};
38
39template<size_t N>
40struct pascal_triangle<N, 0> // nC0 = 1
41{
42 constexpr static size_t value = 1;
43};
44
45template<size_t N>
46struct pascal_triangle<N, 1> // nC1 = N
47{
48 constexpr static size_t value = N;
49};
50
51template<size_t N, size_t R>
53
55{
56 cout << "5 C 0 = " << pascal<5, 0> << endl;
57 cout << "5 C 1 = " << pascal<5, 1> << endl;
58 cout << "5 C 2 = " << pascal<5, 2> << endl;
59 cout << "5 C 3 = " << pascal<5, 3> << endl;
60 cout << "5 C 4 = " << pascal<5, 4> << endl;
61 cout << "5 C 5 = " << pascal<5, 5> << endl;
62}
63
64template<size_t Start_R, size_t End_R>
65struct static_loop
66{
67 template<size_t N>
69 {
70 if constexpr(Start_R <= End_R)
71 {
72 cout << N << " C " << Start_R << " = "
73 << pascal<N, Start_R> << endl;
74 }
75
76 if constexpr(Start_R + 1 <= End_R)
77 {
78 // we are calling print_pascal_triangle<N>()
79 // recursively
81 template print_pascal_triangle<N>();
82 }
83 }
84
85};
86
87template<size_t N>
89{
91 template print_pascal_triangle<N>();
92}
93
94/*
95 Fibonacci numbers are defined as below
96
97 f0 = 0
98 f1 = 1;
99
100 if n > 1,
101
102 f(n) = f(n-2) + f(n-1)
103
104 pascal's tree
105 C(n, r) = C(n-1, r-1) + C(n-1, r)
106 */
107int main()
108{
109 // test_pascal_triangle();
110
111 cout << "Combinations with 4 elements " << endL;
112
113 print_pascals<4>();
114
115 cout << "Combinations with 5 elements " << endL;
116
117 print_pascals<5>();
118
119 cout << "Combinations with 6 elements " << endL;
120
121 print_pascals<6>();
122
123 cout << "Combinations with 7 elements " << endL;
124
125 print_pascals<7>();
126
127 cout << "Combinations with 8 elements " << endL;
128
129 print_pascals<8>();
130
131}
132
constexpr size_t pascal
auto endL
auto & cout
void test_pascal_triangle()
auto endl
int main()
void print_pascals()
static constexpr size_t value
static void print_pascal_triangle()