Expand the Binomial

Haskell, 80 74 64 63 62 bytes

I've tried many more sophisticated approaches, but they all were longer than the straightforward approach. (In meantime golfed down a bit.)

Thanks for -1 byte @Laikoni!

(a#b)n|c<-b/a=(*a**n)<$>1:c*n:map(n*(n-1)*c*c*)[1/2,c*(n-2)/6]

Try it online!


Mathematica, 45 37 bytes

It turns out that Binomial threads over lists!

Binomial[#3,r={0,1,2,3}]#2^r#^(#3-r)&

Each of Binomial[#3,r={0,1,2,3}], #2^r and #^(#3-r) returns a length-4 list, which are implicitly multiplied together term-by-term, giving the binomial coefficients.


Old solution (45 bytes):

FoldList[#~D~x/#2&,(#+#2x)^#3,Range@3]/.x->0&

And a 43-byte solution that works except when n = 0:

Series[(#+#2x)^#3,{x,0,3}][[3]]~PadRight~4&

Haskell, 46 bytes

(a%b)n=scanl(*)(a**n)[(n-i+1)/i*b/a|i<-[1..3]]

Try it online!

Expresses the ratios of consecutive terms, and uses them to recover the terms by scanl(*) starting with the first term a*n. The ratios have form (n-i+1)/i*b/a.

Tags:

Math

Code Golf