Vieta’s Formulas

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: 1, 1, -5

Input : n = 4
        roots = {-1, 2, -3, 7}
Output : Polynomial coefficients: 1, -5, -19, 29, 42

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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[n] = 1;
  
    for (int i = 1; i <= n; i++) {
        for (int j = n - i - 1; j < n; j++) {
            coeff[j] = coeff[j] + (-1) * 
                roots[i - 1] * 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement vieta formula 
// to calculate polynomial coefficients.
import java.util.Arrays;
  
class GFG
{
  
// Function to calculate polynomial 
// coefficients.
static void vietaFormula(int roots[], int n)
{
    // Declare an array for
    // polynomial coefficient.
    int coeff[] = new int[++n + 1];
    Arrays.fill(coeff, 0);
      
    // Set highest order coefficient as 1
    coeff[n] = 1;
  
    for (int i = 1; i <n; i++)
    {
        for (int j = n - i - 1; j < n; j++)
        {
            coeff[j] = coeff[j] + (-1) * 
                roots[i - 1] * 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)
{
    // 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);
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement 
# Vieta's formula to calculate
# polynomial coefficients.
def vietaFormula(roots, n):
      
    # Declare an array for
    # polynomial coefficient.
    coeff = [0] * (n + 1)
      
    # Set Highest Order 
    # Coefficient as 1
    coeff[n] = 1
    for i in range(1, n + 1):
        for j in range(n - i - 1, n):
            coeff[j] += ((-1) * roots[i - 1] *
                                coeff[j + 1])
      
    # Reverse Array 
    coeff = coeff[::-1
      
    print("Polynomial Coefficients : ", end = "")
      
    # Print Coefficients
    for i in coeff: 
        print(i, end = " ")
    print()
  
# Driver Code
if __name__ == "__main__":
      
    # Degree of Polynomial
    n = 4
      
    # Initialise an array by
    # root of polynomial
    roots = [-1, 2, -3, 7
      
    # Function call
    vietaFormula(roots, n)
      
# This code is contributed
# by Arihant Joshi

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement vieta formula 
// to calculate polynomial coefficients.
using System;
      
class GFG
{
  
// Function to calculate polynomial 
// coefficients.
static void vietaFormula(int []roots, int n)
{
    // Declare an array for
    // polynomial coefficient.
    int []coeff = new int[++n + 1];
      
    // Set highest order coefficient as 1
    coeff[n] = 1;
  
    for (int i = 1; i <n; i++)
    {
        for (int j = n - i - 1; j < n; j++)
        {
            coeff[j] = coeff[j] + (-1) * 
                roots[i - 1] * 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)
{
    // 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);
}
}
  
// This code has been contributed by 29AjayKumar

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to implement vieta formula 
// to calculate polynomial coefficients.
  
// Function to calculate polynomial 
// coefficients.
function vietaFormula($roots, $n)
{
    // Declare an array for
    // polynomial coefficient.
    $coeff = array_fill(0, $n + 1, 0);
  
    // Set all coefficients as zero initially
      
    // Set highest order coefficient as 1
    $coeff[$n] = 1;
  
    for ($i = 1; $i <= $n; $i++)
    {
        for ($j = $n - $i; $j < $n; $j++)
        {
            $coeff[$j] = $coeff[$j] + (-1) * 
                         $roots[$i - 1] * 
                         $coeff[$j + 1];
        }
    }
  
    echo "polynomial coefficients: ";
    for ($i = $n; $i >= 0; $i--) 
    {
        echo $coeff[$i]. " ";
    }
}
  
// Driver code
  
// Degree of required polynomial
$n = 4;
  
// Initialise an array by
// root of polynomial
$roots = array(-1, 2, -3, 7);
  
// Function call
vietaFormula($roots, $n);
  
// This code is contributed by mits
?>

chevron_right


Output:

Polynomial Coefficients: 1 -5 -19 29 42 

Time Complexity : \mathcal{O}(n^{2}).



My Personal Notes arrow_drop_up

I am a problem solving enthusiast and I love competitive programming

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.