In computing the Chinese Remainder theorem from a vector of tuples (residue, modulus) the following code fails :
c = ((1,5),(3,7),(11,13),(19,23))
def crt(c):
residues, moduli = zip(*c)
N = product(moduli)
complements = (N/ni for ni in moduli)
scaled_residues = (product(pair) for pair in zip(residues,complements))
inverses = (modular_inverse(*pair) for pair in zip(complements,moduli))
si = (product(u) for u in zip(scaled_residues,inverses))
result = sum(si) % N
return result
Giving the result as 0 ( I guess the generated iterables are empty ). Yet the following code works perfectly :
def crt(c):
residues, moduli = zip(*c)
N = product(moduli)
complements = list((N/ni for ni in moduli)) # <-- listed
scaled_residues = (product(pair) for pair in zip(residues,complements))
inverses = (modular_inverse(*pair) for pair in zip(complements,moduli))
si = (product(u) for u in zip(scaled_residues,inverses))
result = sum(si) % N
return result
Which yields (a) correct result of 8851.
Why should I have to list(
one of the first generators? Adding list
to any subsequent generator does not change the fail (0) result. Only listing this first generator produces the correct result. What is going on here ?