Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Absolute difference between sum and product of roots of a quartic equation

  • Last Updated : 22 Apr, 2021

Given a quartic equation in the form ax^4+bx^3+cx^2+dx+e  , determine the absolute difference between the sum of its roots and the product of its roots. Note that roots need not be real – they can also be complex.
Examples: 
 

Input: 4x^4 + 3x^3 + 2x^2 + x - 1
Output: 0.5

Input: x^4 + 4x^3 + 6x^2 + 4x + 1
Output: 5

 

Approach: Solving the quartic equation to obtain each individual root would be time-consuming and inefficient, and would require much effort and computational power. A more efficient solution utilises the following formulae:
 

The quartic ax^4+bx^3+cx^2+dx+e always has sum of roots \dfrac{-b}{a},
and product of roots \dfrac{e}{a}. 

Hence by computing |\dfrac{-b}{a}- \dfrac{e}{a}|  we find the absolute difference between sum and product of roots.
Below is the implementation of above approach: 
 

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function taking coefficient of
// each term of equation as input
double sumProductDifference(int a, int b,
                            int c, int d,
                            int e)
{
    // Finding sum of roots
    double rootSum = (double)(-1 * b) / a;
 
    // Finding product of roots
    double rootProduct = (double) e / a;
     
    // Absolute difference
    return abs(rootSum - rootProduct);
}
 
// Driver code
int main()
{
    cout << sumProductDifference(8, 4, 6, 4, 1);
     
    return 0;
}
 
// This code is contributed
// by ANKITRAI1

Java




// Java implementation of above approach
 
public class GFG {
    // Function taking coefficient of
    // each term of equation as input
    static double sumProductDifference(int a, int b, int c, int d, int e) {
        // Finding sum of roots
        double rootSum = (double)(-1 * b) / a;
 
        // Finding product of roots
        double rootProduct = (double) e / a;
         
        // Absolute difference
        return Math.abs(rootSum - rootProduct);
    }
 
    // Driver Code
    public static void main(String args[]) {
        System.out.println(sumProductDifference(8, 4, 6, 4, 1));
    }
}

Python3




# Python implementation of above approach
 
# Function taking coefficient of
# each term of equation as input
def sumProductDifference(a, b, c, d, e):
 
    # Finding sum of roots
    rootSum = (-1 * b)/a
 
    # Finding product of roots
    rootProduct = e / a
 
    # Absolute difference
    return abs(rootSum-rootProduct)
 
print(sumProductDifference(8, 4, 6, 4, 1))

C#




// C# implementation of above approach
using System;
 
class GFG
{
// Function taking coefficient of
// each term of equation as input
static double sumProductDifference(int a, int b,
                                   int c, int d,
                                   int e)
{
    // Finding sum of roots
    double rootSum = (double)(-1 * b) / a;
 
    // Finding product of roots
    double rootProduct = (double) e / a;
     
    // Absolute difference
    return Math.Abs(rootSum - rootProduct);
}
 
// Driver Code
public static void Main()
{
    Console.Write(sumProductDifference(8, 4, 6, 4, 1));
}
}
 
// This code is contributed
// by ChitraNayal

PHP




<?php
// PHP implementation of above approach
 
// Function taking coefficient of
// each term of equation as input
function sumProductDifference($a, $b,
                              $c, $d,$e)
{
    // Finding sum of roots
    $rootSum = (double)(-1 * $b) / $a;
 
    // Finding product of roots
    $rootProduct = (double) $e / $a;
     
    // Absolute difference
    return abs($rootSum - $rootProduct);
}
 
// Driver code
echo sumProductDifference(8, 4, 6, 4, 1);
     
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript




<script>
 
// Javascript implementation of above approach
 
// Function taking coefficient of
// each term of equation as input
function sumProductDifference(a, b, c, d, e)
{
     
    // Finding sum of roots
    var rootSum = (-1 * b) / a;
 
    // Finding product of roots
    var rootProduct = e / a;
     
    // Absolute difference
    return Math.abs(rootSum - rootProduct);
}
 
 
// Driver Code
document.write(sumProductDifference(8, 4, 6, 4, 1));
 
// This code is contributed by Ankita saini
 
</script>
Output: 
0.625

 

Explanation: The input equation is 8x^4+4x^3+6x^2+4x+1
By finding |\dfrac{-b}{a}-\dfrac{e}{a}|  , we get |\dfrac{-4}{8} - \dfrac{1}{8}|  ,
which is \dfrac{5}{8}  , or 0.625  .
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!