| Bytes | Lang | Time | Link |
|---|---|---|---|
| 009 | J | 241005T225430Z | south |
| 008 | Vyxal | 241003T094230Z | emanresu |
| 009 | 05AB1E | 241004T070008Z | Kevin Cr |
| 009 | APLDyalog Unicode | 241003T083724Z | akamayu |
| 044 | JavaScript ES6 | 241003T025930Z | Arnauld |
| 041 | Ruby | 241003T091617Z | G B |
| 015 | Uiua 0.13.0dev.2 | 241003T002534Z | Tbw |
| 018 | Charcoal | 241003T055940Z | Neil |
J, 9 bytes
%&!*-![+-
Computes the same thing as everyone else. n as left arg, k as right arg.
%&!*-![+-
- NB. n-k
[ NB. n
+ NB. addition
- NB. n-k
! NB. combinations, x!y -> C(y,x)
%&! NB. take factorial of n and k and divide the results
* NB. multiplication
More simply put
%&!*-![+-
-![+- NB. (n-k)!(n+n-k)
%&! NB. (n!)%(k!)
* NB. multiply the results
Vyxal, 9 8 bytes
2ʀ*Ṙ/
Getting the inputs to work nicely was annoying, and it's likely that there's a byte to be shaved here (I wish vysearch was a thing). Same formula as everyone else. -1 inspired by Kevin Cruijssen's 05AB1E answer.
2ʀ # [0, 1, 2]
*Ṙ # [2n, n, 0]
ε # [2n-k, n-k, k]
¡ # Factorial of each
ƒ/ # (2n-k)! / (n-k)! / k!
05AB1E, 9 bytes
xs0)α!.»÷
Inputs in the order \$n,k\$.
Try it online or verify all test cases.
Explanation:
x # Double the first (implicit) input (without popping)
# STACK: n,2n
s # Swap the two values
# STACK: 2n,n
0 # Push 0
# STACK: 2n,n,0
) # Wrap all values on the stack into a list
# STACK: [2n,n,0]
α # Get the absolute difference with the second (implicit) input
# STACK: [abs(k-2n),abs(k-n),abs(k-0)], aka [2n-k,n-k,k]
! # Get the factorial of each
# STACK: [(2n-k)!,(n-k)!,k!]
.» # Left-reduce this list by:
÷ # Integer-dividing
# STACK: (2n-k)!/(n-k)!/k!
# (which is output implicitly as result)
APL(Dyalog Unicode), 12 9 bytes SBCS
It takes n and k as its left and right arguments, respectively, then outputs the coefficient of \$x^k\$ in the \$n\$-th approximant.
It calculates \$\frac{n!}{k!}\cdot{2n-k\choose n-k} = \frac{n!}{k!}\cdot\frac{(2n-k)!}{n!(n-k)!} = \frac{(2n-k)!}{k!(n-k)!}\$.
÷⍥!×-!⊣+-
÷⍥! ⍝ n!÷k!
× ⍝ times
- ⍝ n-k
! ⍝ choose from
⊣+- ⍝ n+n-k
JavaScript (ES6), 44 bytes
Expects (n)(k) and returns the corresponding coefficient.
n=>k=>(g=n=>n?n*g(n-1):1)(2*n-k)/g(k)/g(n-k)
Ruby, 41 bytes
->n,k{eval [[1,*k...n+n-=k]*?*,*1..n]*?/}
Same formula as @Tbw, but 1-based to make it shorter.
Uiua 0.13.0-dev.2, 15 bytes SBCS
÷∩/×+1⊃⇡-:+1+⇡.
Takes \$n\$ and \$k\$ and outputs the coefficient of \$x^k\$ in the numerator of the \$n\$th approximant. This is \$\frac{(2n-k)!}{k!(n-k)!}\$.
Charcoal, 18 bytes
NθNηI÷Π⁻⊕…θ⊗θηΠ⊕…η
Attempt This Online! Link is to verbose version of code. Explanation: Uses the same formula from @Tbw's answer.
Nθ Input `n` as a number
Nη Input `k` as a number
… Range from
θ Input `n` to
θ Input `n`
⊗ Doubled
⊕ Vectorised incremented
⁻ Vectorised subtract
η Input `k`
Π Take the product, i.e. `(2n-k)!/(n-k)!`
÷ (Integer) divided by
η Input `k`
… Range from `0`
⊕ Vectorised incremented
Π Take the product, i.e. `k!`
I Cast to string
Implicitly print