Fzen
Fzen

Reputation: 25

Get a^ib^jc^k from expand (a+b+c)^n

I want to write a function func(n) get a^ib^jc^k from expand (a+b+c)^n.

Example :

When n = 1 then (a + b + c)^1 = a + b + c and func(n) return {a, b, c}.

When n = 2 then (a + b + c)^2 = a^2 + b^2 + c^2 + 2ab + 2bc + 2ca and func(n) return {a^2, b^2, c^2, ab, bc, ca}.

I have solution using three loops but in think it's not nice, someone can help me. Thanks you very much.

Upvotes: 0

Views: 111

Answers (1)

zhk
zhk

Reputation: 331

In maple to do some sort of programming you can use proc short for procedure.

restart:
ftn:= proc(n)
     expand((a+b+c)^n);
     {op(%)};
     end;

Now let's try to check whether we get what you want.

ftn(1);

{a, b, c}

ftn(2);

{a^2, b^2, c^2, 2*a*b, 2*a*c, 2*b*c}

If you do not want the constant coefficients of ab, ac and bc then you can do this

restart:
ftn:= proc(n)
     expand((a+b+c)^n);
     convert([coeffs(%,[a,b,c],'powers')],array):
    convert({powers},list);
     end;
ftn(2);

[a^2, b^2, c^2, ab, ac, b*c]

Upvotes: 1

Related Questions