# Polynomial long division

Polynomial long division
You are encouraged to solve this task according to the task description, using any language you may know.
 This page uses content from Wikipedia. The original article was at Polynomial long division. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)
In algebra, polynomial long division is an algorithm for dividing a polynomial by another polynomial of the same or lower degree.

Let us suppose a polynomial is represented by a vector, ${\displaystyle x}$ (i.e., an ordered collection of coefficients) so that the ${\displaystyle i}$th element keeps the coefficient of ${\displaystyle x^{i}}$, and the multiplication by a monomial is a shift of the vector's elements "towards right" (injecting ones from left) followed by a multiplication of each element by the coefficient of the monomial.

Then a pseudocode for the polynomial long division using the conventions described above could be:

degree(P):
return the index of the last non-zero element of P;
if all elements are 0, return -∞

polynomial_long_division(N, D) returns (q, r):
// N, D, q, r are vectors
if degree(D) < 0 then error
q ← 0
while degree(N) ≥ degree(D)
d ← D shifted right by (degree(N) - degree(D))
q(degree(N) - degree(D)) ← N(degree(N)) / d(degree(d))
// by construction, degree(d) = degree(N) of course
d ← d * q(degree(N) - degree(D))
N ← N - d
endwhile
r ← N
return (q, r)


Note: vector * scalar multiplies each element of the vector by the scalar; vectorA - vectorB subtracts each element of the vectorB from the element of the vectorA with "the same index". The vectors in the pseudocode are zero-based.

• Error handling (for allocations or for wrong inputs) is not mandatory.
• Conventions can be different; in particular, note that if the first coefficient in the vector is the highest power of x for the polynomial represented by the vector, then the algorithm becomes simpler.

Example for clarification
This example is from Wikipedia, but changed to show how the given pseudocode works.

      0    1    2    3
----------------------
N:  -42    0  -12    1        degree = 3
D:   -3    1    0    0        degree = 1

d(N) - d(D) = 2, so let's shift D towards right by 2:

N:  -42    0  -12    1
d:    0    0   -3    1

N(3)/d(3) = 1, so d is unchanged. Now remember that "shifting by 2"
is like multiplying by x2, and the final multiplication
(here by 1) is the coefficient of this monomial. Let's store this
into q:
0     1     2
---------------
q:   0     0     1

now compute N - d, and let it be the "new" N, and let's loop

N:  -42    0   -9    0        degree = 2
D:   -3    1    0    0        degree = 1

d(N) - d(D) = 1, right shift D by 1 and let it be d

N:  -42    0   -9    0
d:    0   -3    1    0        * -9/1 = -9

q:   0    -9     1

d:    0   27   -9    0

N ← N - d

N:  -42  -27    0    0        degree = 1
D:   -3    1    0    0        degree = 1

looping again... d(N)-d(D)=0, so no shift is needed; we
multiply D by -27 (= -27/1) storing the result in d, then

q:  -27   -9     1

and

N:  -42  -27    0    0        -
d:   81  -27    0    0        =
N: -123    0    0    0        (last N)

d(N) < d(D), so now r ← N, and the result is:

0   1  2
-------------
q:   -27  -9  1   →  x2 - 9x - 27
r:  -123   0  0   →          -123


## Ada

long_division.adb:

with Ada.Text_IO; use Ada.Text_IO; procedure Long_Division is   package Int_IO is new Ada.Text_IO.Integer_IO (Integer);   use Int_IO;    type Degrees is range -1 .. Integer'Last;   subtype Valid_Degrees is Degrees range 0 .. Degrees'Last;   type Polynom is array (Valid_Degrees range <>) of Integer;    function Degree (P : Polynom) return Degrees is   begin      for I in reverse P'Range loop         if P (I) /= 0 then            return I;         end if;      end loop;      return -1;   end Degree;    function Shift_Right (P : Polynom; D : Valid_Degrees) return Polynom is      Result : Polynom (0 .. P'Last + D) := (others => 0);   begin      Result (Result'Last - P'Length + 1 .. Result'Last) := P;      return Result;   end Shift_Right;    function "*" (Left : Polynom; Right : Integer) return Polynom is      Result : Polynom (Left'Range);   begin      for I in Result'Range loop         Result (I) := Left (I) * Right;      end loop;      return Result;   end "*";    function "-" (Left, Right : Polynom) return Polynom is      Result : Polynom (Left'Range);   begin      for I in Result'Range loop         if I in Right'Range then            Result (I) := Left (I) - Right (I);         else            Result (I) := Left (I);         end if;      end loop;      return Result;   end "-";    procedure Poly_Long_Division (Num, Denom : Polynom; Q, R : out Polynom) is      N : Polynom := Num;      D : Polynom := Denom;   begin      if Degree (D) < 0 then         raise Constraint_Error;      end if;      Q := (others => 0);      while Degree (N) >= Degree (D) loop         declare            T : Polynom := Shift_Right (D, Degree (N) - Degree (D));         begin            Q (Degree (N) - Degree (D)) := N (Degree (N)) / T (Degree (T));            T := T * Q (Degree (N) - Degree (D));            N := N - T;         end;      end loop;      R := N;   end Poly_Long_Division;    procedure Output (P : Polynom) is      First : Boolean := True;   begin      for I in reverse P'Range loop         if P (I) /= 0 then            if First then               First := False;            else               Put (" + ");            end if;            if I > 0 then               if P (I) /= 1 then                  Put (P (I), 0);                  Put ("*");               end if;               Put ("x");               if I > 1 then                  Put ("^");                  Put (Integer (I), 0);               end if;            elsif P (I) /= 0 then               Put (P (I), 0);            end if;         end if;      end loop;      New_Line;   end Output;    Test_N : constant Polynom := (0 => -42, 1 => 0, 2 => -12, 3 => 1);   Test_D : constant Polynom := (0 => -3, 1 => 1);   Test_Q : Polynom (Test_N'Range);   Test_R : Polynom (Test_N'Range);begin   Poly_Long_Division (Test_N, Test_D, Test_Q, Test_R);   Put_Line ("Dividing Polynoms:");   Put ("N: "); Output (Test_N);   Put ("D: "); Output (Test_D);   Put_Line ("-------------------------");   Put ("Q: "); Output (Test_Q);   Put ("R: "); Output (Test_R);end Long_Division;

output:

Dividing Polynoms:
N: x^3 + -12*x^2 + -42
D: x + -3
-------------------------
Q: x^2 + -9*x + -27
R: -123

## APL

div←{    {        q r d←⍵        (≢d) > n←≢r : q r        c ← (⊃⌽r) ÷ ⊃⌽d        ∇ (c,q) ((¯1↓r) - c × ¯1↓(-n)↑d) d    } ⍬ ⍺ ⍵}
Output:
      N←¯42 0 ¯12 1
D←¯3 1
⍪N div D
¯27 ¯9 1
¯123


## BBC BASIC

      DIM N%(3) : N%() = -42, 0, -12, 1      DIM D%(3) : D%() =  -3, 1,   0, 0      DIM q%(3), r%(3)      PROC_poly_long_div(N%(), D%(), q%(), r%())      PRINT "Quotient = "; FNcoeff(q%(2)) "x^2" FNcoeff(q%(1)) "x" FNcoeff(q%(0))      PRINT "Remainder = " ; r%(0)      END       DEF PROC_poly_long_div(N%(), D%(), q%(), r%())      LOCAL d%(), i%, s%      DIM d%(DIM(N%(),1))      s% = FNdegree(N%()) - FNdegree(D%())      IF s% >= 0 THEN        q%() = 0        WHILE s% >= 0          FOR i% = 0 TO DIM(d%(),1) - s%            d%(i%+s%) = D%(i%)          NEXT          q%(s%) = N%(FNdegree(N%())) DIV d%(FNdegree(d%()))          d%() = d%() * q%(s%)          N%() -= d%()          s% = FNdegree(N%()) - FNdegree(D%())        ENDWHILE        r%() = N%()      ELSE        q%() = 0        r%() = N%()      ENDIF      ENDPROC       DEF FNdegree(a%())      LOCAL i%      i% = DIM(a%(),1)      WHILE a%(i%)=0        i% -= 1        IF i%<0 EXIT WHILE      ENDWHILE      = i%       DEF FNcoeff(n%)      IF n%=0 THEN = ""      IF n%<0 THEN = " - " + STR$(-n%) IF n%=1 THEN = " + " = " + " + STR$(n%)

Output:

Quotient =  + x^2 - 9x - 27
Remainder = -123


## C

Translation of: Fortran
#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <assert.h>#include <gsl/gsl_vector.h> #define MAX(A,B) (((A)>(B))?(A):(B)) void reoshift(gsl_vector *v, int h){  if ( h > 0 ) {    gsl_vector *temp = gsl_vector_alloc(v->size);    gsl_vector_view p = gsl_vector_subvector(v, 0, v->size - h);    gsl_vector_view p1 = gsl_vector_subvector(temp, h, v->size - h);    gsl_vector_memcpy(&p1.vector, &p.vector);    p = gsl_vector_subvector(temp, 0, h);    gsl_vector_set_zero(&p.vector);    gsl_vector_memcpy(v, temp);    gsl_vector_free(temp);  }} gsl_vector *poly_long_div(gsl_vector *n, gsl_vector *d, gsl_vector **r){  gsl_vector *nt = NULL, *dt = NULL, *rt = NULL, *d2 = NULL, *q = NULL;  int gn, gt, gd;   if ( (n->size >= d->size) && (d->size > 0) && (n->size > 0) ) {    nt = gsl_vector_alloc(n->size); assert(nt != NULL);    dt = gsl_vector_alloc(n->size); assert(dt != NULL);    rt = gsl_vector_alloc(n->size); assert(rt != NULL);    d2 = gsl_vector_alloc(n->size); assert(d2 != NULL);    gsl_vector_memcpy(nt, n);    gsl_vector_set_zero(dt); gsl_vector_set_zero(rt);    gsl_vector_view p = gsl_vector_subvector(dt, 0, d->size);    gsl_vector_memcpy(&p.vector, d);    gsl_vector_memcpy(d2, dt);    gn = n->size - 1;    gd = d->size - 1;    gt = 0;     while( gsl_vector_get(d, gd) == 0 ) gd--;     while ( gn >= gd ) {      reoshift(dt, gn-gd);      double v = gsl_vector_get(nt, gn)/gsl_vector_get(dt, gn);      gsl_vector_set(rt, gn-gd, v);      gsl_vector_scale(dt, v);      gsl_vector_sub(nt, dt);      gt = MAX(gt, gn-gd);      while( (gn>=0) && (gsl_vector_get(nt, gn) == 0.0) ) gn--;      gsl_vector_memcpy(dt, d2);    }     q = gsl_vector_alloc(gt+1); assert(q != NULL);    p = gsl_vector_subvector(rt, 0, gt+1);    gsl_vector_memcpy(q, &p.vector);    if ( r != NULL ) {      if ( (gn+1) > 0 ) {	*r = gsl_vector_alloc(gn+1); assert( *r != NULL );	p = gsl_vector_subvector(nt, 0, gn+1);	gsl_vector_memcpy(*r, &p.vector);      } else {	*r = gsl_vector_alloc(1); assert( *r != NULL );	gsl_vector_set_zero(*r);      }    }    gsl_vector_free(nt); gsl_vector_free(dt);    gsl_vector_free(rt); gsl_vector_free(d2);    return q;  } else {    q = gsl_vector_alloc(1); assert( q != NULL );    gsl_vector_set_zero(q);    if ( r != NULL ) {      *r = gsl_vector_alloc(n->size); assert( *r != NULL );      gsl_vector_memcpy(*r, n);    }    return q;  } } void poly_print(gsl_vector *p){  int i;  for(i=p->size-1; i >= 0; i--) {    if ( i > 0 )       printf("%lfx^%d + ", 	     gsl_vector_get(p, i), i);    else      printf("%lf\n", gsl_vector_get(p, i));  }} gsl_vector *create_poly(int d, ...){  va_list al;  int i;  gsl_vector *r = NULL;   va_start(al, d);  r = gsl_vector_alloc(d); assert( r != NULL );   for(i=0; i < d; i++)    gsl_vector_set(r, i, va_arg(al, double));   return r;}
int main(){  int i;  gsl_vector *q, *r;  gsl_vector *nv, *dv;   //nv = create_poly(4,  -42., 0., -12., 1.);  //dv = create_poly(2,  -3., 1.);  //nv = create_poly(3,  2., 3., 1.);  //dv = create_poly(2,  1., 1.);  nv = create_poly(4, -42., 0., -12., 1.);  dv = create_poly(3, -3., 1., 1.);   q = poly_long_div(nv, dv, &r);   poly_print(q);  poly_print(r);   gsl_vector_free(q);  gsl_vector_free(r);   return 0;}

### Another version

Without outside libs, for clarity. Note that polys are stored and show with zero-degree term first:
#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h> typedef struct {        int power;        double * coef;} poly_t, *poly; #define E(x, i) (x)->coef[i] /* passing in negative power to have a zeroed poly */poly p_new(int power, ...){        int i, zeroed = 0;        va_list ap;         if (power < 0) {                power = -power;                zeroed = 1;        }         poly p = malloc(sizeof(poly_t));        p->power = power;        p->coef = malloc(sizeof(double) * ++power);         if (zeroed)                for (i = 0; i < power; i++) p->coef[i] = 0;        else {                va_start(ap, power);                for (i = 0; i < power; i++)                        E(p, i) = va_arg(ap, double);                va_end(ap);        }         return p;} void p_del(poly p){        free(p->coef);        free(p);} void p_print(poly p){        int i;        for (i = 0; i <= p->power; i++)                printf("%g ", E(p, i));        printf("\n");} poly p_copy(poly p){        poly q = p_new(-p->power);        memcpy(q->coef, p->coef, sizeof(double) * (1 + p->power));        return q;} /* p: poly;  d: divisor;  r: remainder; returns quotient */poly p_div(poly p, poly d, poly* r){        poly q;        int i, j;        int power = p->power - d->power;        double ratio;         if (power < 0) return 0;         q = p_new(-power);        *r= p_copy(p);         for (i = p->power; i >= d->power; i--) {                E(q, i - d->power) = ratio = E(*r, i) / E(d, d->power);                E(*r ,i) = 0;                 for (j = 0; j < d->power; j++)                        E(*r, i - d->power + j) -= E(d, j) * ratio;        }        while (! E(*r, --(*r)->power));         return q;} int main(){        poly p = p_new(3, 1., 2., 3., 4.);        poly d = p_new(2, 1., 2., 1.);        poly r;        poly q = p_div(p, d, &r);         printf("poly: "); p_print(p);        printf("div:  "); p_print(d);        printf("quot: "); p_print(q);        printf("rem:  "); p_print(r);         p_del(p);        p_del(q);        p_del(r);        p_del(d);         return 0;}

## C++

#include <iostream>#include <math.h> using namespace std; // does:  prints all members of vector// input: c - ASCII char with the name of the vector//        d - degree of vector//        A - pointer to vectorvoid Print(char c, int d, double* A) {	int i; 	for (i=0; i < d+1; i++)			cout << c << "[" << i << "]= " << A[i] << endl;	cout << "Degree of " << c << ": " << d << endl << endl;} int main() {	double *N,*D,*d,*q,*r;	// vectors - N / D = q       N % D = r	int dN, dD, dd, dq, dr;	// degrees of vectors	int i;					// iterators // setting the degrees of vectors	cout << "Enter the degree of N:";	cin >> dN;	cout << "Enter the degree of D:"; 	cin >> dD;	dq = dN-dD;  	dr = dN-dD;  // allocation and initialization of vectors	N=new double [dN+1];						cout << "Enter the coefficients of N:"<<endl;  	for ( i = 0; i < dN+1; i++ ) {		cout << "N[" << i << "]= " << endl;		cin >> N[i];	} 	D=new double [dN+1];	cout << "Enter the coefficients of D:"<<endl;		for ( i = 0; i < dD+1; i++ ) {		cout << "D[" << i << "]= " << endl;		cin >> D[i];	} 	d=new double [dN+1];	for( i = dD+1 ; i < dN+1; i++ ) {		D[i] = 0;	} 	q=new double [dq+1];	for( i = 0 ; i < dq + 1 ; i++ ) {		q[i] = 0;	} 	r=new double [dr+1];	for( i = 0 ; i < dr + 1 ; i++ ) {		r[i] = 0;	} 	if( dD < 0) {		cout << "Degree of D is less than zero. Error!";	} 	cout << "-- Procedure --" << endl << endl;	if( dN >= dD ) {		while(dN >= dD) {// d equals D shifted right			for( i = 0 ; i < dN + 1 ; i++ ) {				d[i] = 0;			}			for( i = 0 ; i < dD + 1 ; i++ ) {				d[i+dN-dD] = D[i];			}			dd = dN; 			Print( 'd', dd, d ); // calculating one element of q			q[dN-dD] = N[dN]/d[dd]; 			Print( 'q', dq, q ); // d equals d * q[dN-dD]			for( i = 0 ; i < dq + 1 ; i++ ) {				d[i] = d[i] * q[dN-dD];			} 			Print( 'd', dd, d ); // N equals N - d			for( i = 0 ; i < dN + 1 ; i++ ) {				N[i] = N[i] - d[i];			}			dN--; 			Print( 'N', dN, N );			cout << "-----------------------" << endl << endl; 		} 	} // r equals N 	for( i = 0 ; i < dN + 1 ; i++ ) {		r[i] = N[i];	}	dr = dN; 	cout << "=========================" << endl << endl;	cout << "-- Result --" << endl << endl; 	Print( 'q', dq, q );	Print( 'r', dr, r ); // dealocation	delete [] N;	delete [] D;	delete [] d;	delete [] q;	delete [] r;}

## C#

Translation of: Java
using System; namespace PolynomialLongDivision {    class Solution {        public Solution(double[] q, double[] r) {            Quotient = q;            Remainder = r;        }         public double[] Quotient { get; }        public double[] Remainder { get; }    }     class Program {        static int PolyDegree(double[] p) {            for (int i = p.Length - 1; i >= 0; --i) {                if (p[i] != 0.0) return i;            }            return int.MinValue;        }         static double[] PolyShiftRight(double[] p, int places) {            if (places <= 0) return p;            int pd = PolyDegree(p);            if (pd + places >= p.Length) {                throw new ArgumentOutOfRangeException("The number of places to be shifted is too large");            }            double[] d = new double[p.Length];            p.CopyTo(d, 0);            for (int i = pd; i >= 0; --i) {                d[i + places] = d[i];                d[i] = 0.0;            }            return d;        }         static void PolyMultiply(double[] p, double m) {            for (int i = 0; i < p.Length; ++i) {                p[i] *= m;            }        }         static void PolySubtract(double[] p, double[] s) {            for (int i = 0; i < p.Length; ++i) {                p[i] -= s[i];            }        }         static Solution PolyLongDiv(double[] n, double[] d) {            if (n.Length != d.Length) {                throw new ArgumentException("Numerator and denominator vectors must have the same size");            }            int nd = PolyDegree(n);            int dd = PolyDegree(d);            if (dd < 0) {                throw new ArgumentException("Divisor must have at least one one-zero coefficient");            }            if (nd < dd) {                throw new ArgumentException("The degree of the divisor cannot exceed that of the numerator");            }            double[] n2 = new double[n.Length];            n.CopyTo(n2, 0);            double[] q = new double[n.Length];            while (nd >= dd) {                double[] d2 = PolyShiftRight(d, nd - dd);                q[nd - dd] = n2[nd] / d2[nd];                PolyMultiply(d2, q[nd - dd]);                PolySubtract(n2, d2);                nd = PolyDegree(n2);            }            return new Solution(q, n2);        }         static void PolyShow(double[] p) {            int pd = PolyDegree(p);            for (int i = pd; i >= 0; --i) {                double coeff = p[i];                if (coeff == 0.0) continue;                if (coeff == 1.0) {                    if (i < pd) {                        Console.Write(" + ");                    }                } else if (coeff == -1.0) {                    if (i < pd) {                        Console.Write(" - ");                    } else {                        Console.Write("-");                    }                } else if (coeff < 0.0) {                    if (i < pd) {                        Console.Write(" - {0:F1}", -coeff);                    } else {                        Console.Write("{0:F1}", coeff);                    }                } else {                    if (i < pd) {                        Console.Write(" + {0:F1}", coeff);                    } else {                        Console.Write("{0:F1}", coeff);                    }                }                if (i > 1) Console.Write("x^{0}", i);                else if (i == 1) Console.Write("x");            }            Console.WriteLine();        }         static void Main(string[] args) {            double[] n = { -42.0, 0.0, -12.0, 1.0 };            double[] d = { -3.0, 1.0, 0.0, 0.0 };            Console.Write("Numerator   : ");            PolyShow(n);            Console.Write("Denominator : ");            PolyShow(d);            Console.WriteLine("-------------------------------------");            Solution sol = PolyLongDiv(n, d);            Console.Write("Quotient    : ");            PolyShow(sol.Quotient);            Console.Write("Remainder   : ");            PolyShow(sol.Remainder);        }    }}
Output:
Numerator   : x^3 - 12.0x^2 - 42.0
Denominator : x - 3.0
-------------------------------------
Quotient    : x^2 - 9.0x - 27.0
Remainder   : -123.0

## Clojure

This example performs multivariate polynomial division using Buchberger's algorithm to decompose a polynomial into its Gröbner bases. Polynomials are represented as hash-maps of monomials with tuples of exponents as keys and their corresponding coefficients as values: e.g. 2xy + 3x + 5y + 7 is represented as {[1 1] 2, [1 0] 3, [0 1] 5, [0 0] 7}.

Since this algorithm is much more efficient when the input is in graded reverse lexicographic (grevlex) order a comparator is included to be used with Clojure's sorted-map—(into (sorted-map-by grevlex) ...)—as well as necessary functions to compute polynomial multiplication, monomial complements, and S-polynomials.

(defn grevlex [term1 term2]  (let [grade1 (reduce +' term1)        grade2 (reduce +' term2)        comp (- grade2 grade1)] ;; total degree    (if (not= 0 comp)      comp      (loop [term1 term1             term2 term2]        (if (empty? term1)          0          (let [grade1 (last term1)                grade2 (last term2)                comp (- grade1 grade2)] ;; differs from grlex because terms are flipped from above            (if (not= 0 comp)            comp            (recur (pop term1)                   (pop term2))))))))) (defn mul  ;; transducer  ([poly1]  ;; completion   (fn     ([] poly1)     ([poly2] (mul poly1 poly2))     ([poly2 & more] (mul poly1 poly2 more))))  ([poly1 poly2]   (let [product (atom (transient (sorted-map-by grevlex)))]     (doall  ;; for is lazy so must to be forced for side-effects       (for [term1 poly1            term2 poly2            :let [vars (mapv +' (key term1) (key term2))                  coeff (* (val term1) (val term2))]]        (if (contains? @product vars)          (swap! product assoc! vars (+ (get @product vars) coeff))          (swap! product assoc! vars coeff))))     (->> product          (deref)          (persistent!)          (denull))))  ([poly1 poly2 & more]   (reduce mul (mul poly1 poly2) more))) (defn compl [term1 term2]   (map (fn [x y]         (cond           (and (zero? x) (not= 0 y)) nil           (< x y) nil           (>= x y) (- x y)))       term1       term2)) (defn s-poly [f g]  (let [f-vars (first f)        g-vars (first g)        lcm (compl f-vars g-vars)]    (if (not-any? nil? lcm)      {(vec lcm)       (/ (second f) (second g))}))) (defn divide [f g]  (loop [f f         g g         result (transient {})         remainder {}]    (if (empty? f)      (list (persistent! result)            (->> remainder                 (filter #(not (nil? %)))                 (into (sorted-map-by grevlex))))      (let [term1 (first f)            term2 (first g)            s-term (s-poly term1 term2)]        (if (nil? s-term)          (recur (dissoc f (first term1))                 (dissoc g (first term2))                 result                 (conj remainder term1))          (recur (sub f (mul g s-term))                 g                 (conj! result s-term)                 remainder)))))) (deftest divide-tests  (is (= (divide {[1 1] 2, [1 0] 3, [0 1] 5, [0 0] 7}                 {[1 1] 2, [1 0] 3, [0 1] 5, [0 0] 7})         '({[0 0] 1} {})))  (is (= (divide {[1 1] 2, [1 0] 3, [0 1] 5, [0 0] 7}                 {[0 0] 1})         '({[1 1] 2, [1 0] 3, [0 1] 5, [0 0] 7} {})))  (is (= (divide {[1 1] 2, [1 0] 10, [0 1] 3, [0 0] 15}                 {[0 1] 1, [0 0] 5})         '({[1 0] 2, [0 0] 3} {})))  (is (= (divide {[1 1] 2, [1 0] 10, [0 1] 3, [0 0] 15}                 {[1 0] 2, [0 0] 3})         '({[0 1] 1, [0 0] 5} {}))))

## Common Lisp

Polynomials are represented as lists of degree/coefficient pairs ordered by degree (highest degree first), and pairs with zero coefficients can be omitted. Multiply and divide perform long multiplication and long division, respectively. multiply returns one value, the product, and divide returns two, the quotient and the remainder.

(defun add (p1 p2)  (do ((sum '())) ((and (endp p1) (endp p2)) (nreverse sum))    (let ((pd1 (if (endp p1) -1 (caar p1)))          (pd2 (if (endp p2) -1 (caar p2))))      (multiple-value-bind (c1 c2)          (cond           ((> pd1 pd2) (values (cdr (pop p1)) 0))           ((< pd1 pd2) (values 0 (cdr (pop p2))))           (t  (values (cdr (pop p1)) (cdr (pop p2)))))        (let ((csum (+ c1 c2)))          (unless (zerop csum)            (setf sum (acons (max pd1 pd2) csum sum)))))))) (defun multiply (p1 p2)  (flet ((*p2 (p)           (destructuring-bind (d . c) p             (loop for (pd . pc) in p2                   collecting (cons (+ d pd) (* c pc))))))    (reduce 'add (mapcar #'*p2 p1) :initial-value '()))) (defun subtract (p1 p2)  (add p1 (multiply '((0 . -1)) p2))) (defun divide (dividend divisor &aux (sum '()))  (assert (not (endp divisor)) (divisor)    'division-by-zero    :operation 'divide    :operands (list dividend divisor))  (flet ((floor1 (dividend divisor)           (if (endp dividend) (values '() ())             (destructuring-bind (d1 . c1) (first dividend)               (destructuring-bind (d2 . c2) (first divisor)                 (if (> d2 d1) (values '() dividend)                   (let* ((quot (list (cons (- d1 d2) (/ c1 c2))))                          (rem (subtract dividend (multiply divisor quot))))                     (values quot rem))))))))    (loop (multiple-value-bind (quotient remainder)              (floor1 dividend divisor)            (if (endp quotient) (return (values sum remainder))              (setf dividend remainder                    sum (add quotient sum)))))))
> (divide '((3 . 1) (2 . -12) (0 . -42)) ; x^3 - 12x^2 - 42          '((1 . 1) (0 . -3)))           ; x - 3((2 . 1) (1 . -9) (0 . -27)) ; x^2 - 9x - 27((0 . -123))                 ; -123

## D

import std.stdio, std.range, std.algorithm, std.typecons, std.conv; Tuple!(double[], double[]) polyDiv(in double[] inN, in double[] inD)nothrow pure @safe {    // Code smell: a function that does two things.    static int trimAndDegree(T)(ref T[] poly) nothrow pure @safe @nogc {        poly = poly.retro.find!q{ a != b }(0.0).retro;        return poly.length.signed - 1;    }     auto N = inN.dup;    const(double)[] D = inD;    const dD = trimAndDegree(D);    auto dN = trimAndDegree(N);    double[] q;    if (dD < 0)        throw new Error("ZeroDivisionError");    if (dN >= dD) {        q = [0.0].replicate(dN);        while (dN >= dD) {            auto d = [0.0].replicate(dN - dD) ~ D;            immutable mult = q[dN - dD] = N[$- 1] / d[$ - 1];            d[] *= mult;            N[] -= d[];            dN = trimAndDegree(N);        }    } else        q = [0.0];    return tuple(q, N);}  int trimAndDegree1(T)(ref T[] poly) nothrow pure @safe @nogc {    poly.length -= poly.retro.countUntil!q{ a != 0 };    return poly.length.signed - 1;} void main() {    immutable N = [-42.0, 0.0, -12.0, 1.0];    immutable D = [-3.0, 1.0, 0.0, 0.0];    writefln("%s / %s = %s  remainder %s", N, D, polyDiv(N, D)[]);}
Output:
[-42, 0, -12, 1] / [-3, 1, 0, 0] = [-27, -9, 1]  remainder [-123]

## E

 Some lines in this example are too long (more than 80 characters). Please fix the code if it's possible and remove this message.

This program has some unnecessary features contributing to its length:

• It creates polynomial objects rather than performing its operations directly on arrays.
• It includes code for printing polynomials nicely.
• It prints the intermediate steps of the division.
pragma.syntax("0.9")
pragma.enable("accumulator")

def superscript(x, out) {
if (x >= 10) { superscript(x // 10) }
out.print("⁰¹²³⁴⁵⁶⁷⁸⁹"[x %% 10])
}

def makePolynomial(initCoeffs :List) {
def degree := {
var i := initCoeffs.size() - 1
while (i >= 0 && initCoeffs[i] <=> 0) { i -= 1 }
if (i < 0) { -Infinity } else { i }
}
def coeffs := initCoeffs(0, if (degree == -Infinity) { [] } else { degree + 1 })

def polynomial {
/** Print the polynomial (not necessary for the task) */
to __printOn(out) {
out.print("(λx.")
var first := true
for i in (0..!(coeffs.size())).descending() {
def coeff := coeffs[i]
if (coeff <=> 0) { continue }
out.print(" ")
if (coeff <=> 1 && !(i <=> 0)) {
# no coefficient written if it's 1 and not the constant term
} else if (first) {      out.print(coeff)
} else if (coeff > 0) {  out.print("+ ", coeff)
} else {                 out.print("- ", -coeff)
}
if (i <=> 0) {         # no x if it's the constant term
} else if (i <=> 1) {  out.print("x")
} else {               out.print("x"); superscript(i, out)
}
first := false
}
out.print(")")
}

/** Evaluate the polynomial (not necessary for the task) */
to run(x) {
return accum 0 for i => c in coeffs { _ + c * x**i }
}

to degree() { return degree }
to coeffs() { return coeffs }
to highestCoeff() { return coeffs[degree] }

/** Could support another polynomial, but not part of this task.
Computes this * x**power. */
to timesXToThe(power) {
return makePolynomial([0] * power + coeffs)
}

/** Multiply (by a scalar only). */
to multiply(scalar) {
return makePolynomial(accum [] for x in coeffs { _.with(x * scalar) })
}

/** Subtract (by another polynomial only). */
to subtract(other) {
def oc := other.coeffs() :List
return makePolynomial(accum [] for i in 0..(coeffs.size().max(oc.size())) { _.with(coeffs.fetch(i, fn{0}) - oc.fetch(i, fn{0})) })
}

/** Polynomial long division. */
to quotRem(denominator, trace) {
var numerator := polynomial
require(denominator.degree() >= 0)
if (numerator.degree() < denominator.degree()) {
return [makePolynomial([]), denominator]
} else {
var quotientCoeffs := [0] * (numerator.degree() - denominator.degree())
while (numerator.degree() >= denominator.degree()) {
trace.print("  ", numerator, "\n")

def qCoeff := numerator.highestCoeff() / denominator.highestCoeff()
def qPower := numerator.degree() - denominator.degree()
quotientCoeffs with= (qPower, qCoeff)

def d := denominator.timesXToThe(qPower) * qCoeff
trace.print("- ", d,  "          (= ", denominator, " * ", qCoeff, "x"); superscript(qPower, trace); trace.print(")\n")
numerator -= d

trace.print("  -------------------------- (Quotient so far: ",  makePolynomial(quotientCoeffs), ")\n")
}
return [makePolynomial(quotientCoeffs), numerator]
}
}
}
return polynomial
}
def n := makePolynomial([-42, 0, -12, 1])def d := makePolynomial([-3, 1])println("Numerator: ", n)println("Denominator: ", d)def [q, r] := n.quotRem(d, stdout)println("Quotient: ", q)println("Remainder: ", r)

Output:

Numerator: (λx. x³ - 12x² - 42)
Denominator: (λx. x - 3)
(λx. x³ - 12x² - 42)
- (λx. x³ - 3.0x²)          (= (λx. x - 3) * 1.0x²)
-------------------------- (Quotient so far: (λx. x²))
(λx. -9.0x² - 42.0)
- (λx. -9.0x² + 27.0x)          (= (λx. x - 3) * -9.0x¹)
-------------------------- (Quotient so far: (λx. x² - 9.0x))
(λx. -27.0x - 42.0)
- (λx. -27.0x + 81.0)          (= (λx. x - 3) * -27.0x⁰)
-------------------------- (Quotient so far: (λx. x² - 9.0x - 27.0))
Quotient: (λx. x² - 9.0x - 27.0)
Remainder: (λx. -123.0)


## Elixir

Translation of: Ruby
defmodule Polynomial do  def division(_, []), do: raise ArgumentError, "denominator is zero"  def division(_, [0]), do: raise ArgumentError, "denominator is zero"  def division(f, g) when length(f) < length(g), do: {[0], f}  def division(f, g) do    {q, r} = division(g, [], f)    if q==[], do: q = [0]    if r==[], do: r = [0]    {q, r}  end   defp division(g, q, r) when length(r) < length(g), do: {q, r}  defp division(g, q, r) do    p = hd(r) / hd(g)    r2 = Enum.zip(r, g)         |> Enum.with_index         |> Enum.reduce(r, fn {{pn,pg},i},acc ->              List.replace_at(acc, i, pn - p * pg)            end)    division(g, q++[p], tl(r2))  endend [ { [1, -12, 0, -42], [1, -3] },  { [1, -12, 0, -42], [1, 1, -3] },  { [1, 3, 2],        [1, 1] },  { [1, -4, 6, 5, 3], [1, 2, 1] } ]|> Enum.each(fn {f,g} ->     {q, r} = Polynomial.division(f, g)     IO.puts "#{inspect f} / #{inspect g} => #{inspect q} remainder #{inspect r}"   end)
Output:
[1, -12, 0, -42] / [1, -3] => [1.0, -9.0, -27.0] remainder [-123.0]
[1, -12, 0, -42] / [1, 1, -3] => [1.0, -13.0] remainder [16.0, -81.0]
[1, 3, 2] / [1, 1] => [1.0, 2.0] remainder [0.0]
[1, -4, 6, 5, 3] / [1, 2, 1] => [1.0, -6.0, 17.0] remainder [-23.0, -14.0]


## Factor

USE: math.polynomials { -42 0 -12 1 } { -3 1 } p/mod ptrim [ . ] [email protected]
Output:
V{ -27 -9 1 }
V{ -123 }


## Fortran

Works with: Fortran version 95 and later
module Polynom  implicit none contains   subroutine poly_long_div(n, d, q, r)    real, dimension(:), intent(in) :: n, d    real, dimension(:), intent(out), allocatable :: q    real, dimension(:), intent(out), allocatable, optional :: r     real, dimension(:), allocatable :: nt, dt, rt    integer :: gn, gt, gd     if ( (size(n) >= size(d)) .and. (size(d) > 0) .and. (size(n) > 0) ) then         allocate(nt(size(n)), dt(size(n)), rt(size(n)))        nt = n       dt = 0       dt(1:size(d)) = d       rt = 0       gn = size(n)-1       gd = size(d)-1       gt = 0        do while ( d(gd+1) == 0 )          gd = gd - 1       end do        do while( gn >= gd )          dt = eoshift(dt, -(gn-gd))          rt(gn-gd+1) = nt(gn+1) / dt(gn+1)          nt = nt - dt * rt(gn-gd+1)          gt = max(gt, gn-gd)          do             gn = gn - 1             if ( nt(gn+1) /= 0 ) exit          end do          dt = 0          dt(1:size(d)) = d       end do        allocate(q(gt+1))       q = rt(1:gt+1)       if ( present(r) ) then          if ( (gn+1) > 0 ) then             allocate(r(gn+1))             r = nt(1:gn+1)          else             allocate(r(1))             r = 0.0          end if       end if       deallocate(nt, dt, rt)    else       allocate(q(1))       q = 0       if ( present(r) ) then          allocate(r(size(n)))          r = n       end if    end if   end subroutine poly_long_div   subroutine poly_print(p)    real, dimension(:), intent(in) :: p     integer :: i     do i = size(p), 1, -1       if ( i > 1 ) then          write(*, '(F0.2,"x^",I0," + ")', advance="no") p(i), i-1       else          write(*, '(F0.2)') p(i)       end if    end do   end subroutine poly_print end module Polynom
program PolyDivTest  use Polynom  implicit none   real, dimension(:), allocatable :: q  real, dimension(:), allocatable :: r   !! three tests from Wikipedia, plus an extra  !call poly_long_div( (/ -3., 1. /), (/ -42., 0.0, -12., 1. /), q, r)  call poly_long_div( (/ -42., 0.0, -12., 1. /), (/ -3., 1. /), q, r)  !call poly_long_div( (/ -42., 0.0, -12., 1. /), (/ -3., 1., 1. /), q, r)  !call poly_long_div( (/ 2., 3., 1. /), (/ 1., 1. /), q, r)   call poly_print(q)  call poly_print(r)  deallocate(q, r) end program PolyDivTest

## Go

By the convention and pseudocode given in the task:

package main import "fmt" func main() {    n := []float64{-42, 0, -12, 1}    d := []float64{-3, 1}    fmt.Println("N:", n)    fmt.Println("D:", d)    q, r, ok := pld(n, d)    if ok {        fmt.Println("Q:", q)        fmt.Println("R:", r)    } else {        fmt.Println("error")    }} func degree(p []float64) int {    for d := len(p) - 1; d >= 0; d-- {        if p[d] != 0 {            return d        }    }    return -1} func pld(nn, dd []float64) (q, r []float64, ok bool) {    if degree(dd) < 0 {        return    }    nn = append(r, nn...)    if degree(nn) >= degree(dd) {        q = make([]float64, degree(nn)-degree(dd)+1)        for degree(nn) >= degree(dd) {            d := make([]float64, degree(nn)+1)            copy(d[degree(nn)-degree(dd):], dd)            q[degree(nn)-degree(dd)] = nn[degree(nn)] / d[degree(d)]            for i := range d {                d[i] *= q[degree(nn)-degree(dd)]                nn[i] -= d[i]            }        }    }    return q, nn, true}

Output:

N: [-42 0 -12 1]
D: [-3 1]
Q: [-27 -9 1]
R: [-123 0 0 0]


## GAP

GAP has built-in functions for computations with polynomials.

x := Indeterminate(Rationals, "x");p := x^11 + 3*x^8 + 7*x^2 + 3;q := x^7 + 5*x^3 + 1;QuotientRemainder(p, q);# [ x^4+3*x-5, -16*x^4+25*x^3+7*x^2-3*x+8 ]

## Haskell

Translated from the OCaml code elsewhere on the page.

Works with: GHC version 6.10
import Data.List shift n l = l ++ replicate n 0 pad n l = replicate n 0 ++ l norm :: Fractional a => [a] -> [a]norm = dropWhile (== 0) deg l = length (norm l) - 1 zipWith' op p q = zipWith op (pad (-d) p) (pad d q)  where d = (length p) - (length q) polydiv f g = aux (norm f) (norm g) []  where aux f s q | ddif < 0 = (q, f)                  | otherwise = aux f' s q'           where ddif = (deg f) - (deg s)                 k = (head f) / (head s)                 ks = map (* k) $shift ddif s q' = zipWith' (+) q$ shift ddif [k]                 f' = norm $tail$ zipWith' (-) f ks

And this is the also-translated pretty printing function.

str_poly l = intercalate " + " $terms l where term v 0 = show v term 1 1 = "x" term v 1 = (show v) ++ "x" term 1 p = "x^" ++ (show p) term v p = (show v) ++ "x^" ++ (show p) terms :: Fractional a => [a] -> [String] terms [] = [] terms (0:t) = terms t terms (h:t) = (term h (length t)) : (terms t) ## J divmod=:[: (}: ; {:) ([ (] -/@,:&}. (* {:)) ] , %&{.~)^:(>:@-~&#)&.|.~ Wikipedia example: _42 0 _12 1 divmod _3 1 This produces the result: ┌────────┬────┐ │_27 _9 1│_123│ └────────┴────┘  This means that ${\displaystyle -42-12x^{2}+x^{3}}$ divided by ${\displaystyle -3+x}$ produces ${\displaystyle -27-9x+x^{2}}$ with a remainder of ${\displaystyle -123}$. ## Java Translation of: Kotlin import java.util.Arrays; public class PolynomialLongDivision { private static class Solution { double[] quotient, remainder; Solution(double[] q, double[] r) { this.quotient = q; this.remainder = r; } } private static int polyDegree(double[] p) { for (int i = p.length - 1; i >= 0; --i) { if (p[i] != 0.0) return i; } return Integer.MIN_VALUE; } private static double[] polyShiftRight(double[] p, int places) { if (places <= 0) return p; int pd = polyDegree(p); if (pd + places >= p.length) { throw new IllegalArgumentException("The number of places to be shifted is too large"); } double[] d = Arrays.copyOf(p, p.length); for (int i = pd; i >= 0; --i) { d[i + places] = d[i]; d[i] = 0.0; } return d; } private static void polyMultiply(double[] p, double m) { for (int i = 0; i < p.length; ++i) { p[i] *= m; } } private static void polySubtract(double[] p, double[] s) { for (int i = 0; i < p.length; ++i) { p[i] -= s[i]; } } private static Solution polyLongDiv(double[] n, double[] d) { if (n.length != d.length) { throw new IllegalArgumentException("Numerator and denominator vectors must have the same size"); } int nd = polyDegree(n); int dd = polyDegree(d); if (dd < 0) { throw new IllegalArgumentException("Divisor must have at least one one-zero coefficient"); } if (nd < dd) { throw new IllegalArgumentException("The degree of the divisor cannot exceed that of the numerator"); } double[] n2 = Arrays.copyOf(n, n.length); double[] q = new double[n.length]; while (nd >= dd) { double[] d2 = polyShiftRight(d, nd - dd); q[nd - dd] = n2[nd] / d2[nd]; polyMultiply(d2, q[nd - dd]); polySubtract(n2, d2); nd = polyDegree(n2); } return new Solution(q, n2); } private static void polyShow(double[] p) { int pd = polyDegree(p); for (int i = pd; i >= 0; --i) { double coeff = p[i]; if (coeff == 0.0) continue; if (coeff == 1.0) { if (i < pd) { System.out.print(" + "); } } else if (coeff == -1.0) { if (i < pd) { System.out.print(" - "); } else { System.out.print("-"); } } else if (coeff < 0.0) { if (i < pd) { System.out.printf(" - %.1f", -coeff); } else { System.out.print(coeff); } } else { if (i < pd) { System.out.printf(" + %.1f", coeff); } else { System.out.print(coeff); } } if (i > 1) System.out.printf("x^%d", i); else if (i == 1) System.out.print("x"); } System.out.println(); } public static void main(String[] args) { double[] n = new double[]{-42.0, 0.0, -12.0, 1.0}; double[] d = new double[]{-3.0, 1.0, 0.0, 0.0}; System.out.print("Numerator : "); polyShow(n); System.out.print("Denominator : "); polyShow(d); System.out.println("-------------------------------------"); Solution sol = polyLongDiv(n, d); System.out.print("Quotient : "); polyShow(sol.quotient); System.out.print("Remainder : "); polyShow(sol.remainder); }} Output: Numerator : x^3 - 12.0x^2 - 42.0 Denominator : x - 3.0 ------------------------------------- Quotient : x^2 - 9.0x - 27.0 Remainder : -123.0 ## Julia This task is straightforward with the help of Julia's Polynomials package.  using Polynomials p = Poly([-42,0,-12,1])q = Poly([-3,1]) d, r = divrem(p,q) println(p, " divided by ", q, " is ", d, " with remainder ", r, ".")  Output: -42 - 12x^2 + x^3 divided by -3 + x is -27.0 - 9.0x + x^2 with remainder -123.0.  ## Kotlin // version 1.1.51 typealias IAE = IllegalArgumentException data class Solution(val quotient: DoubleArray, val remainder: DoubleArray) fun polyDegree(p: DoubleArray): Int { for (i in p.size - 1 downTo 0) { if (p[i] != 0.0) return i } return Int.MIN_VALUE} fun polyShiftRight(p: DoubleArray, places: Int): DoubleArray { if (places <= 0) return p val pd = polyDegree(p) if (pd + places >= p.size) { throw IAE("The number of places to be shifted is too large") } val d = p.copyOf() for (i in pd downTo 0) { d[i + places] = d[i] d[i] = 0.0 } return d} fun polyMultiply(p: DoubleArray, m: Double) { for (i in 0 until p.size) p[i] *= m} fun polySubtract(p: DoubleArray, s: DoubleArray) { for (i in 0 until p.size) p[i] -= s[i]} fun polyLongDiv(n: DoubleArray, d: DoubleArray): Solution { if (n.size != d.size) { throw IAE("Numerator and denominator vectors must have the same size") } var nd = polyDegree(n) val dd = polyDegree(d) if (dd < 0) { throw IAE("Divisor must have at least one one-zero coefficient") } if (nd < dd) { throw IAE("The degree of the divisor cannot exceed that of the numerator") } val n2 = n.copyOf() val q = DoubleArray(n.size) // all elements zero by default while (nd >= dd) { val d2 = polyShiftRight(d, nd - dd) q[nd - dd] = n2[nd] / d2[nd] polyMultiply(d2, q[nd - dd]) polySubtract(n2, d2) nd = polyDegree(n2) } return Solution(q, n2)} fun polyShow(p: DoubleArray) { val pd = polyDegree(p) for (i in pd downTo 0) { val coeff = p[i] if (coeff == 0.0) continue print (when { coeff == 1.0 -> if (i < pd) " + " else "" coeff == -1.0 -> if (i < pd) " - " else "-" coeff < 0.0 -> if (i < pd) " -${-coeff}" else "$coeff" else -> if (i < pd) " +$coeff" else "$coeff" }) if (i > 1) print("x^$i")        else if (i == 1) print("x")    }    println()} fun main(args: Array<String>) {    val n = doubleArrayOf(-42.0, 0.0, -12.0, 1.0)    val d = doubleArrayOf( -3.0, 1.0,   0.0, 0.0)    print("Numerator   : ")    polyShow(n)    print("Denominator : ")    polyShow(d)    println("-------------------------------------")    val (q, r) = polyLongDiv(n, d)    print("Quotient    : ")    polyShow(q)    print("Remainder   : ")    polyShow(r)}
Output:
Numerator   : x^3 - 12.0x^2 - 42.0
Denominator : x - 3.0
-------------------------------------
Quotient    : x^2 - 9.0x - 27.0
Remainder   : -123.0


## Maple

As Maple is a symbolic computation system, polynomial arithmetic is, of course, provided by the language runtime. The remainder (rem) and quotient (quo) operations each allow for the other to be computed simultaneously by passing an unassigned name as an optional fourth argument. Since rem and quo deal also with multivariate polynomials, the indeterminate is passed as the third argument.

 > p := randpoly( x ); # pick a random polynomial in x                           5       4       3       2             p := -56 - 7 x  + 22 x  - 55 x  - 94 x  + 87 x > rem( p, x^2 + 2, x, 'q' ); # remainder                              220 + 169 x > q; # quotient                           3       2                       -7 x  + 22 x  - 41 x - 138 > quo( p, x^2 + 2, x, 'r' ); # quotient                           3       2                       -7 x  + 22 x  - 41 x - 138 > r; # remainder                              220 + 169 x> expand( (x^2+2)*q + r - p ); # check                                   0

## Mathematica

PolynomialQuotientRemainder[x^3-12 x^2-42,x-3,x]

output:

{-27 - 9 x + x^2, -123}

## OCaml

First define some utility operations on polynomials as lists (with highest power coefficient first).

let rec shift n l = if n <= 0 then l else shift (pred n) (l @ [0.0])let rec pad n l = if n <= 0 then l else pad (pred n) (0.0 :: l)let rec norm = function | 0.0 :: tl -> norm tl | x -> xlet deg l = List.length (norm l) - 1 let zip op p q =  let d = (List.length p) - (List.length q) in  List.map2 op (pad (-d) p) (pad d q)

Then the main polynomial division function

let polydiv f g =  let rec aux f s q =    let ddif = (deg f) - (deg s) in    if ddif < 0 then (q, f) else      let k = (List.hd f) /. (List.hd s) in      let ks = List.map (( *.) k) (shift ddif s) in      let q' = zip (+.) q (shift ddif [k])      and f' = norm (List.tl (zip (-.) f ks)) in      aux f' s q' in  aux (norm f) (norm g) []

For output we need a pretty-printing function

let str_poly l =  let term v p = match (v, p) with    | (  _, 0) -> string_of_float v    | (1.0, 1) -> "x"    | (  _, 1) -> (string_of_float v) ^ "*x"    | (1.0, _) -> "x^" ^ (string_of_int p)    | _ -> (string_of_float v) ^ "*x^" ^ (string_of_int p) in  let rec terms = function    | [] -> []    | h :: t ->       if h = 0.0 then (terms t) else (term h (List.length t)) :: (terms t) in  String.concat " + " (terms l)

and then the example

let _ =  let f = [1.0; -4.0; 6.0; 5.0; 3.0] and g = [1.0; 2.0; 1.0] in  let q, r = polydiv f g in  Printf.printf    " (%s) div (%s)\ngives\nquotient:\t(%s)\nremainder:\t(%s)\n"    (str_poly f) (str_poly g) (str_poly q) (str_poly r)

gives the output:

 (x^4 + -4.*x^3 + 6.*x^2 + 5.*x + 3.) div (x^2 + 2.*x + 1.)
gives
quotient:	(x^2 + -6.*x + 17.)
remainder:	(-23.*x + -14.)


## Octave

Octave has already facilities to divide two polynomials (deconv(n,d)); and the reason to adopt the convention of keeping the highest power coefficient first, is to make the code compatible with builtin functions: we can use polyout to output the result.

function [q, r] = poly_long_div(n, d)  gd = length(d);  pv = zeros(1, length(n));  pv(1:gd) = d;  if ( length(n) >= gd )    q = [];    while ( length(n) >= gd )      q = [q, n(1)/pv(1)];      n = n - pv .* (n(1)/pv(1));      n = shift(n, -1);           %       tn = n(1:length(n)-1);      % eat the higher power term      n = tn;                     %      tp = pv(1:length(pv)-1);      pv = tp;                    % make pv the same length of n    endwhile    r = n;  else    q = [0];    r = n;  endifendfunction [q, r] = poly_long_div([1,-12,0,-42], [1,-3]);polyout(q, 'x');polyout(r, 'x');disp("");[q, r] = poly_long_div([1,-12,0,-42], [1,1,-3]);polyout(q, 'x');polyout(r, 'x');disp("");[q, r] = poly_long_div([1,3,2], [1,1]);polyout(q, 'x');polyout(r, 'x');disp("");[q, r] = poly_long_div([1,3], [1,-12,0,-42]);polyout(q, 'x');polyout(r, 'x');

## PARI/GP

This uses the built-in PARI polynomials.

poldiv(a,b)={  my(rem=a%b);  [(a - rem)/b, rem]};poldiv(x^9+1, x^3+x-3)

Alternately, use the built-in function divrem:

divrem(x^9+1, x^3+x-3)~

## Perl

This solution keeps the highest power coefficient first, like OCaml solution and Octave solution.

Translation of: Octave
use strict;use List::Util qw(min); sub poly_long_div{    my ($rn,$rd) = @_;     my @n = @$rn; my$gd = scalar(@$rd); if ( scalar(@n) >=$gd ) {	my @q = ();	while ( scalar(@n) >= $gd ) { my$piv = $n[0]/$rd->[0];	    push @q, $piv;$n[$_] -=$rd->[$_] *$piv foreach ( 0 .. min(scalar(@n), $gd)-1 ); shift @n; } return ( \@q, \@n ); } else { return ( [0],$rn );    }}

## Ursala

The input is a pair of lists of coefficients in order of increasing degree. Trailing zeros can be omitted. The output is a pair of lists (q,r), the quotient and remainder polynomial coefficients. This is a straightforward implementation of the algorithm in terms of list operations (fold, zip, map, distribute, etc.) instead of array indexing, hence not unnecessarily verbose.

#import std#import flo polydiv = zeroid~-l~~; leql?rlX\~&NlX ^H\(@rNrNSPXlHDlS |\ :/0.) @NlX //=> ?(   @lrrPX ==!| zipp0.; @x not zeroid+ [email protected]>hr ~&t,   (^lryPX/~&lrrl2C minus^*p/~&rrr times*lrlPD)^/[email protected] ~&,   @r ^|\~& ~&i&& :/0.)

test program:

#cast %eLW example = polydiv(<-42.,0.,-12.,1.>,<-3.,1.,0.,0.>)

output:

(
<-2.700000e+01,-9.000000e+00,1.000000e+00>,
<-1.230000e+02>)

## zkl

fcn polyLongDivision(a,b){  // (a0 + a1x + a2x^2 + a3x^3 ...)   _assert_(degree(b)>=0,"degree(%s) < 0".fmt(b));   q:=List.createLong(a.len(),0.0);   while((ad:=degree(a)) >= (bd:=degree(b))){      z,d,m := ad-bd, List.createLong(z,0.0).extend(b), a[ad]/b[bd];;      q[z]=m;      d,a = d.apply('*(m)), a.zipWith('-,d);   }   return(q,a);		// may have trailing zero elements}fcn degree(v){  // -1,0,..len(v)-1, -1 if v==0   v.len() - v.copy().reverse().filter1n('!=(0)) - 1;}fcn polyString(terms){ // (a0,a1,a2...)-->"a0 + a1x + a2x^2 ..."   str:=[0..].zipWith('wrap(n,a){ if(a) "+ %sx^%s ".fmt(a,n) else "" },terms)   .pump(String)   .replace("x^0 "," ").replace(" 1x"," x").replace("x^1 ","x ")   .replace("+ -", "- ");   if(not str)     return(" ");  // all zeros   if(str[0]=="+") str[1,*];     // leave leading space   else            String("-",str[2,*]);}
q,r:=polyLongDivision(T(-42.0, 0.0, -12.0, 1.0),T(-3.0, 1.0));println("Quotient  = ",polyString(q));println("Remainder = ",polyString(r));
Output:
Quotient  = -27 - 9x + x^2
Remainder = -123