# All Unique Combinations of any array of numbers

Page 1 of 1

## 5 Replies - 5685 Views - Last Post: 16 September 2009 - 10:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=125705&amp;s=f69712d3679f86fbb38a661d482feecf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sbrijnath

Reputation: 0
• Posts: 3
• Joined: 14-September 09

# All Unique Combinations of any array of numbers

Posted 14 September 2009 - 02:31 AM

Does anyone have a fast porcedure to list all unique combinations of any array of letters / numbers. For example, 5 letters will generate 2^5 = 32 unique combinations.....
Is This A Good Question/Topic? 0

## Replies To: All Unique Combinations of any array of numbers

### #2 vb5prgrmr

• D.I.C Lover

Reputation: 109
• Posts: 1,016
• Joined: 21-March 09

## Re: All Unique Combinations of any array of numbers

Posted 14 September 2009 - 06:23 AM

Multiple nested for loops is what you are looking for...
```Dim N1 As Integer, N2 As Integer,..., N5 As Integer
Dim MyArray(1 To 10) As String, MyString As String
Dim L As Integer, U As Integer
'...
L = LBound(MyArray)
U = UBound(MyArray)

For N1= L To U
For N2 = N1 + 1 To U
For N3 = N2 + 1 To U
'...

```

Just an example on how to get started...

Good Luck

### #3 sbrijnath

Reputation: 0
• Posts: 3
• Joined: 14-September 09

## Re: All Unique Combinations of any array of numbers

Posted 15 September 2009 - 03:09 AM

vb5prgrmr, on 14 Sep, 2009 - 05:23 AM, said:

Multiple nested for loops is what you are looking for...
```Dim N1 As Integer, N2 As Integer,..., N5 As Integer
Dim MyArray(1 To 10) As String, MyString As String
Dim L As Integer, U As Integer
'...
L = LBound(MyArray)
U = UBound(MyArray)

For N1= L To U
For N2 = N1 + 1 To U
For N3 = N2 + 1 To U
'...

```

Just an example on how to get started...

Good Luck

### #4 sbrijnath

Reputation: 0
• Posts: 3
• Joined: 14-September 09

## Re: All Unique Combinations of any array of numbers

Posted 15 September 2009 - 08:55 PM

Thanks very much for this. The issue I have is with the exponential time aspect of this code. It works well for upto n =10 but then slows dramatically. Is there code anywhere that is optimized for (say) n =20 or 30 ?

Regards

### #5 vb5prgrmr

• D.I.C Lover

Reputation: 109
• Posts: 1,016
• Joined: 21-March 09

## Re: All Unique Combinations of any array of numbers

Posted 16 September 2009 - 07:31 AM

No not really, but you can get the same results from a recursive call that I have heard is faster but have never tested it.

Good Luck

### #6 raziel_

• Like a lollipop

Reputation: 469
• Posts: 4,280
• Joined: 25-March 09

## Re: All Unique Combinations of any array of numbers

Posted 16 September 2009 - 10:01 AM

it is faster but i dont know if you dont got an error of stack overflow. but with 30 dont think it will happen i use it for something like that.