2

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?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
tekknolagi
  • 10,663
  • 24
  • 75
  • 119

3 Answers3

6

Use the quadratic formula.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • 1
    they are essentially equivalent. Look at the Quadratic Factorization section of the referenced page. – Keith Randall Feb 15 '11 at 01:06
  • 2
    @tekk The results you get from solving the quadratic are basically the factors. a parabola with roots 3 and 5 has the factored form `(x-3)(x-5)` :D – Gordon Gustafson Feb 15 '11 at 01:07
  • the issue is what if its like this (3x - 4)(x + 9) i got as far as to where x has no coefficient – tekknolagi Feb 15 '11 at 01:13
  • You'll see from the referenced page that the factorization is a(x-r1)(x-r2) where r1 and r2 come from the quadratic formula. You could equally write that as (a*x-a*r1)(x-r2) if you'd like. – Keith Randall Feb 15 '11 at 01:27
  • Use this method for the roots, at the bottom of the Wikipedia page: http://stackoverflow.com/questions/4503849/quadratic-equation-in-ada – Alexandre C. Feb 16 '11 at 14:15
6

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.

Nelson
  • 3
  • 4
hugomg
  • 68,213
  • 24
  • 160
  • 246
2

I tried implementing hugomg's approach. I stole the "gcd" and "simplify fraction" function from online. Here is my sloppy approach:

from math import sqrt

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def simplify_fraction(numer, denom):
    if denom == 0:
        return "Division by 0 - result undefined"

    # Remove greatest common divisor:
    common_divisor = gcd(numer, denom)
    (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
    # Note that reduced_den > 0 as documented in the gcd function.

    if common_divisor == 1:
        return (numer, denom)
    else:
        # Bunch of nonsense to make sure denominator is negative if possible
        if (reduced_den > denom):
            if (reduced_den * reduced_num < 0):
                return(-reduced_num, -reduced_den)
            else:
                return (reduced_num, reduced_den)
        else:
            return (reduced_num, reduced_den)

def quadratic_function(a,b,c):
    if (b**2-4*a*c >= 0):
        x1 = (-b+sqrt(b**2-4*a*c))/(2*a)
        x2 = (-b-sqrt(b**2-4*a*c))/(2*a)
        # Added a "-" to these next 2 values because they would be moved to the other side of the equation
        mult1 = -x1 * a
        mult2 = -x2 * a
        (num1,den1) = simplify_fraction(a,mult1)
        (num2,den2) = simplify_fraction(a,mult2)
        if ((num1 > a) or (num2 > a)):
            # simplify fraction will make too large of num and denom to try to make a sqrt work
            print("No factorization")
        else:
            # Getting ready to make the print look nice
            if (den1 > 0):
                sign1 = "+"
            else:
                sign1 = ""
            if (den2 > 0):
                sign2 = "+"
            else:
                sign2 = ""
            print("({}x{}{})({}x{}{})".format(int(num1),sign1,int(den1),int(num2),sign2,int(den2)))
    else:
        # if the part under the sqrt is negative, you have a solution with i
        print("Solutions are imaginary")
    return

# This function takes in a, b, and c from the equation:
# ax^2 + bx + c
# and prints out the factorization if there is one

quadratic_function(7,27,-4)

If I run this I get the output:

(7x-1)(1x+4)
mrmath3
  • 41
  • 6
  • When you manually factor `−8x^2 −15x+2` you should get `(−8x+1)(x+2)`. `quadratic_function(-8, -15, 2)` from your code outputs `No factorization`. Where is the bug? – John Smith Aug 29 '21 at 09:55
  • @JohnSmith, change `if ((num1 > a) or (num2 > a)):` to `if ((num1 > abs(a)) or (num2 > abs(a))):`. – M. Al Jumaily May 16 '23 at 15:26