C(n,m)≡k∏i=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(mi⇒ni).
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