ZeroLimits

Po-Shen Loh Quadratic Method

I found out about the Po-Shen Loh method of solving quadratic equations a few years ago, and I've been meaning to write a post about it since, and only just got around to it.

You can read the original blog post by Dr Loh, which goes into the math behind the method. I'll cover some of that math here too, as well as a code implementation of the method.

Method

Say we start with a quadratic equation:

x2+bx+c=0

where a, b and c are real numbers. We want to factorise this into the form:

(x−r1)(x−r2)=0

where r1 and r2 are also real numbers, known as the roots of the quadratic. These are essentially points where, if the equation was plotted, the graph would cross the x-axis.

The standard method for solving quadratics is using a formula that we've all learned in school:

x=−b±b2−4ac2a

which gives us two values for x, where plugging them into the original quadratic equation would give us 0. These are the two roots, r1 and r2.

However, let's try expanding our factorised equation back into the quadratic form:

(x−r1)(x−r2)=0x2−r2x−r1x+r1r2=0x2−(r1+r2)x+r1r2=0

We can see that −b=r1+r2 and c=r1r2. Now, we could try to guess which values for r1 and r2 fit for these constraints, which works for some simple quadratic equations with integer roots, but gets complicated to do for non-integer roots.

Instead, we can realise that r1,r2=−b2±u for some real number u. This is basically like saying that since r1 and r2 add up to −b, −b/2 must be exactly in between them, and we can simply add or subtract a value u to get to r1 and r2. For example, if we have r1=3 and r2=5, then −b=r1+r2=3+5=8. From this, we can see that −b2=4, and we can simply add and subtract u=1 to get back r1=3 and r2=5.

Next, we can see that c=r1r2. Since we already know that r1=−b2+u and r2=−b2−u, we can simply multiply them together to get c:

c=r1r2=(−b2+u)(−b2−u)=b24−u2

The third line leads from (a+b)(a−b)=a2−b2. Since the values of b and c are already known from the original quadratic equation, we can simply rearrange this equation to get the value of u:

c=b24−u2u2=b24−cu=b24−c

Finally, we can plug in this value into our previous equations for r1 and r2:

r1=−b2+b24−cr2=−b2−b24−c

Now you might be thinking, "well this is all great, but what if we have a coefficient in front of the x2 term?". Great question, let's see what happens! We start with:

ax2+bx+c=0

where, again, a, b and c are all real numbers. All we need to do to apply our previous method is to divide the entire equation by a:

x2+bax+ca=0

If we plug in our new values of b′=b/a and c′=c/a into the equation for our roots from earlier, we get:

r1=−b′2+b′24−c′=−b2a+b24a2−ca=−b2a+b2−4ac4a2=−b2a+b2−4ac2a=−b+b2−4ac2ar2=−b−b2−4ac2a

Seem familiar? These are the roots given by the original quadratic formula. If you were wondering where the formula comes from, this is it.

Why do it using this method then? I think the logical steps that lead to the roots using the Po-Shen Loh method are much more intuitive and easy to work with than trying to remember the quadratic formula, especially for someone who's just learning about quadratics or doesn't use them often enough to have it burned into their memory.

Code implementation

The algorithm above shouldn't be too difficult to implement yourself if you have even a little bit of coding knowledge, since it's mainly just additions, multiplications and square roots. I've written a short implementation of the algorithm in Python, but you can translate the code into whatever other language you'd like.

import math

# Enter your values for the coefficients a, b and c in ax²+bx+c=0
a = 1
b = 2
c = 1

# Calculate b' and c'
b_prime = b / a
c_prime = c / a

# Calculate u = √(b'²/4 - c')
u = math.sqrt((b_prime**2) / 4 - c_prime)

# Calculate the two roots
r1 = -b_prime / 2 + u
r2 = -b_prime / 2 - u

print(f"""
r1 = {r1}
r2 = {r2}
""")

While this code doesn't handle it, the Po-Shen Loh method can also be used to handle complex roots.