Combinational Algorithm

Associate
Joined
30 Apr 2003
Posts
1,794
Location
The dark side of the moon
I'm trying to code a combinational algorithm and I'm struggling a little bit.

I am looking to generate every possible, unique 4 digit array from these 10 numbers.
eg.

numbers = [1,2,3,4,5,6,7,8,9,10]
length = 4

Output:
1,2,3,4
1,2,3,5
1,2,3,6
...
...
...
6,7,1,2
6,7,1,3
etc
etc

Can anyone offer any advice?
 
Combinations and permutations...I can't remember a thing about these :D

For combinations, are 1,1,1,2 and 2,1,1,1 counted as different results or is that permutations?
 
Sorry, I should have been more clear :)

I only want unique numbers.

ie.

4,7,2,3
7,2,9,1

but not

1,1,6,8
4,5,1,4
etc

Hope that makes more sense :)
 
Cool - so using that example, does 7,2,9,1 count as a separate result to 7,2,1,9? ie, does the order matter? (that's the question I meant to ask at first)

Just trying to refresh my brain here with all this stuff in general so that I stand a chance of helping :D
 
It would be good if they were treated the same in order that the number of combinations is less, but to be honest it doesn't matter that much if 4,9,7,1 is said to be the same as 9,4,7,1.
 
Ah cool. I remember all this now :D

I have a handy page here which may help. There's a section on there which'll show all the combinations - I don't want to give you the answer but I haven't found a tutorial as such on the concepts.

And it's written in un-commented obscure Javascript anyway so to interpret it (I believe the relevant function from the source code is doperm() ) you'll need to have a rough idea of the theories anyway, which I guess you do, or you wouldn't be trying/having to do this :)
 
Beansprout said:
Ah cool. I remember all this now :D

I have a handy page here which may help. There's a section on there which'll show all the combinations - I don't want to give you the answer but I haven't found a tutorial as such on the concepts.

And it's written in un-commented obscure Javascript anyway so to interpret it (I believe the relevant function from the source code is doperm() ) you'll need to have a rough idea of the theories anyway, which I guess you do, or you wouldn't be trying/having to do this :)
Thanks, that helped a lot.

I'm going to try coding my own in C# based on that now. I'll post the code if/when I get it working :)
 
This can be done very concisely and neatly using recursion (I wrote this program in 3 lines of ML once. . .but I cant remember how :( ).
 
Last edited:
Back
Top Bottom