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

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

#include "ADS/Contract.h"
#include <cstddef>
#include <cstdint>
#include <vector>
#include "ADS/numeric/impl/Combinatorics.tpp"

Go to the source code of this file.

Classes

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

Namespaces

namespace  ADS

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:

  • binomial(), \( \binom{n}{k} \) by its recursive definition \( \binom{n}{k} = \binom{n-1}{k-1} + \binom{n-1}{k} \), whose calls are as many as \( 2 \binom{n}{k} - 1 \), and tartaglia(), the triangle of Tartaglia of all the \( \binom{m}{k} \), \( m \le n \), in \( \Theta( n^2 ) \) time, each then read in \( O( 1 ) \);
  • fibonacci(), \( F_0 = F_1 = 1 \) and \( F_n = F_{n-1} + F_{n-2} \), with a vector of \( n \) places and with three variables, \( O( n ) \) time and \( O( 1 ) \) space, while the recursion makes about \( 1.6^n \) calls; FibonacciCache keeps the numbers already computed between the calls, in a mutable member, so that a number already asked for costs \( O( 1 ) \) and a new one only the steps from the largest one known;
  • beyond the book, factorial() and factorialRec(), the iterative and the recursive realization of the same specification (the functional abstraction), and binomialByFactorials(), \( n! / ( k! ( n - k )! ) \), which overflows as soon as \( n! \) does, i.e., for \( n > 20 \), while the recursion and the triangle reach \( n = 67 \).

The contracts [see Contract.h]: the results fit in 64 bits, i.e., \( 0 \le k \le n \le 67 \) for binomial() and the triangle, \( 0 \le n \le 92 \) for the numbers of Fibonacci, \( 0 \le n \le 20 \) for the factorial and for binomialByFactorials(). The functions are defined in Combinatorics.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, the introduction of chapter 13 and section 13.4, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa