﻿ Polynomial factorization calculator

Polynomial factorization calculator

This JavaScript/WebAssembly application can evaluate and factor polynomial expressions modulo a prime number or a power of a prime number. It can also evaluate and factor integer polynomials by entering zero in the modulo input box.

Integer polynomials in one variable are expressions that include a variable named x, integer numbers and the addition, subtraction and multiplication operators.

They can be expressed in the form: f(x) = f0 + f1x + f2x2 + ... + fnxn.

The number n is the degree of the polynomial. The coefficient fn is the leading coefficient and the coefficient f0 is the trailing coefficient. They can be written as deg(f), lc(f) and tc(f) respectively.

Let f(x) and g(x) be two polynomials such that deg(f) ≥ deg(g). We can define addition, subtraction and multiplication as follows:

f(x) + g(x) = h(x) means hi = fi + gi if i ≤ deg(g) and hi = fi otherwise.

f(x) − g(x) = h(x) means hi = figi if i ≤ deg(g) and hi = fi otherwise.

g(x) − f(x) = h(x) means hi = gifi if i ≤ deg(g) and hi = −fi otherwise.

f(x) ⁢ g(x) = h(x) means hi = f0gi + f1gi − 1 + ... + fig0 if i ≤ deg(g), hi = fi − deg(g)gdeg(g) + fi + 1 − deg(g)gdeg(g) − 1 + ... + fig0 otherwise.

The factorization of integer polynomials is a process to find one or more irreducible polynomials whose product is the original polynomial. An irreducible polynomial cannot be expressed as a product of two or more integer polynomials.

For example: x4 − 1 = (x2 + 1) ⁢ (x + 1) ⁢ (x − 1)

It can be shown that any integer polynomial can be factored in only one way in irreducible polynomials.

Multiplication of polynomials modulo a prime number can be performed in the usual way by multiplying the different terms of the polynomial and then adding the coefficients of the same degree. Finally the coefficients are reduced modulo that prime.

For example, the product of 3x2 + 5x + 1 and 6x2 + 4x + 3 modulo 7 is 18x4 + (30+12)x3 + (9+20+6)x2 + (15+4)x + 3 modulo 7 which equals 4x4 + 5x + 3

It can be shown that the polynomials modulo a prime can be factored into the leading coefficient and monic prime polynomials in only one way (monic polynomials have the leading coefficient equal to one.)

It can also be shown that if there are no repeated factors, the polynomial can be factored modulo a power of that prime in only one way.

Use the upper input box to enter the polynomial expression and the lower input box to enter a numerical expression for the modulus, which must be either zero or an integer number greater than 1 of the form prime number raised to an exponent. You can just evaluate the polynomial or evaluate and factor it, by pressing the corresponding button.

Example for integer polynomial factorization: to factor x30 − 1, type x^30-1 in the upper input box and 0 in the lower input box. Then press the factor button.

Example for polynomial modulo a prime factorization: copy any of the expressions below in the upper input box and type the number 211 (a prime number) in the lower input box. Then press the button "Factor expression".

• 6x^8+x^5+3
• 2*((x+6)*(x-5)+xx)^4+23x

The exponentiation symbol is not present in some mobile devices, so two asterisks ** can by typed as the exponentiation operator. So, equivalent expressions would be:

• 6x**8+x**5+3
• 2*((x+6)*(x-5)+xx)**4+23x

You can type a dot (.) and the application will replace it by "x^". This saves a lot of time in mobile devices because there is no need to switch between alphabetical and numerical keyboards for simple polynomials.

For the first example you would type:

• 6.8+.5+3

Notice that factoring large degree polynomials will take a lot of time. That's why the applet accepts polynomials of degree up to 1000.

The superscripts checkbox can be used to see the exponents in the usual superscript notation (when the checkbox is checked) or preceded by the caret character (when it is unchecked). The first option is used to see the factorization on the display or print it. The second option is used to copy the data to another application.

After the applet finishes factoring, it multiplies the factors in order to validate if they are equal to the input polynomial.

You can also enter expressions that use the following operators and parentheses:

• - for subtraction
• * for multiplication
• / for division
• % for remainder
• ^ or ** for exponentiation

for the modulus you can use the above operators and also:

• n!: factorial
• p#: primorial (product of all primes less or equal than p).
• B(n): Previous probable prime before n
• F(n): Fibonacci number Fn
• L(n): Lucas number Ln = Fn-1 + Fn+1
• N(n): Next probable prime after n
• P(n): Unrestricted Partition Number (number of decompositions of n into sums of integers without regard to order).
• Gcd(m,n): Greatest common divisor of these two integers or polynomials.
• Der(m): Derivative of polynomial.
• Modinv(m,n): inverse of m modulo n, only valid when gcd(m,n)=1.
• Modpow(m,n,r): finds mn modulo r.

You can use the prefix 0x for hexadecimal numbers, for example 0x38 is equal to 56.

Polynomials modulo a prime number

The polynomial division requires multiple modular divisions where the divisor is the leading coefficient of the divisor polynomial.

To compute the modular division a / b (mod p), first the modular multiplicative inverse c is found. This number has the property bc ≡ 1 (mod p) and it can be found using the extended Euclidean algorithm as follows:

``` function modInv(value, modulus) {   V1 ← 1; V3 ← value;   U1 ← 0; U3 ← modulus;   while V3 ≠ 0   {     QQ ← U3 / V3;     T1 ← U1 − V1 * QQ;     T3 ← U3 − V3 * QQ;     U1 ← V1; U3 ← V3;     V1 ← T1; V3 ← T3;   }   return U1; } ```

Then the division is equal to the product ac (mod p).

To minimize the number of modular divisions (which are expensive), we can multiply all coefficients of both polynomials (dividend and divisor) by the multiplicative inverse of the leading coefficient of the divisor polynomial. In this way, we will divide by a monic polynomial, that is, a polynomial whose leading coefficient equals 1. This will not change the quotient, but the remainder will need to be multiplied by the leading coefficient of the divisor polynomial.

Example: perform the division (3⁢x3 + 7⁢x2 + 5⁢x + 6) / (4⁢x2 + 3⁢x + 10) (mod 11):

First we have to find the multiplicative inverse of 4 (mod 11), which equals 3, because 4 × 3 = 12 ≡ 1 (mod 11). Multiplying all coefficients by 3 we get:

(9⁢x3 + 10⁢x2 + 4⁢x + 7) / (x2 + 9⁢x + 8) (mod 11)

Dividing the leading coefficient of the dividend polynomial over the leading coefficient of the divisor polynomial: 9⁢x3 / x2 ≡ 9⁢x (mod 11).

Now we subtract the product of what we have just found by the divisor polynomial from the dividend polynomial. We obtain:

9⁢x3 + 10⁢x2 + 4⁢x + 7 - 9⁢x(x2 + 9⁢x + 8) ≡ 6⁢x2 + 9⁢x + 7 (mod 11). Notice that 10 − 9 × 9 ≡ 6 (mod 11) and 4 − 9 × 8 (mod 11) ≡ 9 (mod 11).

Dividing the leading coefficient of the remainder polynomial we just found over the leading coefficient of the divisor polynomial: 6⁢x2 / x2 ≡ 6 (mod 11).

Now we subtract the product of what we have just found by the divisor polynomial from the remainder polynomial. We obtain:

(6⁢x2 + 9⁢x + 7) − 6⁢(x2 + 9⁢x + 8) ≡ 10x + 3 (mod 11). Notice that 9 − 6 × 9 ≡ 10 (mod 11) and 7 − 6 × 8 ≡ 3 (mod 11).

The quotient polynomial equals 9⁢x + 6 and the remainder polynomial equals 4⁢(10x + 3) ≡ 7 x + 1 (mod 11).

Integer polynomials

The division proceeds in the same way as in the previous subsection, with the difference that there is no multiplicative inverse of the leading coefficient of the divisor polynomial. So for each iteration of the algorithm, a division is required. If the divisor polynomial is not monic, sometimes the division cannot be performed. This occurs when the leading coefficient of the remainder is not multiple of the leading coefficient of the divisor.

The greatest common divisor of two polynomials is the polynomial of the highest possible degree, that divides both polynomials.

For example: gcd(x2 + 6⁢x + 5, 2⁢x2 + 13⁢x + 15) = x + 5

Polynomials modulo a prime number

The greatest common divisor can be found using the Euclidean algorithm as follows:

``` function gcdm(poly1, poly2, p) {   a ← poly1;   b ← poly2;   while b ≠ 0     t ← b;     b ← remainder(a, b) (mod p);     a ← t;   return a; } ```

Integer polynomials

While the same algorithm could be used for finding the gcd of two integer polynomials, the coefficients of the intermediate polynomials increase very quickly, so this is not useful.

There are two methods to find the gcd: the subresultant algorithm and the modular algorithm. The latter, invented by William Brown, uses gcd of polynomials modulo a prime, so this is better for our purposes.

``` function gcdp(poly1, poly2) {   c1 ← cont(poly1);   c2 ← cont(poly2);   c ← gcd(c1, c2);   a ← poly1 / c1;   b ← poly2 / c2;   h ← gcd(lc(poly1), lc(poly2));   d ← min(deg(poly1), deg(poly2));   m ← 1;   gm ← 0;   repeat   {     do     {       do       {         p ← nextPrime();       } while remainder(m*h, p) = 0;       r ← poly1 (mod p);       s ← poly2 (mod p);       gp ← gcdm(r, s, p);       if deg(gp) = 0       {         output c; stop;       }     } while deg(gp) > d;     gp ← (h mod p)/lc(gp) * gp;     if deg(gp) < d     {       m ← 1;       gm ← 0;       d ← deg(gp);     }     h ← CRA([gp, p], [gm, m]);     if h = gm     {       h ← h / cont(h);       if remainder(a, h) = 0 and remainder(b, h) = 0       {         output c*h; stop;       }     }     m ← p * m;     gm ← h;   } } ```

The content of a polynomial is the greatest common divisor of all coefficients of that polynomial. The sign of the content matches the sign of the leading coefficient. It is expressed as cont(f) in the previous algorithm.

The coefficients of the result of the Chinese Remainder Algorithm (function CRA) computed modulo mp, must be in the range −mp/2 to mp/2.

The idea behind this algorithm is to compute several gcd of the input polynomials modulo different primes. We can see that their degrees are greater or equal than the degree of the polynomial gcd.

There are three cases:

• The degree of the modular gcd is greater than the degree of previous modular gcd: the new result is discarded because it has incorrect degree.
• The degree of the modular gcd is less than the degree of previous modular gcd: the old result is discarded because it has incorrect degree. The new result is used instead.
• Both degrees are equal: the coefficients of the both gcds are merged using the Chinese Remainder Theorem. This algorithm computes g (mod mp) given gm (mod m) and gp (mod p).

The algorithm continues until the computed polynomial divides both input polynomials.

The factoring algorithm can be divided in four steps: square-free factorization, distinct degree factorization, equal degree factorization and factor lift. All steps require monic polynomials, so before starting, we will have to multiply all coefficients by the modular multiplicative inverse of the leading coefficient of the polynomial.

Square-free factorization

The next steps do not work if there are repeated factors, so the first step is to factor the polynomial in such a way that there are no repeated factors.

The derivative of the polynomial f(x) = f0 + f1x + f2x2 + ... + fnxn is f'(x) = f1 + 2⁢f2x + ... + nfnxn − 1

The recursive algorithm is:

``` function SFF(f) {   i ← 1; g ← f';   if g ≠ 0   {     c ← gcd(f, g);     w ← f / c;     while w ≠ 1     {       y ← gcd(w, c); z ← w / y;       Output(zi); i ← i + 1;       w ← y; c ← c / y;     }     if c ≠ 1     {       c ← c1/p;       Output(SFF(c)p);     }   }   else   {     f ← f1/p;     Output(SFF(f)p);   } } ```

For each polynomial on the output of this algorithm we have to perform the next steps.

Distinct degree factorization

This is a method that exploits the fact that the product of all monic irreducible polynomials of degrees that divide d (mod p) equals xex where e = pd.

``` function DDF(f, p) {   i ← 1;   h ← f;   j ← x;   q ← 0;   while deg(h) ≥ 2⁢i   {     j ← jp (mod h);     g ← gcdm(h, j − x);     if g ≠ 1     {       Output(g, i);       q ← 1;       h ← h / g;     }   }   if h ≠ 1   {     Output(h, deg(h));     q ← 1;   }   if q = 0   {     Output(f, 1);   } } ```

The pairs that form the output of this function are the input for the next step. The first element of the pair is a factor of f which is the product of all factors of the degree specified in the second element of the pair.

Equal degree factorization

This is a probabilistic method due to David Cantor and Hans Zassenhaus that finds all factors of the same degree from the output of the previous algorithm:

``` function EDF(f, d, p) {   n ← deg(f);   set list of factors to f;   while size(list of factors) < n/d   {     h ← random polynomial with deg(h) < n;     e ← (qd − 1) / 2;     g ← he − 1 (mod f);     for each element u of list of factors     {       if deg(u) > d       {         j ← gcdm(g, u);         if j ≠ 1 and j ≠ u         {           discard u from list of factors;           add j and u/j to list of factors;         }       }     }   }   Output list of factors } ```

Polynomial lift

Once all irreducible factors of the polynomial modulo p are found, we can find the factor of the polynomial modulo pn if there are no repeated factors. The following algorithm can lift from modulo m to m2, so we can execute it several times until the complete lifting is done.

On input: f = g*h (mod m), s*g + t*h = 1 (mod m)

On output: f = g'*h' (mod m2), s'*g' + t'*h' = 1 (mod m2) with deg(s') < deg(h'), deg(t') < deg(g')

All calculations below are performed mod m2.

```   e ← f - g*h   Compute q, r such that s*e = q*h + r   g' ← g + t*e + q*g   h' ← h + r   e ← s*g' + t*h' − 1   Compute q, r such that s*e = q*h + r   s' ← s − r   t' ← t − t*e − q*g' ```

The initial values of s and t are obtained from g and h using Extended Euclidean algorithm.

We can use the output of the previous section to factor integer polynomials.

First of all we need to divide the polynomial by its content (the greatest common divisor of all coefficients with the sign of the leading coefficient). The result is the principal part, named pp(f).

To perform the square-free factorization, we can factor recursively gcd(f, f') and f/gcd(f, f').

At this moment we know that there are no repeated factors. We have to find a small prime p for which the polynomial has no repeated factors. This can be easily found by checking that gcd(f, f') ≡ 1 (mod p).

We need to find a bound of the coefficient of factors. The Knuth-Cohen bound for coefficients of polynomial factors can be computed as follows:

If polynomial G divides F we have for all j:

|Gj| ≤ binomial(n − 1, j)*(Σi |Fi|2)1/2 + binomial(n − 1, j −1) * |Fm|

where m is the degree of F and n is the degree of G. The maximum degree to be considered is n = ceil(m/2).

Once the bound B is found, we have to compute the least value of e such that 2⁢B < pe.

Now we factor the polynomial mod pe which has r different factors. If r > 10, we can try a different value of p, so we minimize the number of factors found. The application tries up to 5 different primes.

We will now combine the factors found modulo pe to get integer polynomial factors. There are 2r possible combinations, but most of them can be easily discarded.

Let the combination of factors found are f0, f1, ..., fs. Compute a ≡ lc(f) ⁢ tc(f0)⁢ tc(f1) ⁢ ... ⁢tc(fs) (mod pe) and b ≡ lc(f) ⁢ tc(f) (mod pe). In both cases the products must be in the range −pe/2 to pe/2. If a does not divide b, we can discard that combination.

For the very few combinations that remain, compute a ≡ lc (f) ⁢ f0f1 ⁢ ... ⁢ fs (mod pe). Again, this time the coefficients of this polynomial must be in the range −pe/2 to pe/2. Compute b = gcdp(a, lc(f) ⁢ f). If the degree of b is zero, discard this combination. Otherwise, the polynomial b is a proper factor of f.

You can download the source of the current program and the old sum polynomial factorization applet from GitHub. Notice that the source code is in C language and you need the Emscripten environment in order to generate Javascript.

Written by Dario Alpern. Last updated 8 June 2019.