Definite integral of polynomial functions
Jelly, \$\frac {30} 7 = 4.29\$
÷J$ŻUḅI
Try it online!
Takes the polynomial as a list of coefficients in little-endian format (i.e. the example is 7,2,5,4
). This can handle any (positive) degree you want, but I've limited it at 30 as the question states \$0 < N < 30\$
+2 bytes and a score of \$3.33\$ if the polynomial must be taken in big-endian format
How it works
÷J$ŻUḅI - Main link. Takes coeffs on the left and [a, b] on the right
$ - To the coeffs:
J - Yield [1, 2, 3, ..., N]
÷ - Divide each coeff by the orders
Ż - Prepend 0
U - Reverse
ḅ - Calculate as polynomials, with x = a and x = b
I - Reduce by subtraction
J, \$\frac{30}{12}\approx2.5\$
[:-/[-p..p.[
Try it online!
-1 thanks to Bubbler
p..
Integral of a polynomial. Returns a list representing the solution polynomial.p.
Evaluate polynomial at given bounds.[-
Subtract from constant terms (makes both values negative).-/
And subtract negative ending bound answer from negative starting bound answer.
Factor, \$\frac{30}{56}\approx0.5357\$
[ [ 1 + 3dup nip v^n first2 - swap / * ] map-index sum ]
Try it online!
Takes the bounds and coefficients in reverse, e.g. { 3 2 } { 7 2 5 4 }
, and returns a rational number, e.g. 108+2/3
.
To take the inputs as given and return a float, add a reverse
, a neg
, and a >float
to get 75 bytes:
Factor, \$\frac{30}{75}=0.4\$
[ reverse [ 1 + 3dup nip v^n first2 - swap / * ] map-index sum neg >float ]
Try it online!