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:
where , and are real numbers. We want to factorise this into the form:
where and 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 -axis.
The standard method for solving quadratics is using a formula that we've all learned in school:
which gives us two values for , where plugging them into the original quadratic equation would give us 0. These are the two roots, and .
However, let's try expanding our factorised equation back into the quadratic form:
We can see that and . Now, we could try to guess which values for and 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 for some real number . This is basically like saying that since and add up to , must be exactly in between them, and we can simply add or subtract a value to get to and . For example, if we have and , then . From this, we can see that , and we can simply add and subtract to get back and .
Next, we can see that . Since we already know that and , we can simply multiply them together to get :
The third line leads from . Since the values of and are already known from the original quadratic equation, we can simply rearrange this equation to get the value of :
Finally, we can plug in this value into our previous equations for and :
Now you might be thinking, "well this is all great, but what if we have a coefficient in front of the term?". Great question, let's see what happens! We start with:
where, again, , and are all real numbers. All we need to do to apply our previous method is to divide the entire equation by :
If we plug in our new values of and into the equation for our roots from earlier, we get:
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.