Open In App

Vieta’s Formulas

Improve
Improve
Like Article
Like
Save
Share
Report

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 r_{1}                and r_{2}                , then find a polynomial. 
Consider the polynomial a_{2}x^{2} + a_{1}x + a_{0}                . Given the roots, we can also write it as 
k(x - r_{1})(x - r_{2})                .
Since both equation represents the same polynomial, so equate both polynomial
a_{2}x^{2} + a_{1}x + a_{0} = k(x - r_{1})(x - r_{2})
Simplifying the above equation, we get
a_{2}x^{2} + a_{1}x + a_{0} = kx^{2}-k(r_{1} + r_{2})x + k(r_{1}r_{2})
Comparing the coefficients of both sides, we get
For x^{2}                a_{2} = k                ,
For x                a_{1} = -k(r_{1} + r_{2})
For constant term, a_{0} = kr_{1}r_{2}                ,
Which gives, 
a_{2} = k                ,
\frac{a_{1}}{a_{2}} = -(r_{1} + r_{2})........(1)
\frac{a_{0}}{a_{2}} = r_{1}r_{2}..................(2)
Equation (1) and (2) are known as Vieta’s Formulas for a second degree polynomial. 
In general, for an nth                degree polynomial, there are n different Vieta’s Formulas. They can be written in a condensed form as 
For 0 \leq k \leq n
\sum_{1 \leq i_{1} < i_{2} < ... < i_{k} \leq n} (r_{i_{1}}r_{i_{2}} ... r_{i_{k}}) = (-1)^{k}\frac{a_{n-k}}{a_{n}}.

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();

                    

Output
Polynomial Coefficients: 42 -29 -19 5 1 

Time Complexity : \mathcal{O}(n^{2})                .
Auxiliary Space: O(n) because it is using extra space for array coeff
 



Last Updated : 13 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads