Processing math: 100%

Monday, February 8, 2016

Parity of binomial coefficients

Consider the binomial coefficient C(n,m) = n*(n-1)*...*(n-m+1)/(m*(m-1)*...*2*1).  When is this number odd and when is it even?  In this post, we use Lucas' theorem to provides a simple characterization of this.  Lucas' theorem states that for integers m, n and prime p, the following relationship holds:
C(n,m)ki=0C(ni,mi)modp
where ni and mi are the digits of n and m in base p respectively, with n0 and m0 being the least significant digits.

When p=2, ni and mi are the bits in the binary expansion of n and m and C(ni,mi) is 0 if and only if ni<mi. This implies that C(n,m)0mod2 if and only if  ni<mi for some i.

The truth table for  ni<mi is:

nimini<mi000011100110

i.e., it is logically equivalent to miAND(NOTni).  If we think of AND and NOT as bitwise operations on the binary representation of numbers, then we have shown that:

C(n,m)0mod2 if and only if mAND(NOTn) is not zero.

A simple Python function that implements this statement is

def bincoefmod2(n,m): 
# computes binomial(n,m) mod 2
    return 0 if ~n & m else 1

or equivalently:

def bincoefmod2(n,m): 
# computes binomial(n,m) mod 2
    return int(not ~n & m)


Incidentally, for bits ni and mi, ni<mi is logically equivalent to NOT(mini).

Next we consider C(n,m)C(a,b) mod 2.  Clearly this is equivalent (mod 2)  to (C(n,m) mod 2)*(C(a,b) mod 2).  Thus C(n,m)C(a,b)0mod2 if and only if mAND(NOTn) is not zero or bAND(NOTa)  is not zero.  This in turns implies the following:

C(n,m)C(a,b)0mod2(mAND(NOTn))OR(bAND(NOTa))0

where OR is a bitwise operation. The corresponding Python function is:


def bincoefprodmod2(n,m,a,b): 
# computes binomial(n,m)*binomial(a,b) mod 2
    return 0 if (~n & m)|(~a & b) else 1

or equivalently:
def bincoefprodmod2(n,m,a,b): 
# computes binomial(n,m)*binomial(a,b) mod 2
    return int(not (~n & m)|(~a & b))



No comments:

Post a Comment