Combinatoric selections (Euler Problem 53)

Back to overview.

https://projecteuler.net/problem=53

There are exactly ten ways of selecting three from five, 12345:

$123, 124, 125, 134, 135, 145, 234, 235, 245, 345$

In combinatorics, we use the notation, 5C3 = 10.

In general,

$nCr = \frac{n!}{r!(n−r)!}$, where r ≤ n, n! = n×(n−1)×...×3×2×1, and 0! = 1.

It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.

How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater than one-million?

In [3]:
import math

def combinations_naiv(n, r):
    return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))

assert(combinations_naiv(5, 3) == 10)
assert(combinations_naiv(23, 10) == 1144066)
assert(combinations_naiv(20, 20) == 1)
In [7]:
count = 0

for n in range (1, 101):
    for r in range(1, n + 1):
        if combinations_naiv(n, r) > 10**6:
            count += 1

s = count
In [9]:
print(s)
assert(s == 4075)
4075
In [ ]: