randRangeNonZero( -10, 10 ) randRangeNonZero( -10, 10 )
1 SQUARE*A*B A*B SQUARE*(-A-B) -A-B

Factor the following expression:

plus(SQUARE + "x^2") + plus( LINEAR + "x" ) + CONSTANT

^\s*\(\s*[xX]\s*A < 0 ? "\\+" : "[-\u2212]"\s*abs( A )\s*\)\s*\(\s*[xX]\s*B < 0 ? "\\+" : "[-\u2212]"\s*abs( B )\s*\)\s*$
^\s*\(\s*[xX]\s*B < 0 ? "\\+" : "[-\u2212]"\s*abs( B )\s*\)\s*\(\s*[xX]\s*A < 0 ? "\\+" : "[-\u2212]"\s*abs( A )\s*\)\s*$
a factored expression, like (x+1)(x+2)

When we factor a polynomial, we are basically reversing this process of multiplying linear expressions together:

\qquad \begin{eqnarray} (x + a)(x + b) \quad&=&\quad xx &+& xb + ax &+& ab \\ \\ &=&\quad x^2 &+& \color{GREEN}{(a + b)}x &+& \color{BLUE}{ab} \end{eqnarray}

\qquad \begin{eqnarray} \hphantom{(x + a)(x + b) \quad}&\hphantom{=}&\hphantom{\quad xx }&\hphantom{+}&\hphantom{ (a + b)x }&\hphantom{+}& \\ &=&\quad x^2 & SIMPLELINEAR >= 0 ? "+" : ""& plus( "\\color{" + GREEN + "}{" + SIMPLELINEAR + "}x" )& SIMPLECONSTANT >= 0 ? "+" : ""& plus( "\\color{" + BLUE + "}{" + SIMPLECONSTANT + "}" ) \end{eqnarray}

The coefficient on the x term is SIMPLELINEAR and the constant term is SIMPLECONSTANT, so to reverse the steps above, we need to find two numbers that add up to SIMPLELINEAR and multiply to SIMPLECONSTANT.

You can try out different factors of SIMPLECONSTANT to see if you can find two that satisfy both conditions. If you're stuck and can't think of any, you can also rewrite the conditions as a system of equations and try solving for a and b:

\qquad \color{PINK}{a} + \color{PINK}{b} = \color{GREEN}{SIMPLELINEAR}

\qquad \color{PINK}{a} \times \color{PINK}{b} = \color{BLUE}{SIMPLECONSTANT}

The two numbers -A and -B satisfy both conditions:

\qquad \color{PINK}{-A} + \color{PINK}{-B} = \color{GREEN}{SIMPLELINEAR}

\qquad \color{PINK}{-A} \times \color{PINK}{-B} = \color{BLUE}{SIMPLECONSTANT}

So we can factor the expression as: (x A < 0 ? "+" : "" \color{PINK}{-A}) (x B < 0 ? "+" : "" \color{PINK}{-B})