Write a program or function that takes in a quadratic equation and prints its factorized roots. The input can be taken in from any reasonable source, such as a function parameter, stdin, or a command line argument.

Edit: This is not intended to be a duplicate of other quadratic equation questions. While others simply provide the integers of the equation and expect integer answers, this question is more about parsing the input string and printing an output string of the exact appropriate form.


Input will be either a string or an array of characters, and it will be of the form ax^2 + bx + c, where a, b, and c are integers. The input will always be exactly of this form, whether the integers are positive, negative, or 0.


Output will be a string (or array of characters) of the form (x - d)(x - e) where d and e are the roots of the equation. You may assume that input will have exactly 2 rational roots.

Test Cases

1x^2 + 6x + 9 => (x - -3)(x - -3)

1x^2 + 0x + -16 => (x - 4)(x - -4)

3x^2 + -9x + -12 => (x - -1)(x - 4)

Note: this last example is not an equality as the factor of 3 is missing. Imaginary bonus points if you include the factor, but it is not necessary.


This is code golf, so shortest answer in bytes wins.


If your submission can handle the following restrictions, your score will be halved.

  • If b or c in the input are negative, it will be represented with a minus, i.e. ax^2 + -bx + -c will instead be represented as ax^2 - bx - c
  • If a, b, or c are 1, they will be omitted. If they are 0, the whole value will be omitted. i.e. 1x^2 + 0x + -16 will be represented as x^2 - 16
  • The same rules apply to output; x^2 + 5x must produce exactly (x)(x + 5) or (x + 5)(x)
  • \$\begingroup\$ @totallyhuman This is similar, but that question is about the math involved. My intent with this question is more about parsing the input and producing an output of the correct format. Please let me know if there is anything I can change to make that more clear. Thanks! \$\endgroup\$ – Cowabunghole Aug 14 '17 at 14:34
  • 1
    \$\begingroup\$ I would suggest removing the bonuses - people usually don't want to take the time to figure out if the bonus will save them bytes or not. Figure out which parts are required, and which aren't. That said, with your comment RE I/O, I am voting to reopen. \$\endgroup\$ – Stephen Aug 14 '17 at 14:40
  • \$\begingroup\$ I/O restrictions are usually discouraged and if you remove that, this is the same task as the dupe. \$\endgroup\$ – totallyhuman Aug 14 '17 at 14:42
  • \$\begingroup\$ @totallyhuman Understood. This is my first question on PPCG so I wasn't sure exactly how it would go over. Thanks for the feedback! \$\endgroup\$ – Cowabunghole Aug 14 '17 at 14:45
  • 3
    \$\begingroup\$ Ah, getting a challenge right the first time is pretty hard. ;) You should try to use the Sandbox, where people should help iron out the problems and check if it's a dupe. \$\endgroup\$ – totallyhuman Aug 14 '17 at 14:47

Browse other questions tagged or ask your own question.