Re: Combination List

• To: mathgroup at smc.vnet.net
• Subject: [mg78111] Re: Combination List
• From: m.r at inbox.ru
• Date: Sat, 23 Jun 2007 07:07:10 -0400 (EDT)
• References: <f55qci\$k58\$1@smc.vnet.net><f58bk7\$6ve\$1@smc.vnet.net>

```On Jun 20, 4:26 am, "Bruno Campanini" <B... at gmail.com> wrote:
>
> May I go on...
> What if I can have one or more repeated elements in subsets?
> That is the number of combinations of 3 elements out of 4
> - with one or more duplicated elements - is Binomial[4+3-1,3] = 20
>
> How can I get the list?
>
> Bruno

Here's one way:

In[1]:= f = Compile[{{n, _Integer}, {k, _Integer}},
Module[{ans, i = 1, j = 1, l = 1},
ans = Array[0 &, {Binomial[n + k - 1, k], k}];
While[True,
While[j <= k, ans[[i, j++]] = l];
If[i == Length@ans, Break[]];
While[ans[[i, --j]] == n,];
l = ans[[i++, j]] + 1;
ans[[i]] = ans[[i - 1]];
];
ans
]]

In[2]:= f[4, 3]

Out[2]= {{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,
4, 4}, {4, 4, 4}}

Maxim Rytin
m.r at inbox.ru

```

• Prev by Date: Re: Quick Question
• Next by Date: Re: Integrate modified in version 6?
• Previous by thread: Re: Combination List
• Next by thread: Re: Combination List