contagem2.mws

Combinações com repetição

Combinações com repetição de n elementos 5 a 5 = C(n+5-1,5) = C(n+4,5)

>    alg:=proc(n::integer)

>       local k,i1,i2,i3,i4,i5;

>       k := 0;

>       for i1 from 1 to n do

>          for i2 from 1 to i1 do

>             for i3 from 1 to i2 do

>                for i4 from 1 to i3 do

>                   for i5 from 1 to i4 do

>                       k := k+1;

>                   od;

>                od;

>             od;

>          od;

>       od;

>       RETURN(k);

>    end;

alg := proc (n::integer) local k, i1, i2, i3, i4, i5; k := 0; for i1 to n do for i2 to i1 do for i3 to i2 do for i4 to i3 do for i5 to i4 do k := k+1 end do end do end do end do end do; RETURN(k) end p...
alg := proc (n::integer) local k, i1, i2, i3, i4, i5; k := 0; for i1 to n do for i2 to i1 do for i3 to i2 do for i4 to i3 do for i5 to i4 do k := k+1 end do end do end do end do end do; RETURN(k) end p...

>    alg(10); alg(5);

2002

126

>    binomial(10+4,5); binomial(5+4,5);

2002

126

>    combinat[multinomial](11,1,4,4,2);

34650

>    combinat[multinomial](11,1,4,4,3);

Error, (in combinat/multinom) 1st argument must equal the sum of the others

>    for n from 1 to 4 do

>        sort(expand((x + y + z)^n));

>    od;

x+y+z

x^2+2*x*y+2*x*z+y^2+2*y*z+z^2

x^3+3*x^2*y+3*x^2*z+3*x*y^2+6*x*y*z+3*x*z^2+y^3+3*y^2*z+3*y*z^2+z^3

x^4+4*x^3*y+4*x^3*z+6*x^2*y^2+12*x^2*y*z+6*x^2*z^2+4*x*y^3+12*x*y^2*z+12*x*y*z^2+4*x*z^3+y^4+4*y^3*z+6*y^2*z^2+4*y*z^3+z^4

>    combinat[multinomial](4,0,0,4);combinat[multinomial](4,0,1,3);combinat[multinomial](4,0,2,2);combinat[multinomial](4,1,1,2);

1

4

6

12

A instrução i $ r significa "repete i r vezes"; 1 $ 3 significa "1 1 1"

>    with(combinat);

>    rCombRepete := proc(n::posint, r::posint)

>       local repetelista, i;

>       repetelista := [seq(i $ r,i=1..n)];

>       choose(repetelista,r);

>    end:  

>   

Warning, the protected name Chi has been redefined and unprotected

[Chi, bell, binomial, cartprod, character, choose, composition, conjpart, decodepart, encodepart, fibonacci, firstpart, graycode, inttovec, lastpart, multinomial, nextpart, numbcomb, numbcomp, numbpart...
[Chi, bell, binomial, cartprod, character, choose, composition, conjpart, decodepart, encodepart, fibonacci, firstpart, graycode, inttovec, lastpart, multinomial, nextpart, numbcomb, numbcomp, numbpart...

>    rCombRepete(3,2);

[[1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3]]

>    rCombRepete(4,3);

[[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 1, 4], [1, 2, 2], [1, 2, 3], [1, 2, 4], [1, 3, 3], [1, 3, 4], [1, 4, 4], [2, 2, 2], [2, 2, 3], [2, 2, 4], [2, 3, 3], [2, 3, 4], [2, 4, 4], [3, 3, 3], [3, 3, 4], [3...
[[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 1, 4], [1, 2, 2], [1, 2, 3], [1, 2, 4], [1, 3, 3], [1, 3, 4], [1, 4, 4], [2, 2, 2], [2, 2, 3], [2, 2, 4], [2, 3, 3], [2, 3, 4], [2, 4, 4], [3, 3, 3], [3, 3, 4], [3...

>