Program for Derivative of a Polynomial
Last Updated :
18 Sep, 2023
Given a polynomial as a string and a value. Evaluate polynomial’s derivative for the given value.
Note: The input format is such that there is a white space between a term and the ‘+’ symbol
The derivative of p(x) = ax^n is p'(x) = a*n*x^(n-1)
Also, if p(x) = p1(x) + p2(x)
Here p1 and p2 are polynomials too
p'(x) = p1′(x) + p2′(x)
Input : 3x^3 + 4x^2 + 6x^1 + 89x^0
2
Output :58
Explanation : Derivative of given
polynomial is : 9x^2 + 8x^1 + 6
Now put x = 2
9*4 + 8*2 + 6 = 36 + 16 + 6 = 58
Input : 1x^3
3
Output : 27
We split the input string into tokens and for each term calculate the derivative separately for each term and add them to get the result.
C++
#include <bits/stdc++.h>
using namespace std;
long long derivativeTerm(string pTerm, long long val)
{
string coeffStr = "" ;
int i;
for (i = 0; pTerm[i] != 'x' ; i++)
coeffStr.push_back(pTerm[i]);
long long coeff = atol (coeffStr.c_str());
string powStr = "" ;
for (i = i + 2; i != pTerm.size(); i++)
powStr.push_back(pTerm[i]);
long long power = atol (powStr.c_str());
return coeff * power * pow (val, power - 1);
}
long long derivativeVal(string& poly, int val)
{
long long ans = 0;
istringstream is(poly);
string pTerm;
while (is >> pTerm) {
if (pTerm == "+" )
continue ;
else
ans = (ans + derivativeTerm(pTerm, val));
}
return ans;
}
int main()
{
string str = "4x^3 + 3x^1 + 2x^2" ;
int val = 2;
cout << derivativeVal(str, val);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long derivativeTerm(String pTerm, long val)
{
String coeffStr = "" ;
int i;
for (i = 0 ; pTerm.charAt(i) != 'x' ; i++)
{
if (pTerm.charAt(i)== ' ' )
continue ;
coeffStr += (pTerm.charAt(i));
}
long coeff = Long.parseLong(coeffStr);
String powStr = "" ;
for (i = i + 2 ; i != pTerm.length() && pTerm.charAt(i) != ' ' ; i++)
{
powStr += pTerm.charAt(i);
}
long power=Long.parseLong(powStr);
return coeff * power * ( long )Math.pow(val, power - 1 );
}
static long derivativeVal(String poly, int val)
{
long ans = 0 ;
int i = 0 ;
String[] stSplit = poly.split( "\\+" );
while (i<stSplit.length)
{
ans = (ans +derivativeTerm(stSplit[i], val));
i++;
}
return ans;
}
public static void main (String[] args) {
String str = "4x^3 + 3x^1 + 2x^2" ;
int val = 2 ;
System.out.println(derivativeVal(str, val));
}
}
|
Python3
def derivativeTerm(pTerm, val):
coeffStr = ""
i = 0
while (i < len (pTerm) and
pTerm[i] ! = 'x' ):
coeffStr + = (pTerm[i])
i + = 1
coeff = int (coeffStr)
powStr = ""
j = i + 2
while j < len (pTerm):
powStr + = (pTerm[j])
j + = 1
power = int (powStr)
return (coeff * power *
pow (val, power - 1 ))
def derivativeVal(poly, val):
ans = 0
i = 0
stSplit = poly.split( "+" )
while (i < len (stSplit)):
ans = (ans +
derivativeTerm(stSplit[i],
val))
i + = 1
return ans
if __name__ = = "__main__" :
st = "4x^3 + 3x^1 + 2x^2"
val = 2
print (derivativeVal(st, val))
|
C#
using System;
class GFG{
static long derivativeTerm( string pTerm, long val)
{
string coeffStr = "" ;
int i;
for (i = 0; pTerm[i] != 'x' ; i++)
{
if (pTerm[i] == ' ' )
continue ;
coeffStr += (pTerm[i]);
}
long coeff = long .Parse(coeffStr);
string powStr = "" ;
for (i = i + 2;
i != pTerm.Length && pTerm[i] != ' ' ;
i++)
{
powStr += pTerm[i];
}
long power = long .Parse(powStr);
return coeff * power * ( long )Math.Pow(val, power - 1);
}
static long derivativeVal( string poly, int val)
{
long ans = 0;
int i = 0;
String[] stSplit = poly.Split( "+" );
while (i < stSplit.Length)
{
ans = (ans +derivativeTerm(stSplit[i], val));
i++;
}
return ans;
}
static public void Main()
{
String str = "4x^3 + 3x^1 + 2x^2" ;
int val = 2;
Console.WriteLine(derivativeVal(str, val));
}
}
|
Javascript
<script>
function derivativeTerm( pTerm,val)
{
let coeffStr = "" ;
let i;
for (i = 0; pTerm[i] != 'x' ; i++)
{
if (pTerm[i]== ' ' )
continue ;
coeffStr += (pTerm[i]);
}
let coeff = parseInt(coeffStr);
let powStr = "" ;
for (i = i + 2; i != pTerm.length && pTerm[i] != ' ' ; i++)
{
powStr += pTerm[i];
}
let power=parseInt(powStr);
return coeff * power * Math.pow(val, power - 1);
}
function derivativeVal(poly,val)
{
let ans = 0;
let i = 0;
let stSplit = poly.split( "+" );
while (i<stSplit.length)
{
ans = (ans +derivativeTerm(stSplit[i], val));
i++;
}
return ans;
}
let str = "4x^3 + 3x^1 + 2x^2" ;
let val = 2;
document.write(derivativeVal(str, val));
</script>
|
Time Complexity: O(n), where n is the number of terms in the polynomial.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...