By "combination", I assume (unlike the comment from myoarin) that you
are choosing "x" items from a list of "y" numbers (and not a
k-permutation). I am pulling this definition from "Introduction to
Algorithms" by Cormen, Leiserson, and Rivest. There is also an
interesting discussion of the difference by the "Mathagony Aunt" at
or search with a phrase like
permutation combination difference
for more references.
For a simple example of the difference, with the digits 0-3, a
2-combination includes the values:
01, 02, 03, 12, 13, 23
where the 2-permutation includes the values:
01, 02, 03, 10, 12, 13, 20, 21, 23, 30, 31, 32
Myoarin, however does make an interesting point if the range of values
is truly one through ten (and not zero through nine) and ten counts as
an item of length "2". Please make a clarification request if that is
important for your problem. The solutions below assume each value
selected is considered a single value when determining length.
Now, with that explanation, let's go through a few methods to generate
a combination and pointers to some source code to implement this.
If you need to print the combinations, a set of nested loops
structured like the following will work if "x" is fixed. (the example
below is for x=4).
FOR I1 = 0 to Y-3
FOR I2 = I1+1 to Y-2
FOR I3 = I2+1 to Y-1
FOR I4 = I3+1 to Y
PRINT I1, I2, I3, I4
You should notice that each inner loop starts at the value subsequent
to the current value of the outer loop. (the first value in this case
would be 0123, the last will be 6789 if y=9).
If you need a function that can be initialized and has a "get next" to
return the next combination (say in a loop doing actions for each
combination), see source code like
for source code in Java or
for source code in C#.
If some part of the answer is unclear or you need further information
on this topic, please make a clarification request.
Clarification of Answer by
13 Nov 2006 17:35 PST
The nested loops I provided in the original answer would satisfy your
revised problem description with a slight change.
Y = 15
to the beginning.
Change the first loop to read
FOR I1 = 1 to Y-3
The print statement would need to be revised to print each number with
two digits. The method to do this varies by language. A BASIC with
"PRINT USING" could use something like
PRINT USING "## ## ## ##", I1, I2, I3, I4
would display each number as two digits (with intervening spaces).
If the spaces are not desired, you could concatenate strings from an
array; something like:
A$(1) = "01"
A$(2) = "02"
A$(15) = "15"
at the start
for the print statement.
If you need further explanation, please make another clarification request.