Given string str which represents a polynomial and a number N, the task is to integrate this polynomial with respect to X at the given value N.
Examples:
Input: str = “90x4 + 24x3 + 18x2 + 18x”, N = 1.
Output: 39
Explanation:
Given, dy/dx = 90*(X4) + 24* (X3) + 18* (X2) + 18*(X). On integrating this equation, we get 18*(X5) + 6*(X4) + 6*(X3) + 9*(X2) and substituting the value X = 1, we get:
18 + 6 + 6 + 9 = 39.
Input: str = “4x3 + 2x2 + 3x”, N = 2
Output: 27
Approach: The idea is to use the identity of the integration. For some given function X with the power of N, the integration of this term is given by:
![]()
Therefore, the following steps are followed to compute the answer:
- Get the string.
- Split the string and perform the integration based on the above formula.
- Substitute the value of N in the obtained expression.
- Add all the individual values to get the final integral value.
Below is the implementation of the above approach:
C++
// C++ program to find the integration// of the given polynomial for the// value N#include <bits/stdc++.h>using namespace std;typedef long long ll;// Function to return the integral// of the given termdouble inteTerm(string pTerm, ll val){ // Get the coefficient string coeffStr = ""; int i; // Loop to iterate through the string // and get the coefficient for (i = 0; pTerm[i] != 'x'; i++) coeffStr.push_back(pTerm[i]); ll coeff = atol(coeffStr.c_str()); // Get the Power string powStr = ""; // Loop to skip 2 characters for x and ^ for (i = i + 2; i != pTerm.size(); i++) powStr.push_back(pTerm[i]); ll power = atol(powStr.c_str()); // Return the computed integral return (coeff * pow(val, power + 1)) / (power + 1);}// Functionto find the integration// of the given polynomial for the// value Ndouble integrationVal(string poly, int val){ ll ans = 0; // Using string stream to get the // 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 integration // of that particular term else ans = (ans + inteTerm(pTerm, val)); } return ans;}// Driver codeint main(){ string str = "4x^3 + 3x^1 + 2x^2"; int val = 2; cout << integrationVal(str, val); return 0;} |
Python3
# Python3 program to find # the integration of the # given polynomial for the# value N# Function to return the integral# of the given termdef inteTerm(pTerm, val): # Get the coefficient coeffStr = "" # Loop to iterate through # the string and get the # coefficient i = 0 while (i < len(pTerm) and pTerm[i] != 'x'): coeffStr += pTerm[i] i += 1 coeff = int(coeffStr) # Get the Power powStr = "" # Loop to skip 2 characters # for x and ^ j = i + 2 while j< len(pTerm): powStr += (pTerm[j]) j += 1 power = int(powStr) # Return the computed integral return ((coeff * pow(val, power + 1)) // (power + 1))# Functionto find the integration# of the given polynomial for the# value Ndef integrationVal(poly, val): ans = 0 # Using string stream to # get the input in tokens stSplit = poly.split("+") i = 0 while i < len(stSplit): ans = (ans + inteTerm(stSplit[i], val)) i += 1 return ans# Driver codeif __name__ == "__main__": st = "4x^3 + 3x^1 + 2x^2" val = 2 print(integrationVal(st, val))# This code is contributed by Chitranayal |
27
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program to find the indefinite Integration of the given Polynomial
- Sgn value of a polynomial
- Minimize the sum of roots of a given polynomial
- Program to differentiate the given Polynomial
- Horner's Method for Polynomial Evaluation
- Complete the sequence generated by a polynomial
- Finding nth term of any Polynomial Sequence
- Python | Finding Solutions of a Polynomial Equation
- Content of a Polynomial
- String hashing using Polynomial rolling hash function
- Program for Derivative of a Polynomial
- Solving Homogeneous Recurrence Equations Using Polynomial Reduction
- Partial derivative of a polynomial using Doubly Linked List
- Polynomial Division using Linked List
- Check if a given value can be reached from another value in a Circular Queue by K-length jumps
- Queries to increment array elements in a given range by a given value for a given number of times
- Only integer with positive value in positive negative value in array
- Find the maximum possible value of the minimum value of modified array
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- Minimum value possible of a given function from the given set
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.
Improved By : chitranayal

