← Back to challenges

Roots of Cubic Equation

PythonHardmathalgorithmsnumbers

Instructions

Find the roots of the equation Ax^3 + Bx^2 + Cx + D = 0. The function is given with the four coefficients A, B, C, D and returns a sorted tuple (x1, x2, x3) of roots.

Examples

find_roots(1, 13, -109, 95) ➞ (-19, 1, 5)

find_roots(1, -9, -169, 561) ➞ (-11, 3, 17)

find_roots(16, -80, -416, 1920) ➞ (-5, 4, 6)

Notes

  • All roots in the Tests are integers.
  • You may find a first root numerically (starting with a guess of zero), round it to a whole number and then solve a quadratic equation for two other roots.
  • Sort the triple before returning.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.