1

I'm definitely not a math man (quite negate for maths..), but I need to know one thing: I've got to calculate how many combinations there can be from n groups of options. I mean, example:

g1 = [a,b]

g2 = [d,e]

by 'combination' I mean a list of items where every item of that list belongs to a different group. So in this case: [a,e], [a,d], [b,d], [b,e].

what if I want to calculate the total number of combinations for example of 4 groups of variable number of items each?

g1 = [a,b,c]

g2 = [d,e]

g3 = [g,h,i]

g4 = [j,k,l,z]

I mean, every combination will be formed of 4 items where every item comes from a different group. [a,d,g,j], [b,d,g,j], etc...

:( Is there a simple formula I can apply?

[EDIT 1]

Both of the answers below (from @Ray and @tylerc0816) are accepted. Both gives details, one by providing a wikipedia link and the other by expliciting the formula. So THANK YOU to both! For future readers: I'm a programmer and this question was made to help me program a product (I mean: commercial item) combination system for an e-commerce. It's a really common path to give the administrator the possibility of automating the generation of all the possible product-options combinations, each with it's id, price, sku, image and so on. I needed a way to predict the total combinations for each product, and this simple formula gives me that.

2 Answers2

2

So you want to choose one item from each group? In that case, proceed as follows. Let's look at your first example. You see that there are $2$ items in each group. There are $2$ items in the first group, and $2$ items in the second group. Thus, there are $2 \times 2 = 4$ total ways of choosing one item from each group, which is precisely the answer you found.

For the second example, there are $3$ items in group $1$, $2$ items in group $2$, $3$ items in group $3$, and $4$ items in group $4$. Thus, there are $3 \times 2 \times 3 \times 4 = 72$ ways of choosing one item from each group.

In general, say you have $n_1$ items in group $1$, $n_2$ items in group $2$, and so on, up to $n_k$ items in group $k$. Then there are $n_1 \times n_2 \times \dotsb \times n_k$ ways of choosing one item from each group.

tylerc0816
  • 4,123
2

I think you are asking for Rule of Product.

Basically, it means you consider the generation of every combination as taking steps. For example, in this case, you choose exactly one element from each of the four groups g1 to g4 to form a 4 element combination.

Step 1: choose one element from g1, 3 choices.

Step 2: choose one element from g2, 2 choices.

and etc.

The core concept is that the steps are independent of each other, what you choose in one step has nothing to do with other steps. And the elements of each group is different, which makes sense. To get the total number of combinations, just take the product of those choices. $3\times2\times3\times4=72$ in this case.

And the most general case is just about the same. Relax, you can definitely handle this. :)

Ray
  • 333