You can look at it from the other end: How many nCr not divisible by p ? There is a fairly simple formula for this.
Qualifiers:
The binomial coefficient nCr is determined by the expression
nCr = n! / (r! * (nr)!)
therefore, the multiplicity v_p(nCr) of p in nCr - an exponent of p in a simple factorization of nCr - is
v_p(nCr) = v_p(n!) - v_p(r!) - v_p((nr)!)
The multiplicity of p in n! can be easily determined, a well-known way of calculating it is
v_p(n!) = β floor(n/p^k) k > 0
If you look at this formula taking into account the expansion of the base p n , you will see that
v_p(n!) = (n - Ο_p(n)) / (p - 1)
where Ο_p(k) is the sum of the digits of the base representation k . In this way,
v_p(nCr) = (n - Ο_p(n) - r + Ο_p(r) - (nr) + Ο_p(nr)) / (p - 1) = (Ο_p(r) + Ο_p(nr) - Ο_p(n)) / (p - 1)
Output:
nCr not divisible by prime p if and only if the addition of r and nr does not have a carrier in the base p .
Because itβs accurate when Ο_p(a + b) = Ο_p(a) + Ο_p(b) . The non-addition in the appendix occurs when the sum of the corresponding digits a and b (plus, possibly, the transfer, if it has already been carried over for less significant digits) is >= p , then the corresponding digit in the sum decreases by p , and the next more significant digit increases by 1 , decreasing the digital sum by p - 1 .
We have a carrier additive n = r + (nr) in the base p if for each digit d_k in the extension n base- p corresponding digit r less than or equal to d_k . The valid choice of digits r is independent, so the total number is the product of the number of samples for individual digits.
The number nCr not divisible by the prime p is
ND(n,p) = β(d_k + 1)
where d_k are numbers in the extension n base p ,
m n = β d_k * p^k k=0
Since there are n+1 (non-zero) binomial coefficients nCr for a given n , the number of (non-zero) binomial coefficients nCr divisible by p is
m n + 1 - β (d_k + 1) k=0
with the above base p extension n .
Using Mike's example n = 10 , p = 3 ,
10 = 1*3^0 + 0*3^1 + 1*3^2
therefore, there are coefficients (1+1)*(0+1)*(1+1) = 4 10Cr , not divisible by 3 and 10 + 1 - 4 = 7 , divisible by 3.
def divisibleCoefficients(n,p): m, nondiv = n, 1 while m > 0: nondiv = nondiv * ((m % p) + 1) m = m // p return (n + 1 - nondiv)