Lesson 13 of 18

Polynomial Roots

Finding Roots of Quadratics

A quadratic equation ax2+bx+c=0ax^2 + bx + c = 0 has roots given by the quadratic formula:

x = rac{-b pm sqrt{b^2 - 4ac}}{2a}

The expression under the square root, Delta=b24acDelta = b^2 - 4ac, is the discriminant:

  • Delta>0Delta > 0 → two distinct real roots
  • Delta=0Delta = 0 → one repeated real root
  • Delta<0Delta < 0 → no real roots (complex)
import math

def quadratic_roots(a, b, c):
    disc = b**2 - 4*a*c
    if disc < 0:
        return None   # complex roots
    x1 = (-b - math.sqrt(disc)) / (2*a)
    x2 = (-b + math.sqrt(disc)) / (2*a)
    return sorted([round(x1, 6), round(x2, 6)])

print(quadratic_roots(1, -3, 2))   # [1.0, 2.0]  (x-1)(x-2)=0
print(quadratic_roots(1, 0, -4))   # [-2.0, 2.0] (x-2)(x+2)=0
print(quadratic_roots(1, 0, 1))    # None (complex roots)

Applications

  • Signal processing — filter design uses polynomial roots
  • Control systems — stability determined by root locations
  • Numerical methods — root-finding is fundamental to scientific computing

Your Task

Implement quadratic_roots(a, b, c) that returns a sorted list of real roots [x1, x2] rounded to 6 decimal places, or None if the roots are complex (discriminant <0< 0).

Pyodide loading...
Loading...
Click "Run" to execute your code.