Back to Practice Dashboard
Python BasicsEasy

Finding Roots of a quadratic equation

Learn how to solve the 'Finding Roots of a quadratic equation' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function find_roots(a, b, c) that takes three numbers a, b, and c representing the coefficients of a quadratic equation ax² + bx + c = 0, and returns the roots.

Use the quadratic formula: x = (-b ± √(b²-4ac)) / (2a)

Return:

- A tuple of two floats (root1, root2) rounded to 2 decimal places when b²-4ac >= 0 (root1 <= root2)

- The string 'Complex Roots' when b²-4ac < 0 (discriminant is negative)

Assume a ≠ 0.

Constraints
  • -1000 <= a, b, c <= 1000
  • a != 0

Examples

Example 1
Input
find_roots(1, -3, 2)
Output
(1.0, 2.0)
Explanation

x² - 3x + 2 = 0. Discriminant = 9 - 8 = 1. Roots: (3±1)/2 = 2 and 1. Sorted: (1.0, 2.0).

Example 2
Input
find_roots(1, -2, 1)
Output
(1.0, 1.0)
Explanation

x² - 2x + 1 = 0. Discriminant = 4 - 4 = 0. Double root: x = 2/2 = 1.0.

Example 3
Input
find_roots(1, 1, 1)
Output
'Complex Roots'
Explanation

x² + x + 1 = 0. Discriminant = 1 - 4 = -3 < 0. No real roots.

Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor