Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * binomial_coefficient.h - function for computing the binomial coefficient 00004 * 00005 * Generated: Sun Nov 04 17:29:46 2007 00006 * Copyright 2007 Martin Liebenberg 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #ifndef __UTILS_MATH_BINOMIAL_COEFFICIENT_H_ 00025 #define __UTILS_MATH_BINOMIAL_COEFFICIENT_H_ 00026 00027 namespace fawkes { 00028 00029 00030 /** @class BinomialCoefficient <utils/math/binomial_coefficient.h> 00031 * Contains method to compute the binomial coefficient. 00032 * 00033 * @author Martin Liebenberg 00034 */ 00035 00036 class BinomialCoefficient 00037 { 00038 public: 00039 /** Calculates the binomial coefficient. 00040 * @param n upper value 00041 * @param k lower value 00042 * @return the binomial coefficient of n and k 00043 */ 00044 static inline unsigned int binoc(unsigned int n, unsigned int k) 00045 { 00046 unsigned int result; 00047 if(k == 0) 00048 return 1; 00049 if(2 * k > n) 00050 result = binoc(n, n - k); 00051 else 00052 { 00053 result = n; 00054 for(unsigned int i = 2; i <= k; i++) 00055 { 00056 result = result * ((n + 1 - i) / i); 00057 } 00058 } 00059 return result; 00060 } 00061 }; 00062 00063 00064 } // end namespace fawkes 00065 00066 #endif