Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Vieta's Formulas
Next article icon

Vieta's Formulas

Last Updated : 13 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
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
 


Next Article
Vieta's Formulas

A

AayushChaturvedi
Improve
Article Tags :
  • Misc
  • Mathematical
  • Engineering Mathematics Questions
  • DSA
  • maths-polynomial
  • expression-evaluation
Practice Tags :
  • Mathematical
  • Misc

Similar Reads

    General and Middle Terms - Binomial Theorem - Class 11 Maths
    Binomial theorem or expansion describes the algebraic expansion of powers of a binomial. According to this theorem, it is possible to expand the polynomial "(a + b)n" into a sum involving terms of the form "axzyc", the exponents z and c are non-negative integers where z + c = n, and the coefficient
    7 min read
    Types of Polynomials (Based on Terms and Degrees)
    Types of Polynomials: In mathematics, an algebraic expression is an expression built up from integer constants, variables, and algebraic operations. There are mainly four types of polynomials based on degree-constant polynomial (zero degree), linear polynomial ( 1st degree), quadratic polynomial (2n
    9 min read
    Polynomials in One Variable | Polynomials Class 9 Maths
    Polynomials in One Variable: Polynomial word originated from two words “poly” which means “many” and the word “nominal” which means “term”. In maths, a polynomial expression consists of variables known as indeterminate and coefficients. Polynomials are expressions with one or more terms with a non-z
    7 min read
    Vieta's Formula
    Vieta's Formulas provide a way to relate the coefficients of a polynomial to the sums and products of its roots. Named after the French mathematician François Viète, these formulas establish a connection between the roots of a polynomial and its coefficients.If you have a polynomial equation and kno
    8 min read
    Class 8 RD Sharma Solutions- Chapter 6 Algebraic Expressions And Identities - Exercise 6.6 | Set 1
    Question 1. Write the following squares of binomials as trinomials:(i) (x + 2)2 Solution: x2 + 2 (x) (2) + 22 x2 + 4x + 4 (ii) (8a + 3b)2 Solution: (8a)2 + 2 (8a) (3b) + (3b) 64a2 + 48ab + 9b2 (iii) (2m + 1)2 Solution: (2m)2 + 2 (2m) (1) + 12 4m2 + 4m + 1 (iv) (9a + 1/6)2 Solution: (9a)2 + 2 (9a) (1
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences