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
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.
- •-1000 <= a, b, c <= 1000
- •a != 0
Examples
find_roots(1, -3, 2)
(1.0, 2.0)
x² - 3x + 2 = 0. Discriminant = 9 - 8 = 1. Roots: (3±1)/2 = 2 and 1. Sorted: (1.0, 2.0).
find_roots(1, -2, 1)
(1.0, 1.0)
x² - 2x + 1 = 0. Discriminant = 4 - 4 = 0. Double root: x = 2/2 = 1.0.
find_roots(1, 1, 1)
'Complex Roots'
x² + x + 1 = 0. Discriminant = 1 - 4 = -3 < 0. No real roots.
Need a Hint?
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.