%mathpiper,def="ChineseRemainderInteger"

/* Chinese Remaindering algorithm, as described in "Modern Computer Algebra".
 */
ChineseRemainderInteger(mlist_IsList,vlist_IsList) <--
[
  Local(m,i,nr,result,msub,euclid,clist);
  clist:={};
  m:=Product(mlist);
  result:=0;

  nr:=Length(mlist);
  For(i:=1,i<=nr,i++)
  [
    msub:=Quotient(m,mlist[i]);
    euclid := ExtendedEuclidean(msub,mlist[i]);
    Local(c);
    c:=vlist[i] * euclid[2];
    c:=Rem(c, mlist[i]);
    DestructiveAppend(clist,c);
    result:=result + msub * c;
  ];
  {result,clist};
];

%/mathpiper