Search code examples
pythonmathquadraticfactorization

Factor a quadratic polynomial in Python


Since factoring a quadratic equation in my head just happens, and has done that since I learned it - how would I go about starting to write a quadratic factorer in Python?


Solution

  • Improving Keiths's answer:

    Start with a polynomial P(x) = a*x^2 + b*x + c. Use the quadratic formula (or another method of your choice) to find the roots r1 and r2 to P(x) = 0.

    You can now factor P(x) as a*(x-r1)(x-r2).


    If your factor (3x - 4)(x - 9) the solution will be 3*(x - 4/3)(x - 9). You might want to find a way to multiply the 3 into the factors to get rid of fractions / look pretty. In this case, it might help to use fraction arithmetic instead of doubles so you can know the denominators better.