ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   recursive or loopless combinations (https://www.excelbanter.com/excel-programming/400857-recursive-loopless-combinations.html)

Mazant, K.

recursive or loopless combinations
 
Does anybody knows how to generate all the
combinations, by selecting one element from each of the sets given below?

I'm looking for a "loopless" one or a recursive one,
sine the number of my sets can vary from 4 to 10

Any help will be appreciated.



My five sets are listed below.

{1,2,3}
{4,5,6,7}
(8,9,10}
{11,12,13,14}
(15,16,17,18}

The first combination can be:
1,4,8,11,15

and the last one,
3,7,10,14,18



Regards,

joel

recursive or loopless combinations
 
Try this code

Public InStrings(5)
Public Levels
Public combo
Public RowCount
Sub combinations()

InStrings(0) = Array(1, 2, 3)
InStrings(1) = Array(4, 5, 6, 7)
InStrings(2) = Array(8, 9, 10)
InStrings(3) = Array(11, 12, 13, 14)
InStrings(4) = Array(15, 16, 17, 18)

Levels = UBound(InStrings)
Level = 1
RowCount = 1
ReDim combo(UBound(InStrings))

Call recursive(Level)
End Sub
Sub recursive(ByVal Level As Integer)

Length = UBound(InStrings(Level - 1)) + 1

For i = 0 To (Length - 1)

combo(Level - 1) = InStrings(Level - 1)(i)
If Level = Levels Then
For j = 0 To (Levels - 1)
If j = 0 Then
ComboString = combo(j)
Else
ComboString = ComboString & "," & combo(j)
End If
Next j
Sheets("Sheet2").Range("A" & RowCount) = ComboString
RowCount = RowCount + 1
Else
Call recursive(Level + 1)
End If

Next i
End Sub



"Mazant, K." wrote:

Does anybody knows how to generate all the
combinations, by selecting one element from each of the sets given below?

I'm looking for a "loopless" one or a recursive one,
sine the number of my sets can vary from 4 to 10

Any help will be appreciated.



My five sets are listed below.

{1,2,3}
{4,5,6,7}
(8,9,10}
{11,12,13,14}
(15,16,17,18}

The first combination can be:
1,4,8,11,15

and the last one,
3,7,10,14,18



Regards,


Mazant, K.[_2_]

recursive or loopless combinations
 
Joel wrote:
Try this code

Public InStrings(5)
Public Levels
Public combo
Public RowCount
Sub combinations()

InStrings(0) = Array(1, 2, 3)
InStrings(1) = Array(4, 5, 6, 7)
InStrings(2) = Array(8, 9, 10)
InStrings(3) = Array(11, 12, 13, 14)
InStrings(4) = Array(15, 16, 17, 18)

Levels = UBound(InStrings)
Level = 1
RowCount = 1
ReDim combo(UBound(InStrings))

Call recursive(Level)
End Sub
Sub recursive(ByVal Level As Integer)

Length = UBound(InStrings(Level - 1)) + 1

For i = 0 To (Length - 1)

combo(Level - 1) = InStrings(Level - 1)(i)
If Level = Levels Then
For j = 0 To (Levels - 1)
If j = 0 Then
ComboString = combo(j)
Else
ComboString = ComboString & "," & combo(j)
End If
Next j
Sheets("Sheet2").Range("A" & RowCount) = ComboString
RowCount = RowCount + 1
Else
Call recursive(Level + 1)
End If

Next i
End Sub



"Mazant, K." wrote:

Does anybody knows how to generate all the
combinations, by selecting one element from each of the sets given below?

I'm looking for a "loopless" one or a recursive one,
sine the number of my sets can vary from 4 to 10

Any help will be appreciated.



My five sets are listed below.

{1,2,3}
{4,5,6,7}
(8,9,10}
{11,12,13,14}
(15,16,17,18}

The first combination can be:
1,4,8,11,15

and the last one,
3,7,10,14,18



Regards,




Thanks Joel.

Regards,


All times are GMT +1. The time now is 10:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com