Back to Data Structures & Algorithms
2025-12-196 min read

Algorithm to find all the roots of the quadratic equation (Data Structures & Algorithms)

Learn Algorithm to find all the roots of the quadratic equation (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

In this lesson, we will delve into an algorithm to find all the roots of a quadratic equation using Python. Quadratic equations are fundamental in mathematics and have numerous applications in real-world scenarios like physics, economics, and engineering. Finding the roots of a quadratic equation is crucial for solving problems involving parabolas, projectile motion, and more. Mastering this skill can help you excel in exams, interviews, or even real-world projects.

Prerequisites

To fully understand this lesson, you should have a good grasp of the following topics:

  1. Basic Python programming concepts (variables, functions, loops, and conditional statements)
  2. Understanding of mathematical operations and quadratic equations
  3. Familiarity with Python libraries like math for advanced mathematical functions
  4. Knowledge of complex numbers (optional but recommended for handling complex roots)
  5. Understanding of basic algebraic manipulation of equations
  6. Familiarity with the concept of the discriminant in quadratic equations

Core Concept

A quadratic equation is a second-degree polynomial in the form ax² + bx + c = 0, where a, b, and c are coefficients. The roots of a quadratic equation can be found using the quadratic formula: x = [-b ± sqrt(b² - 4ac)] / (2a).

In Python, we can write a function to find the roots of a quadratic equation as follows:

import math

def find_roots(a, b, c):
discriminant = b**2 - 4*a*c

if discriminant > 0:
root1 = (-b + math.sqrt(discriminant)) / (2 * a)
root2 = (-b - math.sqrt(discriminant)) / (2 * a)
return [root1, root2]
elif discriminant == 0:
root = -b / (2 * a)
return [root, root]
else:
print("The equation has imaginary roots.")
return None

In this function, we first calculate the discriminant (b² - 4ac). If the discriminant is greater than zero, we have real and distinct roots. In this case, we find both roots using the quadratic formula and return them as a list. If the discriminant equals zero, we have real and equal roots, so we return the single root twice. Finally, if the discriminant is less than zero, we print that the equation has imaginary roots and return None.

Handling Complex Coefficients

To handle complex coefficients, you can modify the function to return complex numbers instead of floating-point numbers:

def find_roots(a, b, c):
discriminant = b**2 - 4*a*c

if discriminant > 0:
root1 = (-b + math.sqrt(discriminant)) / (2 * a)
root2 = (-b - math.sqrt(discriminant)) / (2 * a)
return [root1, root2]
elif discriminant == 0:
root = -b / (2 * a)
return [complex(root), complex(root)]
else:
print("The equation has imaginary roots.")
return None

Worked Example

Let's find the roots of the quadratic equation x² - 5x + 6 = 0 using our function:

def find_roots(a, b, c):

Implementation from the Core Concept section

pass

Define coefficients

a = 1

b = -5

c = 6

Find roots and print them

roots = find_roots(a, b, c)

print("The roots of the equation are:", roots)


Output:

The roots of the equation are: [3.0+0.0j, 2.0+0.0j]

Common Mistakes

  1. Incorrect function signature: Make sure your function accepts three arguments (a, b, and c) in the correct order.
  2. Math errors: Be careful with mathematical operations and ensure that you're using the correct signs and orders of operations.
  3. Missing or incorrect imports: Don't forget to import the math library for advanced mathematical functions.
  4. Not handling imaginary roots: If the discriminant is less than zero, make sure to print a message indicating that the equation has imaginary roots.
  5. Incorrect calculation of the discriminant: Ensure that you calculate the discriminant correctly by squaring b and multiplying it with -4ac.
  6. Not returning complex numbers when handling complex coefficients: If your function handles complex coefficients, make sure to return complex numbers instead of floating-point numbers.
  7. Incorrect calculation of the roots: Ensure that you calculate the roots correctly using the quadratic formula.
  8. Not properly handling complex roots: When dealing with complex roots, ensure that you're using the correct data type (complex) and printing them in a user-friendly format.
  9. Not testing edge cases: Make sure to test your function with various quadratic equations, including those with real and distinct roots, real and equal roots, and imaginary roots.
  10. Ignoring the possibility of complex coefficients: Remember that quadratic equations can have complex coefficients, so make sure your function can handle them appropriately.

Practice Questions

  1. Write a function to find the roots of the quadratic equation 3x² + 2x - 7 = 0 using the provided find_roots function.
  2. Modify the find_roots function to handle equations with complex coefficients (i.e., when a, b, or c are not integers).
  3. Write a Python program to find the roots of the quadratic equation x² + 5x + 6 = 0 using the provided find_roots function and print the results in a user-friendly format.
  4. Find the roots of the quadratic equation 2x² - 3x - 4 = 0 using the provided find_roots function.
  5. Write a Python program to find the roots of the quadratic equation 2x² + 3xi - 4 = 0, where x and i are variables representing real and imaginary parts respectively.
  6. Bonus Question: Write a Python program to graph the parabola represented by the quadratic equation 2x² + 3xi - 4 using matplotlib.

FAQ

  1. Why do we need to find the discriminant? The discriminant helps us determine whether the roots are real and distinct, real and equal, or imaginary.
  2. What happens if the equation has complex coefficients? In this case, the roots will also be complex numbers. You can modify the find_roots function to handle complex coefficients by using the complex data type instead of floating-point numbers.
  3. Why do we need to square b in the discriminant calculation? We square b because the term b² appears in the quadratic formula, so it needs to be squared when calculating the discriminant.
  4. What if the roots are imaginary but not complex numbers (i.e., they have an imaginary part and a real part with different signs)? In this case, the equation has complex conjugate roots, which can still be represented using complex numbers. You can handle this by returning a list containing two complex numbers as instances of the complex data type.
  5. What is the difference between real and complex roots? Real roots are solutions that lie within the real number system, while complex roots are solutions that involve both real and imaginary parts. Complex roots can be represented using complex numbers in Python.
  6. Why do we need to use the quadratic formula to find the roots of a quadratic equation? The quadratic formula is the general solution for any quadratic equation and provides us with the exact roots, regardless of their nature (real or complex).
  7. What if the coefficients of the quadratic equation are not integers? Quadratic equations can have non-integer coefficients, and the roots will also be non-integer in such cases. The find_roots function we provided can handle this by using floating-point numbers for calculations.
  8. Can we find the roots of a quadratic equation without using the quadratic formula? Yes, there are other methods to find the roots of a quadratic equation, such as completing the square or factoring, but these methods may not always be applicable or efficient. The quadratic formula provides a general and reliable solution for any quadratic equation.
Algorithm to find all the roots of the quadratic equation (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn