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

computes and caches Catalan numbers More...

Collaboration diagram for catalan_numbers:
[legend]

Public Member Functions

value_type get (std::size_t n)
 computes the n-th Catalan number and updates the cache.
 

Private Types

using value_type = std::uint64_t
 

Private Member Functions

value_type compute_next ()
 
void add ()
 

Private Attributes

std::vector< value_type > known {1, 1}
 

Detailed Description

computes and caches Catalan numbers

for assert for std::uint64_t for std::size_t for std::plus & std::multiplies for std::transform_reduce for std::vector

Definition at line 22 of file catalan_numbers.cpp.

Member Typedef Documentation

◆ value_type

using catalan_numbers::value_type = std::uint64_t
private

Definition at line 23 of file catalan_numbers.cpp.

Member Function Documentation

◆ add()

void catalan_numbers::add ( )
inlineprivate

Definition at line 32 of file catalan_numbers.cpp.

32{ known.push_back(this->compute_next()); }

◆ compute_next()

value_type catalan_numbers::compute_next ( )
inlineprivate

Definition at line 26 of file catalan_numbers.cpp.

26 {
27 return std::transform_reduce(known.begin(), known.end(), known.rbegin(),
28 static_cast<value_type>(0), std::plus<>(),
29 std::multiplies<>());
30 }

◆ get()

value_type catalan_numbers::get ( std::size_t n)
inline

computes the n-th Catalan number and updates the cache.

Returns
the n-th Catalan number

Definition at line 39 of file catalan_numbers.cpp.

39 {
40 while (known.size() <= n) {
41 this->add();
42 }
43 return known[n];
44 }

Member Data Documentation

◆ known

std::vector<value_type> catalan_numbers::known {1, 1}
private

Definition at line 24 of file catalan_numbers.cpp.

24{1, 1};

The documentation for this class was generated from the following file: