从五个数12345中选出三个数一共有十种方法:
123, 124, 125, 134, 135, 145, 234, 235, 245, and 345
在组合数学中我们用5C3 = 10来表示.
概括来说:
nCr = n! / r!(n−r)!
,其中r ≤ n, n! = n×(n−1)×…×3×2×1, 并且0! = 1.
n = 23时产生第一个超过一百万的数: 23C10 = 1144066.
对于nCr, 1 ≤ n ≤ 100,有多少超过100万的值?包括重复的在内。
import math
def get_combinations_num(n, r):
""" combinations """
return math.factorial(n) / (math.factorial(r) * math.factorial(n - r))
num = 0
n_max = 100
c_upper = 10 ** 6
for i in range(n_max, 0, -1):
num_add = 0
for j in range(int(i / 2) + 1, i - 1):
if get_combinations_num(i, j) > c_upper:
num_add += 2
else:
break
if num_add > 0:
if i % 2 == 0:
num += 1
num += num_add
else:
break
print(num)