Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
n_choose_r.cpp File Reference

Combinations n choose r function implementation More...

#include <iostream>
#include <cassert>
Include dependency graph for n_choose_r.cpp:

Namespaces

 math
 for std::vector
 

Functions

template<class T >
math::n_choose_r (T n, T r)
 This is the function implementation of \( \binom{n}{r} \). More...
 
static void test ()
 Test implementations. More...
 
int main (int argc, char *argv[])
 Main function. More...
 

Detailed Description

Combinations n choose r function implementation

A very basic and efficient method of calculating choosing r from n different choices. \( \binom{n}{r} = \frac{n!}{r! (n-r)!} \)

Author
Tajmeet Singh

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Main function.

Parameters
argccommandline argument count (ignored)
argvcommandline array of arguments (ignored)
Returns
0 on exit
78  {
79  test(); // executing tests
80  return 0;
81 }
Here is the call graph for this function:

◆ test()

static void test ( )
static

Test implementations.

Returns
void
49  {
50  // First test on 5 choose 2
51  uint8_t t = math::n_choose_r(5, 2);
52  assert(((void)"10 is the answer but function says otherwise.\n",
53  t == 10));
54  std::cout << "First test passes." << std::endl;
55  // Second test on 5 choose 3
56  t = math::n_choose_r(5, 3);
57  assert(((void)"10 is the answer but the function says otherwise.\n",
58  t == 10));
59  std::cout << "Second test passes." << std::endl;
60  // Third test on 3 choose 2
61  t = math::n_choose_r(3, 2);
62  assert(((void)"3 is the answer but the function says otherwise.\n",
63  t == 3));
64  std::cout << "Third test passes." << std::endl;
65  // Fourth test on 10 choose 4
66  t = math::n_choose_r(10, 4);
67  assert(((void)"210 is the answer but the function says otherwise.\n",
68  t == 210));
69  std::cout << "Fourth test passes." << std::endl;
70 }
Here is the call graph for this function:
test
static void test()
Test implementations.
Definition: n_choose_r.cpp:49
std::cout
math::n_choose_r
T n_choose_r(T n, T r)
This is the function implementation of .
Definition: n_choose_r.cpp:33
std::endl
T endl(T... args)