Chinese theorem



A few quick comments about crt:

 (1) To define a local variable x, use block([x], ...), not block(x ...

 (2) To access the i-th member of a list l, use inpart(l,i), not l[i]. Why?

   OK:

   (%i8) f(l) := product(l[i],i,1, length(l))$
   (%i9) f([a,b,c]);
   (%o9) a*b*c

   Not intended:

   (%i10) l[x] : 42$
   (%i11) f([a,b,c]);
   (%o11) l[1]*l[2]*l[3]

   OK:

   (%i12) g(l) := product(inpart(l,i),i,1, length(l))$
   (%i13) g([a,b,c]);
   (%o13) a*b*c

 (3) For efficiency, to multiply or add list members, use xreduce, not
     product or sum;
     for example use xreduce("*", M), instead of product(M[i], i, 1, length
     (M)). Alternatively,
     use lsum (but I think there is no lproduct?).

 (4) Use 'return' to break out of a loop, but otherwise 'return' isn't
 needed.

 (5) A more general algorithm or input checking would be good:

   (%i15) crt([2,4],[8,10]);
   (%o15) 4*mod(13*false,20)

Maybe this will help.

Barton

-----maxima-bounces at math.utexas.edu wrote: -----


>crt(A,?M)?:=?block(
>????Mprod?:?product(M[i],?i,?1,?length(M)),
>????Mdiv?:?map(lambda([x],?Mprod?/?x),?M),
>????X?:?map(inv_mod,?Mdiv,?M),
>????x?:?sum(A[i]*X[i]*Mdiv[i],?i,?1,?length(M)),
>????return(mod(x,?Mprod))
>)$