Vieta’s formula relates the coefficients of polynomial to the sum and product of their roots, as well as the products of the roots taken in groups. Vieta’s formula describes the relationship of the roots of a polynomial with its coefficients. Consider the following example to find a polynomial with given roots. (Only discuss real-valued polynomials, i.e. the coefficients of polynomials are real numbers). Let’s take a quadratic polynomial. Given two real roots and , then find a polynomial.
Consider the polynomial . Given the roots, we can also write it as
.
Since both equation represents the same polynomial, so equate both polynomial
Simplifying the above equation, we get
Comparing the coefficients of both sides, we get
For , ,
For , ,
For constant term, ,
Which gives,
,
Equation (1) and (2) are known as Vieta’s Formulas for a second degree polynomial.
In general, for an degree polynomial, there are n different Vieta’s Formulas. They can be written in a condensed form as
For
The following examples illustrate the use of Vieta’s formula to solve a problem.
Examples:
Input : n = 2 roots = {-3, 2} Output : Polynomial coefficients: -6, -1, 1 Input : n = 4 roots = {-1, 2, -3, 7} Output : Polynomial coefficients: 42 -29 -19 5 1
C++
// C++ program to implement vieta formula // to calculate polynomial coefficients. #include <bits/stdc++.h> using namespace std; // Function to calculate polynomial // coefficients. void vietaFormula( int roots[], int n) { // Declare an array for // polynomial coefficient. int coeff[n + 1]; // Set all coefficients as zero initially memset (coeff, 0, sizeof (coeff)); // Set highest order coefficient as 1 coeff[0] = 1; for ( int i = 0; i < n; i++) { for ( int j = i + 1; j > 0; j--) { coeff[j] += roots[i] * coeff[j - 1]; } } cout << "Polynomial Coefficients: " ; for ( int i = n; i >= 0; i--) { cout << coeff[i] << " " ; } } // Driver code int main() { // Degree of required polynomial int n = 4; // Initialise an array by // root of polynomial int roots[] = { -1, 2, -3, 7 }; // Function call vietaFormula(roots, n); return 0; } |
Java
import java.util.*; public class Main { // Function to calculate polynomial coefficients public static void vietaFormula( int [] roots, int n) { int [] coeff = new int [n + 1 ]; // Set all coefficients as zero initially Arrays.fill(coeff, 0 ); // Set highest order coefficient as 1 coeff[ 0 ] = 1 ; for ( int i = 0 ; i < n; i++) { for ( int j = i + 1 ; j > 0 ; j--) { coeff[j] += roots[i] * coeff[j - 1 ]; } } System.out.print( "Polynomial Coefficients: " ); for ( int i = n; i >= 0 ; i--) { System.out.print(coeff[i] + " " ); } } // Driver code public static void main(String[] args) { int n = 4 ; int [] roots = { - 1 , 2 , - 3 , 7 }; vietaFormula(roots, n); } } |
Python3
def vieta_formula(roots, n): # Initialize an array for polynomial coefficients coeff = [ 0 ] * (n + 1 ) # Set the highest order coefficient as 1 coeff[ 0 ] = 1 for i in range (n): for j in range (i + 1 , 0 , - 1 ): coeff[j] + = roots[i] * coeff[j - 1 ] # Return the coefficients list in reverse order return coeff[:: - 1 ] def main(): n = 4 roots = [ - 1 , 2 , - 3 , 7 ] # Call the vieta_formula function coefficients = vieta_formula(roots, n) print ( "Polynomial Coefficients: " , coefficients) if __name__ = = "__main__" : main() |
C#
// C# code using System; public class Program { // Function to calculate polynomial coefficients public static void vietaFormula( int [] roots, int n) { int [] coeff = new int [n + 1]; // Set all coefficients as zero initially Array.Fill(coeff, 0); // Set highest order coefficient as 1 coeff[0] = 1; for ( int i = 0; i < n; i++) { for ( int j = i + 1; j > 0; j--) { coeff[j] += roots[i] * coeff[j - 1]; } } Console.Write( "Polynomial Coefficients: " ); for ( int i = n; i >= 0; i--) { Console.Write(coeff[i] + " " ); } } // Driver code public static void Main( string [] args) { int n = 4; int [] roots = { -1, 2, -3, 7 }; vietaFormula(roots, n); } } |
Javascript
// Javascript program to implement vieta formula // to calculate polynomial coefficients. // Function to calculate polynomial // coefficients. function vietaFormula(roots, n) { // Declare an array for // polynomial coefficient. let coeff = new Array(n + 1).fill(0); // Set highest order coefficient as 1 coeff[0] = 1; for (let i = 0; i < n; i++) { for (let j = i + 1; j > 0; j--) { coeff[j] += roots[i] * coeff[j - 1]; } } console.log( "Polynomial Coefficients: " ); for (let i = n; i >= 0; i--) { console.log(coeff[i] + " " ); } } // Driver code function main() { // Degree of required polynomial let n = 4; // Initialise an array by // root of polynomial let roots = [ -1, 2, -3, 7 ]; // Function call vietaFormula(roots, n); return 0; } main(); |
Polynomial Coefficients: 42 -29 -19 5 1
Time Complexity : .
Auxiliary Space: O(n) because it is using extra space for array coeff
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!