6
\$\begingroup\$

Challenge

Given three numbers \$a\$, \$b\$, and \$n\$, you must expand \$(a + bx)^n\$ in ascending powers of \$x\$ up to \$x^3\$.

Binomial Expansion

Note that this method is an approximation

The binomial expansion works like so:

(a + bx)^n = a^n(1 + bx/a)^n
           = a^n(1 + n(bx/a) + n(n -1)(bx/a)^2/2! + n(n-1)(n-1)(bx/a)^3/3!)

Or, more readable:

$$\begin{align} (a+bx)^n &= a^n\left(1+\frac{b}{a}x\right)^n\\ &\approx a^n\left(1 + n\left(\frac{b}{a}x\right)+ \frac{n(n-1)}{2!}\left(\frac{b}{a}x\right)^2 + \frac{n(n-1)(n-2)}{3!}\left(\frac{b}{a}x\right)^3\right) \end{align}$$

Which simplifies down to an expression in the form

$$\alpha + \beta x + \gamma x^2 + \delta x^3$$

Where \$\alpha\$, \$\beta\$, \$\gamma\$ and \$\delta\$ are constants which you must calculate.

These constants should be given to at least 3 decimal places where appropriate (i.e. 1.2537 should be output instead of 1.25, but 1.2 can be output if the answer is exactly 1.2).

You must then output these four constants in a list (in order as they appear in the final equation). This list may be separated however you wish.

Input

The inputs \$a\$ and \$b\$ will be integers in the range -100 to 100 inclusive where \$a \neq 0\$.

\$n\$ will be given to one decimal place and will be in the range \$-2 \leq n \leq 2\$

Examples

a = 1, b = 6, n = -1.0

1, -6, 36, -216

a = 3, b = 2, n = 2.0

9, 12, 4, 0

a = 5, b = 4, n = 0.3

1.621, 0.389, -0.109, 0.049

Winning

The shortest code in bytes wins.

\$\endgroup\$
7
  • \$\begingroup\$ Related \$\endgroup\$
    – Beta Decay
    Apr 15, 2017 at 9:10
  • \$\begingroup\$ There is no way your equations can hold, as the left side has degree n in x, where as the right side has degree 3. \$\endgroup\$
    – flawr
    Apr 15, 2017 at 9:16
  • \$\begingroup\$ @flawr This method is a way to approximate the value of the polynomial \$\endgroup\$
    – Beta Decay
    Apr 15, 2017 at 9:18
  • \$\begingroup\$ is a guaranteed to be non-zero? \$\endgroup\$
    – tsh
    Apr 15, 2017 at 9:23
  • 1
    \$\begingroup\$ Oh look! There goes Mathematica! \$\endgroup\$ Apr 15, 2017 at 14:39

8 Answers 8

4
\$\begingroup\$

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&
\$\endgroup\$
1
  • 1
    \$\begingroup\$ ooo, I love the Series...[[3]] hack! \$\endgroup\$ Apr 15, 2017 at 18:22
4
\$\begingroup\$

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!

\$\endgroup\$
0
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Casio BASIC, 144 bytes

?→A
?→B
?→N
B÷A→B
A^N→A
A((N(N-1))÷2!×B²)→S
A((N(N-1)(N-2)÷3!×B³)→T
N×B×A→N
ClrText
Locate 1,1,A
Locate 1,2,N
Locate 1,3,S
Locate 1,4,T

Output is in a line like so:

34.5
20
11
56.85
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 55 53 50 bytes

(a,b,n)=>[s=a**n,s*=n*(b/=a),s*=--n*b/2,s*--n*b/3]
(a,b,n)=>a?[s=a**n,s*=n*(b/=a),s*=--n*b/2,s*--n*b/3]:(t=[0,0,0,0])|n>3?t:(t[n]=b**n,t)
\$\endgroup\$
1
\$\begingroup\$

MATL, 30 bytes

^li1G/3:"t@^2G@:q-HM/p*w]x4$h*

Try it online!

Input in a, n, b order.

\$\endgroup\$
1
\$\begingroup\$

Pari/GP, 29 bytes

(a,b,n)->Vec((a+b*x)^n)[1..4]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Axiom, 80 bytes

h(a,b,n)==(v:=series((a+b*x)^(n::Fraction INT));[coefficient(v,i)for i in 0..3])

exercises

(91) -> h(1,6,-1.0)
...
   (91)  [1,- 6,36,- 216]
                                            Type: List Expression Integer
(92) -> h(3,2,2.0)
   (92)  [9,12,4,0]
                                            Type: List Expression Integer
(93) -> h(5,4,0.3)
                  10+-+3     10+-+3    10+-+3
          10+-+3 6 \|5     42 \|5   476 \|5
   (93)  [ \|5  ,-------,- --------,---------]
                    25        625     15625
                                            Type: List Expression Integer
(94) -> map(numeric, %)
   (94)
   [1.6206565966 927624351, 0.3889575832 0626298443, - 0.1089081232 9775363564,
    0.0493716825 6164831482 4]
                                                         Type: List Float
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.