diff --git a/dynamic_programming/catalan_numbers.cpp b/dynamic_programming/catalan_numbers.cpp index b37f43a4e74..c99ea00265c 100644 --- a/dynamic_programming/catalan_numbers.cpp +++ b/dynamic_programming/catalan_numbers.cpp @@ -12,6 +12,7 @@ #include /// for assert #include /// for std::uint64_t #include /// for std::size_t +#include /// for std::transform_reduce #include /// for std::vector /** @@ -22,11 +23,9 @@ class catalan_numbers { std::vector known{1, 1}; value_type compute_next() { - value_type res = 0; - for (std::size_t i = 0; i < known.size(); ++i) { - res += known[i] * known[known.size() - i - 1]; - } - return res; + return std::transform_reduce(known.begin(), known.end(), known.rbegin(), + static_cast(), std::plus<>(), + std::multiplies<>()); } void add() { known.push_back(this->compute_next()); }