| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | Wolfram Language Mathematica | 210216T011408Z | att |
| nan | JavaScript Node.js | 240412T170221Z | 2pichar |
| 014 | APLNARS | 240111T141237Z | Rosario |
| 055 | R | 210323T165623Z | Giuseppe |
| nan | C gcc lm | 210217T105957Z | ErikF |
| nan | JavaScript | 210216T094112Z | user1006 |
| nan | 05AB1E | 210216T083426Z | Kevin Cr |
| nan | J | 210216T003434Z | Jonah |
| nan | Charcoal | 210216T004910Z | Neil |
| nan | Factor | 210216T003400Z | Bubbler |
| nan | APL Dyalog Unicode | 210216T001312Z | user |
| nan | SageMath | 210216T000034Z | Noodle9 |
| nan | Scala | 210215T233601Z | user |
| nan | Jelly | 210215T223915Z | caird co |
Wolfram Language (Mathematica), 30/32=0.9375 30/31=0.9677 \$\frac{30}{30}=1\$
{-1,a=1}.Sum[i/a#2^a++,{i,#}]&
Input [{coeffs...}, {a, b}]. Takes coefficients in ascending order.
some older solutions:
31 bytes
c(a=0;c^++a.{-1,1}/a&/@#).#&
Try it online! Input [{a, b}][{coeffs...}].
32 bytes
FromDigits[#,]~Integrate~{,##2}&
Try it online! Input [{coeffs...}, a, b]. Integrates using Null as the variable.
JavaScript (Node.js), \$\frac{30}{70}\approx0.4286\$
I=b=>c=>(F=x=>c.reduce((s,e,i)=>s+(e/(i+1)*x**(i+1)),0))(b[1])-F(b[0])
Takes coefficients in ascending order (\$k_0,\dots,k_n\$ for \$x^0,\dots,x^n\$)
APL(NARS), 14 chars
a←⎕⋄-/{⍵⊥a}∫¨⎕
For the input of polynom the list
$$ a_n a_{n-1} ... a_1 a_0 $$
means polynom
$$ a_n X^n+a_{n-1} X^{n-1} ... a_1 X+a_0 $$
test:
a←⎕⋄-/{⍵⊥a}∫¨⎕
⎕:
4 5 2 7
⎕:
3 2
108.6666667
R, 55 bytes, score: \$\frac{30}{55}=0.\overline{54}\$
function(b,k)diff(outer(b,a<-rev(seq(!k)),"^")%*%(k/a))
If we could take the coefficients in ascending order of degree, this would be 5 bytes shorter and get a score of \$0.6\$ instead, by removing the rev().
C (gcc) (-lm), score: \$\frac{30}{90}= 0.\bar3\$
Sums the bounds for each term in descending order. Due to quirks with -O0, I was able to write this function without the usual return or explicit value assignment at the end of the function.
double f(b,t,n,i,j)int*b,*t;{for(double s=i=0;j=n-i;)s+=t[i++]*(pow(b[1],j)-pow(*b,j))/j;}
JavaScript, score: \$\frac{30}{71}\approx 0.42\$
n=>k=>(x=y=>k.reduce((a,b,c)=>a+b*y**(z=k.length-c)/z,0))(n[1])-x(n[0])
Demonstration:
f=n=>k=>(x=y=>k.reduce((a,b,c)=>a+b*y**(z=k.length-c)/z,0))(n[1])-x(n[0]);
// test case
console.log(f([2,3])([4,5,2,7]));
// large polynomial of degree 30
console.log(f([3,5])(new Array(31).fill(1) /*shortcut*/));
How it works
Constructs a function dynamically which calls reduce on the coefficients array. Then invokes it on both bounds and takes the difference.
05AB1E, \$\frac{30}{8} = 3.75\$
āR/0ªIβÆ
Port of @Neil's Charcoal answer, so make sure to upvote him as well!
First input is the list of coefficients; second is a pair of \$[b,a]\$ bounds.
Explanation:
ā # Push a list in the range [1, (implicit) coefficients-length]
R # Reverse this range to [length,1]
/ # Divide the coefficients by the ranged list (at the same postitions)
0ª # Append a 0 at the end of this list
Iβ # Convert this list to base-b and base-a using the second input-pair
Æ # And reduce that pair by subtracting
# (after which the result is output implicitly)
J, \$\frac{30}{12}\approx2.5\$
[:-/[-p..p.[
-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.
Charcoal, \$ \frac {30} {20} = 1.5 \$
UMθ∕ι⁻Lθκ⊞θ⁰I⁻↨θζ↨θη
Try it online! Link is to verbose version of code. Explanation:
UMθ∕ι⁻Lθκ
Divide each element of the polynomial by the 1-indexed reverse index.
⊞θ⁰
Append a zero.
I⁻↨θζ↨θη
Calculate the value at each bound and take the difference.
Factor, \$\frac{30}{56}\approx0.5357\$
[ [ 1 + 3dup nip v^n first2 - swap / * ] map-index sum ]
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 ]
APL (Dyalog Unicode), \$\frac{30}{20}=1.5\$
{-/⌽⊥∘(0,⍨⍵÷⌽⍳≢⍵)¨⍺}
Takes the bounds on the left and the coefficients on the right. This is a great place to use APL's ⊥, which can evaluate polynomials (although appending a zero is necessary because we want to use n+1 and not n).
SageMath, \$\frac{30}{67}\approx 0.44776\$
lambda c,b:RR(integrate(sum(a*x**e for e,a in enumerate(c)),[x]+b))
Inputs the coefficients (from the constant to the highest degree) and bounds as lists.
Can surely work for much higher degrees of a polynomial but got tired waiting for it to finish \$10000\$.
Works quite quickly for degree of \$3000\$.
Scala, \$\frac{30}{88}\approx0.3409\$
p=>_.:\(.0)((x,r)=>p.reverse.zipWithIndex.map{case(k,n)=>k*math.pow(x,n+1)/(n+1)}.sum-r)
The question's IO format actually works out quiet well here (although I could save 8 bytes by taking the coefficients in reverse to avoid .reverse).
Jelly, \$\frac {30} 7 = 4.29\$
÷J$ŻUḅI
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