NumericalIntegral
SRM 187 · 2004-03-16 · by Rustyoldman
Problem Statement
Given two real numbers x1 and x2, calculate an approximation to the integral of e-x^2 evaluated between the limits from x1 to x2, which is accurate to the nearest 0.00001.
Return the answer in a
For example: x1 = -0.5 and x2 = 0.5 returns "0.92256"
Notes
- e-x^2 can be calculated in C++ with exp(-x*x) in math.h. e-x^2 can be calculated in C# with Math.Exp(-x*x). The Math class is in the System namespace.e-x^2 can be calculated in Java with Math.exp(-x*x). e-x^2 can be calculated in Visual Basic with Exp(-x*x) in the System.Math namespace.
- The integral of a function is the area inside the closed figure formed by (on the top) the function between the limits of x=x1 and x=x2, (on the sides) vertical line segments at x=x1 and x=x2, and (on the bottom) the portion of the x axis between x=x1 and x=x2. This is shown by the shaded area above (the graph shows the function we are integrating, e-x^2).
- The integral of e-x^2 is known to have no closed form, so don't waste time looking in a table of integrals for an exact formula.
- Because of the 2e-6 constraint, about 40% of randomly chosen x1 and x2 values will be too close to a possible rounding error and will be rejected. This is not an error. It gives you more room for numerical errors.
Constraints
- x1 will be less than x2.
- x2-x1 will be between 0.00001 and 1.00000 inclusive.
- x1 will be between -10.0 and 10.0 inclusive.
- x2 will be between -10.0 and 10.0 inclusive.
- To avoid rounding errors the inputs x1 and x2 must be chosen so that the answer is not within 2e-6 of 0.000005 + a multiple of 0.00001
-0.5 0.5 Returns: "0.92256"
The example from above. This is the largest possible answer given the constraints of this problem.
0.0 0.1 Returns: "0.09967"
2.0 2.451 Returns: "0.00368"
-9.0001 -9.0 Returns: "0.00000"
Values are very small out here.
2.5 3.0 Returns: "0.00034"
1.22 1.23 Returns: "0.00223"
This is where the derivative is highest.
Submissions are judged against all 23 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class NumericalIntegral with a public method string integrate(double x1, double x2) · 23 test cases · 2 s / 256 MB per case