Problem #53 Difficulty: 5%

Combinatoric Selections

Solution Language: Python

The problem

Count how many of the binomial coefficients C(n, r), for 1 ≤ n ≤ 100, are greater than one million.

My solution

Straight from the definition C(n, r) = n! / (r!·(n − r)!), using Python’s big integers. A list holds the factorials 1! through 100! (index k − 1 holds k!, filled by a recursive fact), and the code goes through every (n, r) with 1 ≤ r ≤ n ≤ 100, counting the coefficients over 10⁶.

def comb(n, r):
  return list[n-1]/list[n-r-1]/list[r-1]

It computes all 5,050 values directly, which takes a few hundredths of a second. The file is Python 2, so / is integer division and print count is a statement.