TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
catalan_numbers.cpp File Reference

Provides utilities to compute Catalan numbers using dynamic programming. A Catalan numbers satisfy these recurrence relations: C(0) = C(1) = 1; C(n) = sum(C(i).C(n-i-1)), for i = 0 to n-1 Read more about Catalan numbers here: https://en.wikipedia.org/wiki/Catalan_number https://oeis.org/A000108/. More...

#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <numeric>
#include <vector>
Include dependency graph for catalan_numbers.cpp:

Go to the source code of this file.

Classes

class  catalan_numbers
 computes and caches Catalan numbers More...
 

Functions

void test_catalan_numbers_up_to_20 ()
 
void test_catalan_numbers_25 ()
 
int main ()
 

Detailed Description

Provides utilities to compute Catalan numbers using dynamic programming. A Catalan numbers satisfy these recurrence relations: C(0) = C(1) = 1; C(n) = sum(C(i).C(n-i-1)), for i = 0 to n-1 Read more about Catalan numbers here: https://en.wikipedia.org/wiki/Catalan_number https://oeis.org/A000108/.

Definition in file catalan_numbers.cpp.

Function Documentation

◆ main()

int main ( void )

Definition at line 79 of file catalan_numbers.cpp.

79 {
80 test_catalan_numbers_up_to_20();
81 test_catalan_numbers_25();
82}

◆ test_catalan_numbers_25()

void test_catalan_numbers_25 ( )

Definition at line 73 of file catalan_numbers.cpp.

73 {
74 // data verified with https://oeis.org/A000108/
76 assert(cn.get(25) == 4861946401452ULL);
77}
computes and caches Catalan numbers
value_type get(std::size_t n)
computes the n-th Catalan number and updates the cache.

◆ test_catalan_numbers_up_to_20()

void test_catalan_numbers_up_to_20 ( )

Definition at line 47 of file catalan_numbers.cpp.

47 {
48 // data verified with https://oeis.org/A000108/
50 assert(cn.get(0) == 1ULL);
51 assert(cn.get(1) == 1ULL);
52 assert(cn.get(2) == 2ULL);
53 assert(cn.get(3) == 5ULL);
54 assert(cn.get(4) == 14ULL);
55 assert(cn.get(5) == 42ULL);
56 assert(cn.get(6) == 132ULL);
57 assert(cn.get(7) == 429ULL);
58 assert(cn.get(8) == 1430ULL);
59 assert(cn.get(9) == 4862ULL);
60 assert(cn.get(10) == 16796ULL);
61 assert(cn.get(11) == 58786ULL);
62 assert(cn.get(12) == 208012ULL);
63 assert(cn.get(13) == 742900ULL);
64 assert(cn.get(14) == 2674440ULL);
65 assert(cn.get(15) == 9694845ULL);
66 assert(cn.get(16) == 35357670ULL);
67 assert(cn.get(17) == 129644790ULL);
68 assert(cn.get(18) == 477638700ULL);
69 assert(cn.get(19) == 1767263190ULL);
70 assert(cn.get(20) == 6564120420ULL);
71}