ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Combinatorics.h

The examples of the introduction and of section 13.4 of chapter 13. More...

Files

file  ADS/numeric/Combinatorics.h
 The examples of the introduction and of section 13.4 of chapter 13:

Classes

class  ADS::FibonacciCache
 the numbers of Fibonacci, those computed kept between the calls More...

Functions

std::uint64_t ADS::binomial (int n, int k, long *calls=nullptr)
 the binomial coefficient \( \binom{n}{k} \) by the recursion; the calls added to *calls, if given
std::vector< std::vector< std::uint64_t > > ADS::tartaglia (int n)
 the triangle of Tartaglia, the table of the \( \binom{m}{k} \), \( 0 \le k \le m \le n \)
std::uint64_t ADS::fibonacciTable (int n)
 the number \( F_n \) of Fibonacci, \( F_0 = F_1 = 1 \), with a vector
std::uint64_t ADS::fibonacci (int n)
 the number \( F_n \) of Fibonacci, \( F_0 = F_1 = 1 \), in \( O( 1 ) \) space
std::uint64_t ADS::factorial (int n)
 the factorial \( n! \) by a loop
std::uint64_t ADS::factorialRec (int n)
 the factorial \( n! \) by the recursion \( n! = n ( n - 1 )! \), \( 0! = 1 \)
std::uint64_t ADS::binomialByFactorials (int n, int k)
 the binomial coefficient \( \binom{n}{k} \) as \( n! / ( k! ( n - k )! ) \)

Detailed description

The examples of the introduction and of section 13.4 of chapter 13.

The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.

Function documentation

◆ binomial()

std::uint64_t ADS::binomial ( int n,
int k,
long * calls = nullptr )
inline

the binomial coefficient \( \binom{n}{k} \) by the recursion; the calls added to *calls, if given

In pseudocode (the introduction of chapter 13), \( 2 \binom{n}{k} - 1 \) calls, since each leaf returns 1:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{binomial}( n , k )\quad \triangleright \ \text{exponential: no table} \cr 2 & \quad \textbf{if}\ 0 < k < n\ \textbf{then return} \ \mathrm{binomial}( n - 1 , k ) + \mathrm{binomial}( n - 1 , k - 1 ) \cr 3 & \quad \textbf{return}\ 1 \end{array}} \]

Precondition
\( 0 \le k \le n \le 67 \)

◆ binomialByFactorials()

std::uint64_t ADS::binomialByFactorials ( int n,
int k )
inline

the binomial coefficient \( \binom{n}{k} \) as \( n! / ( k! ( n - k )! ) \)

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{binomialByFactorials}( n , k ) \cr 2 & \quad \textbf{return}\ \mathrm{factorial}( n ) / ( \mathrm{factorial}( k ) \cdot \mathrm{factorial}( n - k ) ) \end{array}} \]

Precondition
\( 0 \le k \le n \le 20 \)

◆ factorial()

std::uint64_t ADS::factorial ( int n)
inline

the factorial \( n! \) by a loop

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{factorial}( n ) \cr 2 & \quad f \gets 1 \cr 3 & \quad \textbf{for}\ i \gets 2\ \textbf{to}\ n\ \textbf{do}\ f \gets f \cdot i \cr 4 & \quad \textbf{return}\ f \end{array}} \]

Precondition
\( 0 \le n \le 20 \)

◆ factorialRec()

std::uint64_t ADS::factorialRec ( int n)
inline

the factorial \( n! \) by the recursion \( n! = n ( n - 1 )! \), \( 0! = 1 \)

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{factorialRec}( n ) \cr 2 & \quad \textbf{if}\ n \le 1\ \textbf{then return}\ 1 \cr 3 & \quad \textbf{return}\ n \cdot \mathrm{factorialRec}( n - 1 ) \end{array}} \]

Precondition
\( 0 \le n \le 20 \)

◆ fibonacci()

std::uint64_t ADS::fibonacci ( int n)
inline

the number \( F_n \) of Fibonacci, \( F_0 = F_1 = 1 \), in \( O( 1 ) \) space

In pseudocode (section 13.4), the last three numbers only:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fibonacci}( n )\quad \triangleright \ \text{the last three terms only} \cr 2 & \quad F_0 \gets 1;\ F_1 \gets 1;\ F_2 \gets 1 \cr 3 & \quad \textbf{for}\ i \gets 2\ \textbf{to}\ n\ \textbf{do}\ ( F_0 , F_1 , F_2 ) \gets ( F_1 , F_2 , F_1 + F_2 ) \cr 4 & \quad \textbf{return}\ F_2 \end{array}} \]

Precondition
\( 0 \le n \le 92 \)

◆ fibonacciTable()

std::uint64_t ADS::fibonacciTable ( int n)
inline

the number \( F_n \) of Fibonacci, \( F_0 = F_1 = 1 \), with a vector

In pseudocode (section 13.4):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fibonacciTable}( n ) \cr 2 & \quad F[ 0 ] \gets 1;\ F[ 1 ] \gets 1 \cr 3 & \quad F[ i ] \gets F[ i - 2 ] + F[ i - 1 ] \text{ for } i = 2 , \ldots , n\quad \triangleright\ O( n ) \text{ space} \cr 4 & \quad \textbf{return}\ F[ n ] \end{array}} \]

Precondition
\( 0 \le n \le 92 \)

◆ tartaglia()

std::vector< std::vector< std::uint64_t > > ADS::tartaglia ( int n)
inline

the triangle of Tartaglia, the table of the \( \binom{m}{k} \), \( 0 \le k \le m \le n \)

In pseudocode (the introduction of chapter 13), each row from the one before:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tartaglia}( n )\quad \triangleright \ \text{the triangle, row by row} \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n\ \textbf{do} \cr 3 & \qquad C[ i ][ 0 ] \gets 1;\ C[ i ][ i ] \gets 1 \cr 4 & \qquad C[ i ][ j ] \gets C[ i - 1 ][ j ] + C[ i - 1 ][ j - 1 ] \text{ for } j = 1 , \ldots , i - 1 \cr 5 & \quad \textbf{return}\ C \end{array}} \]

Precondition
\( 0 \le n \le 67 \)