TermePrincipal := function (P)
  return Coefficients(P)[1]*Monomials(P)[1];
end function;

ChercheMultiple := function(M, TPI)
  local p, i;
  for i:=1 to #TPI do
    if IsDivisibleBy(M,TPI[i]) then
       return true,i;
    end if;
  end for;
  return false,_;
end function;

Division := function(AP, I)
  local P, TPI, L, R, M, t, i, q;
  P   := AP;
  TPI := [TermePrincipal(i) : i in I];
  L   := [0*x : x in I];
  R   := 0*P;
  while not IsZero(P) do
    M := TermePrincipal(P);
    t, i := ChercheMultiple(M, TPI);
    if t then
      q     := M div TPI[i];
      L[i] +:= q;
      P    -:= q * I[i];
    else 
      R +:= M;
      P -:= M;
    end if;
  end while;
  return L, R;
end function;


