data-structures · intermediate · ~15 min

Binomial coefficient

Compute C(n,k) exactly.

Challenge

Implement:

long long choose(int n, int k);

Return C(n,k) = n!/(k!(n-k)!). Return 0 if k<0 or k>n. 0 <= n <= 62.

Input format

n in [0,62], any k.

Output format

C(n,k), or 0 if out of range.

Constraints

Avoid overflow by building the product incrementally.

Starter code

#include <stddef.h>
/* Binomial coefficient C(n,k) = n!/(k!(n-k)!). Return 0 if k<0 or k>n. 0<=n<=62. */
long long choose(int n,int k){ (void)n;(void)k; return 0; }

Common mistakes

Naive Pascal recursion is exponential; overflow if you compute n! directly.

Edge cases to handle

C(n,0)=C(n,n)=1.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.