Dado un polinomio como una string y un valor. Evalúa la derivada del polinomio para el valor dado.
Nota: El formato de entrada es tal que hay un espacio en blanco entre un término y el símbolo ‘+’
La derivada de p(x) = ax^n es p'(x) = a*n*x^(n-1)
Además, si p(x) = p1(x) + p2(x)
Aquí p1 y p2 son polinomios también
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
Dividimos la string de entrada en tokens y para cada término calculamos la derivada por separado para cada término y los sumamos para obtener el resultado.
C++
// C++ program to find value of derivative of // a polynomial. #include <bits/stdc++.h> using namespace std; long long derivativeTerm(string pTerm, long long val) { // Get coefficient string coeffStr = ""; int i; for (i = 0; pTerm[i] != 'x'; i++) coeffStr.push_back(pTerm[i]); long long coeff = atol(coeffStr.c_str()); // Get Power (Skip 2 characters for x and ^) string powStr = ""; for (i = i + 2; i != pTerm.size(); i++) powStr.push_back(pTerm[i]); long long power = atol(powStr.c_str()); // For ax^n, we return anx^(n-1) return coeff * power * pow(val, power - 1); } long long derivativeVal(string& poly, int val) { long long ans = 0; // We use istringstream to get input in tokens istringstream is(poly); string pTerm; while (is >> pTerm) { // If the token is equal to '+' then // continue with the string if (pTerm == "+") continue; // Otherwise find the derivative of that // particular term else ans = (ans + derivativeTerm(pTerm, val)); } return ans; } // Driver code int main() { string str = "4x^3 + 3x^1 + 2x^2"; int val = 2; cout << derivativeVal(str, val); return 0; }
Java
// Java program to find value of derivative of // a polynomial import java.io.*; class GFG { static long derivativeTerm(String pTerm, long val) { // Get coefficient 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); // Get Power (Skip 2 characters for x and ^) String powStr = ""; for (i = i + 2; i != pTerm.length() && pTerm.charAt(i) != ' '; i++) { powStr += pTerm.charAt(i); } long power=Long.parseLong(powStr); // For ax^n, we return a(n)x^(n-1) 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; } // Driver code public static void main (String[] args) { String str = "4x^3 + 3x^1 + 2x^2"; int val = 2; System.out.println(derivativeVal(str, val)); } } // This code is contributed by avanitrachhadiya2155
Python3
# Python3 program to find # value of derivative of # a polynomial. def derivativeTerm(pTerm, val): # Get coefficient coeffStr = "" i = 0 while (i < len(pTerm) and pTerm[i] != 'x'): coeffStr += (pTerm[i]) i += 1 coeff = int(coeffStr) # Get Power (Skip 2 characters # for x and ^) powStr = "" j = i + 2 while j < len(pTerm): powStr += (pTerm[j]) j += 1 power = int(powStr) # For ax^n, we return # a(n)x^(n-1) 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 # Driver code if __name__ == "__main__": st = "4x^3 + 3x^1 + 2x^2" val = 2 print(derivativeVal(st, val)) # This code is contributed by Chitranayal
C#
// C# program to find value of derivative of // a polynomial using System; class GFG{ static long derivativeTerm(string pTerm, long val) { // Get coefficient string coeffStr = ""; int i; for(i = 0; pTerm[i] != 'x'; i++) { if (pTerm[i] == ' ') continue; coeffStr += (pTerm[i]); } long coeff = long.Parse(coeffStr); // Get Power (Skip 2 characters for x and ^) string powStr = ""; for(i = i + 2; i != pTerm.Length && pTerm[i] != ' '; i++) { powStr += pTerm[i]; } long power = long.Parse(powStr); // For ax^n, we return a(n)x^(n-1) 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; } // Driver code static public void Main() { String str = "4x^3 + 3x^1 + 2x^2"; int val = 2; Console.WriteLine(derivativeVal(str, val)); } } // This code is contributed by rag2127
Javascript
<script> // Javascript program to find value of derivative of // a polynomial function derivativeTerm( pTerm,val) { // Get coefficient let coeffStr = ""; let i; for (i = 0; pTerm[i] != 'x' ; i++) { if(pTerm[i]==' ') continue; coeffStr += (pTerm[i]); } let coeff = parseInt(coeffStr); // Get Power (Skip 2 characters for x and ^) let powStr = ""; for (i = i + 2; i != pTerm.length && pTerm[i] != ' '; i++) { powStr += pTerm[i]; } let power=parseInt(powStr); // For ax^n, we return a(n)x^(n-1) 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; } // Driver code let str = "4x^3 + 3x^1 + 2x^2"; let val = 2; document.write(derivativeVal(str, val)); // This code is contributed by ab2127 </script>
Producción:
59
Este artículo es una contribución de Ankit Jain . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA