Solving a cubic equation
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A cubic equation is a polynomial of degree three, usually written as ax^3 + bx^2 + cx + d = 0 with a not equal to zero. In practice, cubics are solved in three main ways: factor them when possible, use a general algebraic method such as Cardano's idea, or apply a numerical method when you only need approximate roots.
Start with factoring when the equation is friendly
If the coefficients are simple integers, the fastest method is often to look for a rational root. For example:
Try small integer candidates such as 1, 2, and 3:
- plugging in
1gives zero, - so
(x - 1)is a factor.
Then divide the cubic by (x - 1) and solve the remaining quadratic:
So the roots are 1, 2, and 3.
Using code to find roots numerically
Not every cubic factors nicely. In programming, numerical methods are often the most practical route.
Here is a Newton-Raphson example in Python for solving x^3 - x - 2 = 0:
This converges to a real root near 1.52138. Numerical methods are especially useful in software because they work even when the symbolic expression is messy.
What Cardano's method is doing
There is a famous exact algebraic solution for cubics. The classic approach first removes the x^2 term by substitution, turning the equation into a depressed cubic. From there, the cubic is rewritten in a form that can be solved using radicals.
In other words, Cardano's method is the cubic analogue of using the quadratic formula, but it is more complicated in practice. It is important mathematically, but in real application code it is usually less useful than factoring or numeric root finding.
Checking your answer in code
Even if you solve the cubic by hand, it is good practice to verify the result numerically:
A true root should make the polynomial evaluate to zero or extremely close to zero if you are using approximations.
When there are three real roots or complex roots
A cubic always has three roots if you count complex numbers. Depending on the coefficients, you may get:
- three distinct real roots,
- one real root and two complex conjugates,
- or repeated roots.
That is why graphing the polynomial can be helpful. A cubic may cross the x-axis once or several times, but the full solution set can still include complex values even when they are not visible on the real-number graph.
Common Pitfalls
The most common mistake is assuming every cubic has a neat factorization over the integers. Many do not, so it is important to switch methods quickly instead of forcing an algebraic pattern that is not there.
Another issue is using Newton's method with a poor starting point and assuming the first iteration result is correct. Numerical methods need convergence checks, especially if the derivative is small near the guess.
Be careful with arithmetic errors when dividing out a known factor. One small sign mistake in polynomial division changes the entire remaining quadratic.
Finally, remember that approximate roots are not exact roots. In code, always verify by substituting back into the polynomial and checking how close the result is to zero.
Summary
- Factor first if the coefficients suggest simple rational roots.
- Use numerical methods such as Newton-Raphson when exact factoring is inconvenient.
- Cardano's method gives a general exact solution, but it is often more useful conceptually than computationally.
- Always verify roots by substituting them back into the original polynomial.
- A cubic can have repeated, real, or complex roots depending on its coefficients.
Related reading
- Solving a Linear Diophantine Equationsee description for examples
- Solving linear equations represented as a string
- Sort a set of 3-D points in clockwise/counter-clockwise order
- Sort Four Points in Clockwise Order
- Sort points in clockwise order?
- Sort polygon's points for drawing
- Sorted intervals query
- Sorting a permutation with minimum cost

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.