Optimizing calculating combination and avoiding overflows

前端 未结 2 1296
借酒劲吻你
借酒劲吻你 2021-01-25 01:42

I am solving a programming problem which is stuck at calculating nCr efficiently and at the same time avoiding overflows. I have made the following trivial simplifi

2条回答
  •  被撕碎了的回忆
    2021-01-25 02:21

    I had to solve this problem, too. What I did was use the fact that there are the same number of multiplications as divisions and bundled them together, taking one multiplication and one division at a time. It comes out as an integer at the end, but I use double for the intermediate terms and then round to the nearest integer at the end.

    // Return the number of combinations of 'n choose k'
    unsigned int binomial(unsigned int n, unsigned int k) {
    unsigned int higher_idx;
    unsigned int lower_idx;
    if(k > n-k) {
        higher_idx = k;
        lower_idx = n - k;
    } else {
        higher_idx = n - k;
        lower_idx = k;
    }
    double product = 1.0;
    double factor;
    unsigned int idx;
    for(idx=n; idx>higher_idx; idx--) {
        factor = (double)idx / double(lower_idx - (n - idx));
        product *= factor;
    }
    return (unsigned int)(product + 0.5);
    }
    

提交回复
热议问题