libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNCombinatorics.cpp
Go to the documentation of this file.
1 /* Copyright 2008-2014 INSA Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNCombinatorics.cpp
19  * \author Jean DUONG
20  */
21 
23 
24 using namespace crn;
25 
33 int Combinatorics::CountCombinations(int n, int p) noexcept
34 {
35  if ((p == 0) || (p == n))
36  {
37  return 1;
38  }
39 
40  if (p == 1)
41  {
42  return n;
43  }
44 
45  return (CountCombinations(n - 1, p - 1) + CountCombinations(n - 1, p));
46 }
47 
55 int Combinatorics::CountArrangements(int n, int p) noexcept
56 {
57  if (p > n)
58  {
59  return 0;
60  }
61 
62  int Arr = 1;
63 
64  for (int k = n - p + 1; k <= n; k++)
65  {
66  Arr *= k;
67  }
68 
69  return Arr;
70 }
71 
int CountCombinations(int n, int p) noexcept
int CountArrangements(int n, int p) noexcept