|
1 |
| -/** Print all the Catalan numbers from 0 to n, n being the user input. |
2 |
| -
|
3 |
| - * A Catalan number satifies the following two properties: |
| 1 | +/** |
| 2 | + * A Catalan number satisfies the following two properties: |
4 | 3 | * C(0) = C(1) = 1; C(n) = sum(C(i).C(n-i-1)), from i = 0 to n-1
|
5 | 4 | * Read more about Catalan numbers here:
|
6 | 5 | https://en.wikipedia.org/wiki/Catalan_number
|
7 | 6 | */
|
8 | 7 |
|
9 |
| -#include <iostream> |
10 |
| -using namespace std; |
11 |
| - |
12 |
| -int *cat; // global array to hold catalan numbers |
13 |
| - |
14 |
| -unsigned long int catalan_dp(int n) { |
15 |
| - /** Using the tabulation technique in dynamic programming, |
16 |
| - this function computes the first `n+1` Catalan numbers |
17 |
| -
|
18 |
| - Parameter |
19 |
| - --------- |
20 |
| - n: The number of catalan numbers to be computed. |
| 8 | +#include <cassert> /// for assert |
| 9 | +#include <cstdint> /// for std::uint64_t |
| 10 | +#include <cstdlib> /// for std::size_t |
| 11 | +#include <vector> /// for std::vector |
21 | 12 |
|
22 |
| - Returns |
23 |
| - ------- |
24 |
| - cat[n]: An array containing the first `n+1` Catalan numbers |
25 |
| - */ |
26 |
| - |
27 |
| - // By definition, the first two Catalan numbers are 1 |
28 |
| - cat[0] = cat[1] = 1; |
29 |
| - |
30 |
| - // Compute the remaining numbers from index 2 to index n, using tabulation |
31 |
| - for (int i = 2; i <= n; i++) { |
32 |
| - cat[i] = 0; |
33 |
| - for (int j = 0; j < i; j++) |
34 |
| - cat[i] += cat[j] * cat[i - j - 1]; // applying the definition here |
| 13 | +/** |
| 14 | + * @brief computes and caches Catalan numbers |
| 15 | + */ |
| 16 | +class catalan_numbers { |
| 17 | + using value_type = std::uint64_t; |
| 18 | + std::vector<value_type> known{1, 1}; |
| 19 | + |
| 20 | + value_type compute_next() { |
| 21 | + value_type res = 0; |
| 22 | + for (std::size_t i = 0; i < known.size(); ++i) { |
| 23 | + res += known[i] * known[known.size() - i - 1]; |
| 24 | + } |
| 25 | + return res; |
35 | 26 | }
|
36 | 27 |
|
37 |
| - // Return the result |
38 |
| - return cat[n]; |
39 |
| -} |
40 |
| - |
41 |
| -int main(int argc, char *argv[]) { |
42 |
| - int n; |
43 |
| - cout << "Enter n: "; |
44 |
| - cin >> n; |
45 |
| - |
46 |
| - cat = new int[n + 1]; |
47 |
| - |
48 |
| - cout << "Catalan numbers from 0 to " << n << " are:\n"; |
49 |
| - for (int i = 0; i <= n; i++) { |
50 |
| - cout << "catalan (" << i << ") = " << catalan_dp(i) << endl; |
51 |
| - // NOTE: Since `cat` is a global array, calling `catalan_dp` |
52 |
| - // repeatedly will not recompute the the values already computed |
53 |
| - // as in case of pre-computed values, the array will simply return them, |
54 |
| - // instead of recomputing them. |
| 28 | + void add() { known.push_back(this->compute_next()); } |
| 29 | + |
| 30 | + public: |
| 31 | + /** |
| 32 | + * @brief computes the n-th Catalan number and updates the cache. |
| 33 | + * @return the n-th Catalan number |
| 34 | + */ |
| 35 | + value_type get(std::size_t n) { |
| 36 | + while (known.size() <= n) { |
| 37 | + this->add(); |
| 38 | + } |
| 39 | + return known[n]; |
55 | 40 | }
|
56 |
| - |
57 |
| - return 0; |
| 41 | +}; |
| 42 | + |
| 43 | +void test_catalan_numbers_up_to_20() { |
| 44 | + // data verified with https://oeis.org/A000108/ |
| 45 | + catalan_numbers cn; |
| 46 | + assert(cn.get(0) == 1ULL); |
| 47 | + assert(cn.get(1) == 1ULL); |
| 48 | + assert(cn.get(2) == 2ULL); |
| 49 | + assert(cn.get(3) == 5ULL); |
| 50 | + assert(cn.get(4) == 14ULL); |
| 51 | + assert(cn.get(5) == 42ULL); |
| 52 | + assert(cn.get(6) == 132ULL); |
| 53 | + assert(cn.get(7) == 429ULL); |
| 54 | + assert(cn.get(8) == 1430ULL); |
| 55 | + assert(cn.get(9) == 4862ULL); |
| 56 | + assert(cn.get(10) == 16796ULL); |
| 57 | + assert(cn.get(11) == 58786ULL); |
| 58 | + assert(cn.get(12) == 208012ULL); |
| 59 | + assert(cn.get(13) == 742900ULL); |
| 60 | + assert(cn.get(14) == 2674440ULL); |
| 61 | + assert(cn.get(15) == 9694845ULL); |
| 62 | + assert(cn.get(16) == 35357670ULL); |
| 63 | + assert(cn.get(17) == 129644790ULL); |
| 64 | + assert(cn.get(18) == 477638700ULL); |
| 65 | + assert(cn.get(19) == 1767263190ULL); |
| 66 | + assert(cn.get(20) == 6564120420ULL); |
58 | 67 | }
|
59 | 68 |
|
60 |
| -/** Sample Test Case: |
61 |
| -
|
62 |
| -$ cd "Dynamic Programming" |
63 |
| -$ g++ Catalan-Numbers.cpp |
64 |
| -$ ./a.exe |
65 |
| -
|
66 |
| -Enter n: 5 |
67 |
| -Catalan numbers from 0 to 5 are: |
68 |
| -catalan (0) = 1 |
69 |
| -catalan (1) = 1 |
70 |
| -catalan (2) = 2 |
71 |
| -catalan (3) = 5 |
72 |
| -catalan (4) = 14 |
73 |
| -catalan (5) = 42 |
| 69 | +void test_catalan_numbers_25() { |
| 70 | + // data verified with https://oeis.org/A000108/ |
| 71 | + catalan_numbers cn; |
| 72 | + assert(cn.get(25) == 4861946401452ULL); |
| 73 | +} |
74 | 74 |
|
75 |
| -*/ |
| 75 | +int main() { |
| 76 | + test_catalan_numbers_up_to_20(); |
| 77 | + test_catalan_numbers_25(); |
| 78 | +} |
0 commit comments