#include <xtend/math.h>
-lxtend
#include <stdint.h>
#include "math.h"
unsigned long xt_n_choose_k(unsigned long n, unsigned long k)
n Number of items to choose from
k Number of items chosen
Compute the binomial coefficient N choose K = N! / (K! * (N-K)!). This
represents the number of ways to choose K items out of a pool of N, such that
we don't care about order. E.g., if choosing 2 letters from the set [A B C D
E], [C D] is considered the same [D C].
This implementation avoids overflow by alternating multiply and
divide operations (rather than try to compute factorials first, which will
fail for relatively small values of N or K).
The number of ways to choose K items from N objects.
#include <xtend/math.h>
unsigned long n = 5, k = 2;
printf("Ways to choose %lu items from %lu = %lun",
k, n, xt_binomial(n, k));