≡ ▼
=L_POLYDIV(a, b)

ArgumentDescriptionExample
aThe dividend: A polynomial defined by coefficients in descending order of power
bThe divisor: A polynomial defined by coefficients in descending order of power

## Description

Long division of two polynomials $$a(x) / b(x)$$ when $$b(x)\ne0$$ results in a polynomial $$q(x)$$ and a remainder polynomial $$r(x)$$ such that $$a(x)=b(x)q(x)+r(x)$$. The L_POLYDIV function returns polynomial q(x) stacked vertically on top of polynomial r(x).

The algorithm used in the L_POLYDIV function is based on polynomial long division, which you can read about on wikipedia.

### Example:

$$4x^5 + 8x^4 + 7x^3 + 5x + 8 \over 2x^2 + 3x + 4$$ $$a(x) = 4x^5 + 8x^4 + 7x^3 + 5x + 8$$ $$b(x) = 2x^2 + 3x + 4$$
=LET(
a, {4,8,7,0,5,8},
b, {2,3,4},
L_POLYDIV(a,b)
)

Result:
{2, 1, -2, 1;
0, 0, 10, 4}


The first row of the result is the quotient q(x) and the second row is the remainder r(x):

$$q(x) = 2x^3 + 1x^2 - 2x + 1$$ $$r(x) = 10x + 4$$

To check the results, we can evaluate a=bq+r as follows:

=LET(
b, {2,3,4},
q, {2,1,-2,1},
r, {10,4},
)

Result: {4,8,7,0,5,8}


## Lambda Formula

This code for using L_POLYDIV in Excel is provided under the License as part of the LAMBDA Library, but to use just this function, you may copy the following code directly into your spreadsheet.

### Code for AFE Workbook Module (Excel Labs Add-in)

/**
* Divide two polynomials using long division: a/b = q remainder r
* Returns polynomial q stacked vertically on top of polynomial r
*/
L_POLYDIV = LAMBDA(a,b,
LET(doc,"https://www.vertex42.com/lambda/polydiv.html",
na,COLUMNS(a),
nb,COLUMNS(b),
result,IF(na<nb,VSTACK(0*b,b),
REDUCE(VSTACK(0*a,a),SEQUENCE(na-nb+1,1,na-nb,-1),LAMBDA(acc,i,LET(
dividend,CHOOSEROWS(acc,2),
t,INDEX(dividend,1,na-nb-i+1)/INDEX(b,1,1),
tv,IF(i>0,HSTACK(t,SEQUENCE(1,i,0,0)),t),
q,IF(i>0,HSTACK(t*b,SEQUENCE(1,i,0,0)),t*b),
)))),
IF(na>=nb,DROP(result,0,nb-1),result)
));


### Named Function for Google Sheets

Name: L_POLYDIV
Description: Divide polynomial a by polynomial b resulting in polynomial q and remainder r
Arguments: a, b
Function:
[in the works]


## L_POLYDIV Examples

Example: Factor Out Known Roots
Two of the known roots of $$p(x) = x^4 + 3x^3 - 11x^2 - 3x + 10$$ are x=-1 and x=1. These means that both $$(x+1)$$ and $$(x-1)$$ are factors of p(x), so dividing p(x) by either of these roots using L_POLYDIV will result in a remainder of 0. Use L_POLYDIV to factor out these two roots, leaving just a 2nd degree polynomial.
Step 1: Divide p(x) by (x+1)

=LET(
a, {1,3,-11,-3,10},
b, {1,1},
L_POLYDIV(a,b)
)

Result:
{1, 2,-13, 10;
0, 0,  0,  0}

Step 2: Divide q(x) by (x-1)

=LET(
a, {1,2,-13,10},
b, {1,-1},
L_POLYDIV(a,b)
)

Result:
{1, 3, -10;
0, 0,   0}


After factoring out the two known roots, we have $$q(x)=x^2+3x-10$$. Using L_QUADRATIC, we can find the two remaining roots:

=L_QUADRATIC({1,3,-10})
Result: {2; -5}


References & Resources