Fawkes API  Fawkes Development Version
binomial_coefficient.h
1 
2 /***************************************************************************
3  * binomial_coefficient.h - function for computing the binomial coefficient
4  *
5  * Generated: Sun Nov 04 17:29:46 2007
6  * Copyright 2007 Martin Liebenberg
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __UTILS_MATH_BINOMIAL_COEFFICIENT_H_
25 #define __UTILS_MATH_BINOMIAL_COEFFICIENT_H_
26 
27 namespace fawkes {
28 
29 
30 /** @class BinomialCoefficient <utils/math/binomial_coefficient.h>
31  * Contains method to compute the binomial coefficient.
32  *
33  * @author Martin Liebenberg
34  */
35 
37 {
38  public:
39  /** Calculates the binomial coefficient.
40  * @param n upper value
41  * @param k lower value
42  * @return the binomial coefficient of n and k
43  */
44  static inline unsigned int binoc(unsigned int n, unsigned int k)
45  {
46  unsigned int result;
47  if(k == 0)
48  return 1;
49  if(2 * k > n)
50  result = binoc(n, n - k);
51  else
52  {
53  result = n;
54  for(unsigned int i = 2; i <= k; i++)
55  {
56  result = result * ((n + 1 - i) / i);
57  }
58  }
59  return result;
60  }
61 };
62 
63 
64 } // end namespace fawkes
65 
66 #endif
Fawkes library namespace.
Contains method to compute the binomial coefficient.
static unsigned int binoc(unsigned int n, unsigned int k)
Calculates the binomial coefficient.