Sub topic 14
From Completing the Square to the Quadratic Formula
This chapter applies completing the square once, to the general equation ax^2 + bx + c = 0, and the result is the quadratic formula. After that, a method works for every quadratic, with no guessing at factors. The chapter assumes you can complete the square when a is not 1 and that you know the three cases from the "Two roots, one repeated root, or no real roots" chapter. You also need to be comfortable with fractions and square roots.
Why derive the formula
Completing the square always works, but each equation takes several steps. Those steps are the same every time, whatever the numbers are. If you do them once with letters in place of numbers, you get a formula that carries out the work for any specific a, b, and c.
The derivation is also a check on your understanding. Each line below is a step you already know, applied to a, b, and c instead of numbers.
The derivation, step by step
Start with the general equation, where a is not 0:
ax^2 + bx + c = 0
- Divide both sides by a. This makes the leading coefficient 1, as in the earlier chapter. The result is x^2 + (b/a)x + c/a = 0.
- Move the constant to the right side. The result is x^2 + (b/a)x = -c/a.
- Take half of the x coefficient and square it. Half of b/a is b/(2a). Its square is b^2/(4a^2).
- Add that square to both sides. The result is x^2 + (b/a)x + b^2/(4a^2) = b^2/(4a^2) - c/a.
- Write the left side as a perfect square. The result is (x + b/(2a))^2 = b^2/(4a^2) - c/a.
- Combine the right side over a common denominator. The common denominator is 4a^2. Rewrite c/a as 4ac/(4a^2). The result is (x + b/(2a))^2 = (b^2 - 4ac)/(4a^2).
- Take square roots of both sides. Remember the plus-or-minus sign. The square root of 4a^2 is 2|a|, but the sign is absorbed by the plus-or-minus, so you can write 2a. The result is x + b/(2a) = ±sqrt(b^2 - 4ac)/(2a).
- Subtract b/(2a) from both sides. The result is:
x = (-b ± sqrt(b^2 - 4ac)) / (2a)
This is the quadratic formula.
The derivation as a flow
The diagram shows the order of the steps and where the formula comes from.
The discriminant and the three cases
The expression under the square root, b^2 - 4ac, is called the discriminant. It decides how many real solutions the equation has. This ties back to the three cases from the earlier chapter, because step 6 shows where the discriminant came from: it is the numerator of the right side of the perfect-square equation.
| Discriminant | Right side of (x + b/(2a))^2 = ... | Real solutions |
|---|---|---|
| b^2 - 4ac > 0 | Positive | Two different solutions |
| b^2 - 4ac = 0 | Zero | One repeated solution, x = -b/(2a) |
| b^2 - 4ac < 0 | Negative | No real solutions |
The denominator 4a^2 is always positive, so the sign of the right side depends only on the sign of the discriminant. A square of a real number cannot be negative. That is why a negative discriminant means no real solutions.
When the discriminant is 0, the plus-or-minus has nothing to add or subtract. Both options give x = -b/(2a), which is the repeated root.
Worked examples
Example 1: two roots
Solve 2x^2 - 5x - 3 = 0.
Here a = 2, b = -5, c = -3.
- Discriminant: (-5)^2 - 4(2)(-3) = 25 + 24 = 49.
- Square root: sqrt(49) = 7.
- Apply the formula: x = (5 ± 7) / 4.
- The two solutions are x = 12/4 = 3 and x = -2/4 = -1/2.
Check x = 3: 2(9) - 15 - 3 = 18 - 15 - 3 = 0. Check x = -1/2: 2(1/4) + 5/2 - 3 = 1/2 + 5/2 - 3 = 0. Both work.
Example 2: a repeated root
Solve 4x^2 - 12x + 9 = 0.
Here a = 4, b = -12, c = 9.
- Discriminant: (-12)^2 - 4(4)(9) = 144 - 144 = 0.
- Apply the formula: x = (12 ± 0) / 8 = 3/2.
The only solution is x = 3/2. The left side is (2x - 3)^2, so factoring agrees.
Example 3: no real roots
Solve x^2 + 2x + 5 = 0.
Here a = 1, b = 2, c = 5. The discriminant is 4 - 20 = -16. A negative number has no real square root, so there are no real solutions.
Example 4: an irrational answer
Solve x^2 - 4x + 1 = 0. Here a = 1, b = -4, c = 1.
- Discriminant: 16 - 4 = 12.
- Apply the formula: x = (4 ± sqrt(12)) / 2.
- Simplify: sqrt(12) = 2 sqrt(3), so x = (4 ± 2 sqrt(3)) / 2 = 2 ± sqrt(3).
This equation does not factor over the integers. That is the situation where the formula, like completing the square, is most useful.
Running the formula as code
The function below implements the formula and reports the case from the discriminant. It uses exact fractions so that rational answers stay exact.
from fractions import Fraction
import math
def quadratic_formula(a, b, c):
"""Return the real solutions of a*x^2 + b*x + c = 0 as a tuple (a != 0)."""
if a == 0:
raise ValueError("a must not be 0")
a, b, c = Fraction(a), Fraction(b), Fraction(c)
disc = b * b - 4 * a * c
if disc < 0:
return ()
if disc == 0:
return (-b / (2 * a),)
root = math.sqrt(disc)
x1 = (-float(b) - root) / (2 * float(a))
x2 = (-float(b) + root) / (2 * float(a))
return tuple(sorted((x1, x2)))
print(quadratic_formula(2, -5, -3)) # (-0.5, 3.0)
print(quadratic_formula(4, -12, 9)) # (Fraction(3, 2),)
print(quadratic_formula(1, 2, 5)) # ()
print(quadratic_formula(1, -4, 1)) # (0.2679..., 3.7320...)The discriminant is computed with exact fractions, so the test for zero is reliable. If you computed it with floating-point numbers, a value that should be exactly 0 could come out as a tiny nonzero number and send you to the wrong case.
Common errors
- Forgetting that -b changes the sign of b. If b = -5, then -b = 5. Put b in parentheses when you substitute, as in (-5)^2, to avoid sign mistakes.
- Dividing only part of the numerator by 2a. The whole expression -b ± sqrt(b^2 - 4ac) is divided by 2a. Writing -b ± sqrt(b^2 - 4ac)/2a is a different, wrong expression.
- Cancelling incorrectly when simplifying. In (4 ± 2 sqrt(3)) / 2, you can divide each term of the numerator by 2. You cannot cancel the 2 in front of sqrt(3) against the denominator and leave the 4 alone.
- Forgetting that the equation must be in standard form first. If the equation is 3x^2 = 5x + 2, move everything to one side before you read off a, b, and c. Here that gives 3x^2 - 5x - 2 = 0, so c = -2.
- Applying the formula with a = 0. The derivation divides by a. If a = 0, the equation is linear, not quadratic.
Where the formula fits among the methods
The formula does not replace the other methods. It is the one you can always fall back on.
- Use factoring when the numbers are small and the factors are easy to see. It is the fastest method for equations such as x^2 + 5x + 6 = 0.
- Use the square root method when the equation is already in the form (x + h)^2 = k, or has no x term.
- Use the formula when factoring is not obvious or the answers are irrational.
- Use completing the square when you want the vertex form of the parabola, or when you want to see why the formula works.
The discriminant is a useful shortcut even if you plan to factor. If it is a perfect square (0, 1, 4, 9, 16, and so on) and a, b, and c are integers, the equation factors over the integers. If it is not a perfect square, no integer factorization exists, so you can stop searching and use the formula.
Exercise
Use the quadratic formula to solve 3x^2 + 2x - 5 = 0. State the discriminant and say how many real solutions the equation has.
Show answer
Here a = 3, b = 2, c = -5. The discriminant is 2^2 - 4(3)(-5) = 4 + 60 = 64, which is positive, so there are two real solutions. Since sqrt(64) = 8, x = (-2 ± 8) / 6. That gives x = 6/6 = 1 and x = -10/6 = -5/3. Check x = 1: 3 + 2 - 5 = 0.
Exercise
For what value of k does x^2 + 6x + k = 0 have exactly one real solution? What is that solution?
Show answer
One real solution means the discriminant is 0. The discriminant is 6^2 - 4(1)(k) = 36 - 4k. Setting 36 - 4k = 0 gives k = 9. The solution is x = -b/(2a) = -6/2 = -3. This matches the equation x^2 + 6x + 9 = (x + 3)^2 = 0.
Exercise
In the derivation, step 4 adds b^2/(4a^2) to both sides. Why is that exact value the right one to add?
Show answer
After dividing by a, the x coefficient is b/a. Completing the square adds the square of half of that coefficient. Half of b/a is b/(2a), and its square is b^2/(4a^2). Adding this makes the left side x^2 + (b/a)x + b^2/(4a^2), which equals (x + b/(2a))^2.