Fastest way to generate binomial coefficients

后端 未结 11 1784
[愿得一人]
[愿得一人] 2020-12-13 02:20

I need to calculate combinations for a number.

What is the fastest way to calculate nCp where n>>p?

I need a fast way to generate binomial coefficients for a

11条回答
  •  我在风中等你
    2020-12-13 03:04

    If you need to compute them for all n, Ribtoks's answer is probably the best. For a single n, you're better off doing like this:

    C[0] = 1
    for (int k = 0; k < n; ++ k)
        C[k+1] = (C[k] * (n-k)) / (k+1)
    

    The division is exact, if done after the multiplication.

    And beware of overflowing with C[k] * (n-k) : use large enough integers.

提交回复
热议问题