Form the Cubic equation from the given roots
Last Updated :
15 Nov, 2021
Given the roots of a cubic equation A, B and C, the task is to form the Cubic equation from the given roots.
Note: The given roots are integral.
Examples:
Input: A = 1, B = 2, C = 3
Output: x^3 – 6x^2 + 11x – 6 = 0
Explanation:
Since 1, 2, and 3 are roots of the cubic equations, Then equation is given by:
(x – 1)(x – 2)(x – 3) = 0
(x – 1)(x^2 – 5x + 6) = 0
x^3 – 5x^2 + 6x – x^2 + 5x – 6 = 0
x^3 – 6x^2 + 11x – 6 = 0.
Input: A = 5, B = 2, C = 3
Output: x^3 – 10x^2 + 31x – 30 = 0
Explanation:
Since 5, 2, and 3 are roots of the cubic equations, Then equation is given by:
(x – 5)(x – 2)(x – 3) = 0
(x – 5)(x^2 – 5x + 6) = 0
x^3 – 5x^2 + 6x – 5x^2 + 25x – 30 = 0
x^3 – 10x^2 + 31x – 30 = 0.
Approach: Let the root of the cubic equation (ax3 + bx2 + cx + d = 0) be A, B and C. Then the given cubic equation can be represents as:
ax3 + bx2 + cx + d = x3 – (A + B + C)x2 + (AB + BC +CA)x + A*B*C = 0.
Let X = (A + B + C)
Y = (AB + BC +CA)
Z = A*B*C
Therefore using the above relation find the value of X, Y, and Z and form the required cubic equation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findEquation( int a, int b, int c)
{
int X = (a + b + c);
int Y = (a * b) + (b * c) + (c * a);
int Z = a * b * c;
cout << "x^3 - " << X << "x^2 + "
<< Y << "x - " << Z << " = 0" ;
}
int main()
{
int a = 5, b = 2, c = 3;
findEquation(a, b, c);
return 0;
}
|
Java
class GFG{
static void findEquation( int a, int b, int c)
{
int X = (a + b + c);
int Y = (a * b) + (b * c) + (c * a);
int Z = a * b * c;
System.out.print( "x^3 - " + X+ "x^2 + "
+ Y+ "x - " + Z+ " = 0" );
}
public static void main(String[] args)
{
int a = 5 , b = 2 , c = 3 ;
findEquation(a, b, c);
}
}
|
Python3
def findEquation(a, b, c):
X = (a + b + c);
Y = (a * b) + (b * c) + (c * a);
Z = (a * b * c);
print ( "x^3 - " , X ,
"x^2 + " ,Y ,
"x - " , Z , " = 0" );
if __name__ = = '__main__' :
a = 5 ;
b = 2 ;
c = 3 ;
findEquation(a, b, c);
|
C#
using System;
class GFG{
static void findEquation( int a, int b, int c)
{
int X = (a + b + c);
int Y = (a * b) + (b * c) + (c * a);
int Z = a * b * c;
Console.Write( "x^3 - " + X +
"x^2 + " + Y +
"x - " + Z + " = 0" );
}
public static void Main()
{
int a = 5, b = 2, c = 3;
findEquation(a, b, c);
}
}
|
Javascript
<script>
function findEquation(a, b, c)
{
let X = (a + b + c);
let Y = (a * b) + (b * c) + (c * a);
let Z = a * b * c;
document.write( "x^3 - " + X + "x^2 + "
+ Y + "x - " + Z + " = 0" );
}
let a = 5, b = 2, c = 3;
findEquation(a, b, c);
</script>
|
Output:
x^3 - 10x^2 + 31x - 30 = 0
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...