# Help calculating combination of combinations

I have a problem which I thought was really easy to solve but now I am here =)

I need to construct a final combination of a content based on combinations of various sub-contents. A sub content is a group of items which can be varied in combinations. The example below lists each groups possible combinations not the items in the group that make up the combinations.

The number of sub-contents can vary to any integer. The number of combinations in a sub-content can also vary to any integer.

Example.

Sub-content:x -> 4 combinations Xa,Xb,Xc,Xd

Sub-content:y -> 10 combinations Ya,Yb,Yc,Yd,Ye,Yf,Yg,Yh,Yi,Yj

Sub-content:z -> 3 combinations Za,Zb,Zc

where


These three different sub-contents will construct the whole content and should result in 4 * 10 * 3 = 120 combinations, right?

Combination 1 should be Xa,Ya,Za and combination 5 should be Xa,Yb,Za etc

  1 2 3 4 5 .. 14 .. 120
X a b c d a    b
Y a a a a b    d
Z a a a a a    a


Seems like an easy enough task to calculate but I cant manage to get it right.

I need a way to calculate which sub-contents a specific combination (1-120) is based upon

Thankful for any assistance and excuse me for not knowing the correct terms.

• – JRN
Aug 16, 2012 at 13:02

Suppose you have a number $n$, $1\le n\le120$. Divide $n-1$ by 4. You have Xa, Xb, Xc, or Xd according as the remainder, $r$, is 0, 1, 2, or 3.

Then take $m=(n-1-r)/4$ and divide it by 10. You have Ya, Yb, ..., Yj according as the remainder $s$ is 0, 1, ..., 9.

Then take $(m-s)/10$ and divide it by 3. You have Za, Zb, or Zc according as the remainder is 0, 1, or 2.

• Thanks for the answer. I managed to convert that into code which solves all combinations regardless of how many sub-contents and versions. Thank you! Aug 17, 2012 at 8:35
• @Niclas, would you mind sharing your code? Oct 10, 2014 at 17:11

$120$ is correct if you want one from each.

If you write / for integer division and % for remainder so 13 / 5 = 2 and 13 % 5 = 3 since 13 = 2 * 5 + 3 and you look at the following functions

• (n - 1) / 30 + 1
• ((n - 1) / 3) % 10 + 1
• (n - 1) % 3 + 1

then the first will tell you which $X$ to choose, the second which $Y$ and the third which $Z$, with $n$ running from $1$ to $120$. There are other solutions, and it would be simpler if you started counting at $0$ rather than $1$.