The Blog of Random

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=0x^2 + bx + c = 0

where aa, bb and cc are real numbers. We want to factorise this into the form:

(xr1)(xr2)=0(x - r_1)(x - r_2) = 0

where r1r_{1} and r2r_{2} 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 xx-axis.

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

x=b±b24ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

which gives us two values for xx, where plugging them into the original quadratic equation would give us 0. These are the two roots, r1r_1 and r2r_2.

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

(xr1)(xr2)=0x2r2xr1x+r1r2=0x2(r1+r2)x+r1r2=0\begin{align} (x - r_1)(x - r_2) & = 0 \\ x^2 - r_2 x - r_1 x + r_1 r_2 & = 0 \\ x^2 - (r_1 + r_2)x + r_1 r_2 & = 0 \end{align}

We can see that b=r1+r2-b = r_1 + r_2 and c=r1r2c = r_1 r_2. Now, we could try to guess which values for r1r_1 and r2r_2 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±ur_1, r_2 = -\frac{b}{2} \pm u for some real number uu. This is basically like saying that since r1r_1 and r2r_2 add up to b-b, b2-\frac{b}{2} must be exactly in between them, and we can simply add or subtract a value uu to get to r1r_1 and r2r_2. For example, if we have r1=3r_1=3 and r2=5r_2=5, then b=r1+r2=3+5=8-b=r_1+r_2=3+5=8. From this, we can see that b2=4-\frac{b}{2} = 4, and we can simply add and subtract u=1u=1 to get back r1=3r_1=3 and r2=5r_2=5.

Next, we can see that c=r1r2c = r_1 r_2. Since we already know that r1=b2+ur_1 = -\frac{b}{2}+u and r2=b2ur_2 = -\frac{b}{2}-u, we can simply multiply them together to get cc:

c=r1r2=(b2+u)(b2u)=b24u2\begin{align} c & = r_1 r_2 \\ & = \left( -\frac{b}{2} + u \right) \left( -\frac{b}{2} - u \right) \\ & = \frac{b^2}{4} - u^2 \end{align}

The third line leads from (a+b)(ab)=a2b2(a+b)(a-b)=a^2-b^2. Since the values of bb and cc are already known from the original quadratic equation, we can simply rearrange this equation to get the value of uu:

c=b24u2u2=b24cu=b24c\begin{align} c & = \frac{b^2}{4} - u^2 \\ u^2 & = \frac{b^2}{4} - c \\ u & = \sqrt{\frac{b^2}{4} - c} \end{align}

Finally, we can plug in this value into our previous equations for r1r_1 and r2r_2:

r1=b2+b24cr2=b2b24c\begin{align} r_1 & = -\frac{b}{2} + \sqrt{\frac{b^2}{4} - c} \\ r_2 & = -\frac{b}{2} - \sqrt{\frac{b^2}{4} - c} \\ \end{align}

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

ax2+bx+c=0ax^2 + bx + c = 0

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

x2+bax+ca=0x^2 + \frac{b}{a} x + \frac{c}{a} = 0

If we plug in our new values of b=bab' = \frac{b}{a} and c=cac' = \frac{c}{a} into the equation for our roots from earlier, we get:

r1=b2+b24c=b2a+b24a2ca=b2a+b24ac4a2=b2a+b24ac2a=b+b24ac2ar2=bb24ac2a\begin{align} r_1 & = -\frac{b'}{2} + \sqrt{\frac{{b'}^2}{4} - c'} \\ & = -\frac{b}{2a} + \sqrt{\frac{b^2}{4a^2} - \frac{c}{a}} \\ & = -\frac{b}{2a} + \sqrt{\frac{b^2 - 4ac}{4a^2}} \\ & = -\frac{b}{2a} + \frac{\sqrt{b^2 - 4ac}}{2a} \\ & = \frac{-b + \sqrt{b^2 - 4ac}}{2a} \\ r_2 & = \frac{-b - \sqrt{b^2 - 4ac}}{2a} \end{align}

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 C, but you can translate the code into whatever other language you'd like.

#include <math.h>
#include <stdio.h>

int main() {
  // Enter your values for the coefficients a, b and c in ax²+bx+c=0
  double a = 1;
  double b = 2;
  double c = 1;

  // Calculate b' and c'
  double b_prime = b / a;
  double c_prime = c / a;

  // Calculate u = √(b'²/4 - c')
  double u = sqrt((b_prime * b_prime) / 4 - c_prime);

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

  printf("r₁ = %f\n"
         "r₂ = %f\n",
         r1, r2);
}

While this code doesn't handle it, the Po-Shen Loh method can also be used to handle complex roots. You can do this in C, but it'll be easier to do in a language that has a built-in complex number type like Go or Python.