How to calculate the number of combinations of $x$ integers, each with a value between $y$ and $z$?

94 Views Asked by At

For example, if I have 4 integers, and each can be between 0 and 36, how many combinations are there?

If the numbers have appeared before, but in a new order, then this still counts as a new combination e.g

$$1,5,3,4$$ $$1,5,4,3$$

Are different combinations.

So how many combinations all together will there be?

Also, how can I write an algorithm that will give me all of these combinations? I need to write some code that will cycle though every combination. My best attempt so far is:

start
a=0,b=0,c=0,d=0

increment d by 1 until d=36
increment c by 1 and then repeat the last step
repeat this until c=36
increment b by 1 and repeat the last 3 steps
repeat this until b=36
increment a by 1 and repeat the last 4 steps
repeat this until a=36

However I do not know if this will even give me every combination. I just cant wrap my head around it. I also do not know if this is a very efficient algorithm. In code this will most likely be implemented with nested for-loops which may take a long time. Is there a better way?

1

There are 1 best solutions below

1
On

Your psuedocode should be nested loops, with optional indentation for clarity.

$$\sum_{a=0}^{36}\sum_{b=0}^{36}\sum_{c=0}^{36}\sum_{d=0}^{36} 1 =\quad\begin{array}{|l}\textrm{Let n=0} \\ \textrm{For a = 0 to 36 step 1} \\ \quad\textrm{For b = 0 to 36 step 1} \\ \qquad\textrm{For c = 0 to 36 step 1} \\ \qquad\quad\textrm{For d = 0 to 36 step 1} \\ \qquad\qquad\textrm{n = n+1} \\ \qquad\quad\textrm{Next d} \\ \qquad\textrm{Next c} \\ \quad\textrm{Next b} \\ \textrm{Next a} \\ \textrm{Return n } \end{array}$$

And clearly that is just counting $37^4$, isn't it?