| Bytes | Lang | Time | Link |
|---|---|---|---|
| 115 | Pascal | 250818T000001Z | Kai Burg |
| 052 | JavaScript | 160513T091944Z | ericw314 |
| 027 | CJam | 141223T093156Z | jimmy230 |
| 043 | CJam | 141223T072520Z | Optimize |
| 071 | R | 141221T230913Z | Alex A. |
Pascal, 115 B (non‐competing)†
Declaring and defining your own function in Pascal introduces a ton of overhead; it is shorter to expand the integral: $$ \begin{align} \int_{d}^{e} \! \left( a \, x^{2} + b \, x + c \right) \mathrm{d}x =& \int_{d}^{e} \! \left( a \, x^{2} \right) \mathrm{d}x + \int_{d}^{e} \! \left( b \, x^{1} \right) \mathrm{d}x + \int_{d}^{e} \! \left( c \, x^{0} \right) \mathrm{d}x \\ =&\; a \int_{d}^{e} \! \left(x^{2} \right) \mathrm{d}x + b \int_{d}^{e} \! \left(x^{1} \right) \mathrm{d}x + c \int_{d}^{e} \! \left(x^{0} \right) \mathrm{d}x \\ =&\; a \left( \left. \frac{x^3}{3} + p \right) \right|_{d}^{e} + b \left( \left. \frac{x^2}{2} + q \right) \right|_{d}^{e} + c \left( \left. \frac{x^1}{1} + r \right) \right|_{d}^{e} \\ = &\;a \left( \left(\frac{e^3}{3} + p \right) - \left(\frac{d^3}{3} + p \right) \right) + \\ &\;b \left( \left(\frac{e^2}{2} + q \right) - \left(\frac{d^2}{2} + q \right) \right) + \\ &\;c \left( \left(\frac{e^1}{1} + r \right) - \left(\frac{d^1}{1} + r \right) \right) \\ = &\;a \left( \frac{e^3}{3} + p - \frac{d^3}{3} - p \right) + \\ &\;b \left( \frac{e^2}{2} + q - \frac{d^2}{2} - q \right) + \\ &\;c \left( \frac{e^1}{1} + r - \frac{d^1}{1} - r \right) \\ =&\; a \left( \frac{e^3}{3} - \frac{d^3}{3} \right) + b \left( \frac{e^2}{2} - \frac{d^2}{2} \right) + c \left( \frac{e^1}{1} - \frac{d^1}{1} \right) \\ =&\; a \left( \frac{e^3 - d^{3}}{3} \right) + b \left( \frac{e^2 - d^{2}}{2} \right) + c \left( \frac{e^1 - d^{1}}{1} \right) \\ =&\; \frac{a \left( e^3 - d^{3} \right) }{3} + \frac{b \left( e^2 - d^{2} \right) }{2} + c \left( e - d \right) \end{align} $$
program p(input,output);var a,b,c,d,e:real;begin
read(a,b,c,d,e);write(a*(e*e*e-d*d*d)/3+b*(e*e-d*d)/2+c*(e-d))end.
Pretty‐printed:
program areaUnderACurve(input, output);
var
a, b, c, d, e: real;
begin
readLn(a, b, c, d, e);
writeLn(a * (e * e * e - d * d * d) / 3 +
b * (e * e - d * d ) / 2 +
c * (e - d ) )
end.
†Pascal’s built‐in data type real provides only an implementation‐defined “reasonable” approximation of the actual mathematically correct and precise result, hence it is impossible to establish a precision of ±10−3.
In practice values of large magnitude lead to errors exceeding the required grade of precision.
JavaScript, 52 bytes
(a,b,c,d,e)=>a*e^3/3+b*e^2/2+c*e-a*d^3/3-b*d^2/2-c*d
Calculates the indefinite integral at e and d, then subtract.
CJam, 27 bytes
q~W%2/~~[2*\~3*\6*0]fb:-6d/
Example input:
[1 2 3 4 8]
Output:
209.33333333333334
CJam, 46 43 bytes
l~\:D-A3#:K*),])\f{\dK/D+\~\Z$*+@_*@*+K/}:+
The arguments are read via STDIN like
1 2 3 4 7
and the output is printed to STDOUT like
135.04650050000018
You can increase the precision by changing the 3 in A3#:K to a higher number. The higher the number, the longer the program will take to finish.
R, 71 bytes
If the input is a vector x <- c(a, b, c, d, e) then you can compute the integral directly as per @feersum's comment.
e=x[5];d=x[4];x[1]*e^3/3+x[2]*e^2/2+x[3]*e-x[1]*d^3/3-x[2]*d^2/2-x[3]*d