1 #include <Base/Math/BinomialCoefficient.hh>
3 #include <Base/Math/Random.hh>
4 #include <Base/Debug/Exception.hh>
11 unsigned binomial_coeff(
const unsigned n,
const unsigned k);
14 int main(
int argc,
char *argv[])
16 const unsigned num = 1000;
17 const unsigned max_n = 25;
19 if (argc==2) arg1 = argv[1];
20 const bool verbose = (arg1!=
"silent");
24 for (
unsigned t=0; t<num; t++){
28 #if __GNUC_PREREQ(4,6)
29 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
31 long long unsigned bc = 0;
33 #if __GNUC_PREREQ(4,6)
34 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
37 bc_rec = binomial_coeff(n, k);
38 BIASASSERT(bc == bc_rec);
40 cout <<
"cannot compute "<<n<<
" over "<<k<<
": overflow:"<<ex.
What()<<endl;
42 if (verbose && t%10==0 ){
52 unsigned binomial_coeff(
const unsigned n,
const unsigned k)
54 if ((k==0) || (k==n) || (k==0 && n==0)){
58 unsigned res1 = binomial_coeff(n-1, k-1);
59 unsigned res2 = binomial_coeff(n-1, k);
60 unsigned binomial_coefficent = res1 + res2;
61 if (binomial_coefficent < res1 ||
62 binomial_coefficent < res2){
63 BEXCEPTION(
"Overflow\n");
65 return binomial_coefficent;
static long long unsigned Compute(const unsigned n, const unsigned k)
Computation of the binomial coefficient "n over k" The function throws an exception when overflow occ...
int GetUniformDistributedInt(const int min, const int max)
get uniform distributed random variable including min/max
class for producing random numbers from different distributions
virtual const std::string & What() const